How to grab data from webpage in Chrome and output into Chrome extension popup?

Posted by chimerical on Stack Overflow See other posts from Stack Overflow or by chimerical
Published on 2010-05-26T16:38:09Z Indexed on 2010/05/26 16:41 UTC
Read the original article Hit count: 295

For a Google Chrome extension, none of the Javascript I write to manipulate the DOM of the extension popup.html seems to have any effect on the popup's DOM. I can manipulate the DOM of the current webpage in the browser just fine by using content_script.js, and I'm interested in grabbing data from the webpage and outputting it into the extension popup, like so (below: popup.html):

<div id="extensionpopupcontent">Links</div>
<a onclick="click()">Some Link</a>

<script type="text/javascript">
 function click() {
  chrome.tabs.executeScript(null, {file: "content_script.js"});
  document.getElementById("extensionpopupcontent").innerHTML = variableDefinedInContentScript;
  window.close();
 }
</script>

I tried using chrome.extension.sendRequest from the documentation at http://code.google.com/chrome/extensions/messaging.html, but I'm not sure how to properly use it in my case, specifically the greeting and the response.

contentscript.js
================
chrome.extension.sendRequest({greeting: "hello"}, function(response) {
  console.log(response.farewell);
});

© Stack Overflow or respective owner

Related posts about google-chrome-extension