title: chrome extension content script before tab-close or page change
body: <p>I have a content-script that's initiated in my <code>background.js</code> with:</p>
  
  <pre><code>chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
      var url = (new String(tab.url)).valueOf();
      if(url.indexOf("chrome") == 0 || url == undefined || url == 'undefined')
          return;
      if(changeInfo.status == 'complete') {            
          chrome.tabs.executeScript(tabId,
          {file: "js/control.js", runAt:"document_end"},
          function() { console.log("Control sent to page");
      });
      console.log("ChangeInfo " + changeInfo.status);
  });
  </code></pre>
  
  <p>I need to execute a function before the page changes or the window closes. I've looked around and can't figure out how to do this. The closest thing I could find was:</p>
  
  <pre><code>chrome.runtime.onSuspend.addListener(function () {
      someObjectDefinedInScript.afunction("stopping");
  });
  </code></pre>
  
  <p>Because I got <code>chrome.runtime.sendMessage</code> to work in the <strong>content-script</strong>.</p>
  
  <p>But this returns an error:</p>
  
  <pre><code>Uncaught TypeError: Cannot call method 'addListener' of undefined
  </code></pre>