title: page content when tab changed in chrome extension
body: <p>background.js</p>
  
  <pre><code>var htmlcontent = null;
  
  chrome.tabs.onUpdated.addListener(function() {
  onWindowLoad();
  alert(htmlcontent);
  });
  
  chrome.tabs.onHighlighted.addListener(function(){
  onWindowLoad();
  alert(htmlcontent);
  });
  
  function onWindowLoad() {
  chrome.tabs.executeScript(null, {
  file: "getPagesSource.js"
  }, function() {
  if (chrome.extension.lastError) {
  htmlcontent = 'There was an error injecting script : \n' + chrome.extension.lastError.message;
  }
  });
  }
  
  chrome.extension.onMessage.addListener(function(request, sender) {
  if (request.action == "getSource") {
  htmlcontent = request.source;
  }
  });
  </code></pre>
  
  <p>getPagesSource.js</p>
  
  <pre><code>function DOMtoString(document_root) {
  return document.body.innerText ;
  }
  
  chrome.extension.sendMessage({
  action: "getSource",
  source: DOMtoString(document)
  });
  </code></pre>
  
  <p>I am developing a chrome extension. I am able to get the Body Content of the current window on a reload but I am not able to get the content when I change a tab. The previous tab's content is getting saved. Am I using the wrong API? Or How do I trigger the onUpdate functionality when I change a tab in Chrome?</p>