How to grab content from DOM of page and output to extension's popup?

8,133 views
Skip to first unread message

birdofprey

unread,
May 26, 2010, 1:34:15 AM5/26/10
to Chromium-extensions
I'm trying to grab data from the webpage's DOM, and output it into the
extension popup, but none of the JS in popup.html seems to have any
effect on the DOM of popup.html.

For example, in this example of popup.html below, I'm using
content_script.js to grab content from the webpage's DOM, and output
it into the extension popup.

<!--Start-->
<div id="extensioncontent"></div>
<a onclick="click()">Scrape</a>

<script>
function click() {
chrome.tabs.executeScript(null, {file: "content_script.js"});
document.getElementById("extensioncontent").innerHTML =
stringDefinedInContentScript;
window.close();
}
</script>
<!--End-->

I tried using the example here:
http://code.google.com/chrome/extensions/messaging.html
in content_script.js, but I'm not sure what greeting: "hello" and
response should be.

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

Arne Roomann-Kurrik

unread,
May 26, 2010, 5:39:17 PM5/26/10
to birdofprey, Chromium-extensions
You can't access data in a content script directly from a popup.  You had the right idea with messaging, but it looks like your popup wasn't set up to listen for messages.  To do this, you'd need to do something like this:

  chrome.extension.onRequest.addListener(
      function(request, sender, sendResponse) {
          if (request.message) {
              // Do something with the text
          }
          sendResponse({});
      }
  );


Then your content script should have code similar to:
    var myMessage = "...";  // Assign your text to this variable
    chrome.extension.sendRequest({message: myMessage}, function(response) {});

~Arne


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.


birdofprey

unread,
May 26, 2010, 6:29:04 PM5/26/10
to Chromium-extensions
Hmm, it's still not making any changes to the popup, even after
assigning a value to myMessage and passing that variable.

Here's what I have:

===============
content_script.js
===============
var myMessage =
document.getElementById("topfoot").getElementsByTagName("small")
[0].innerHTML;


chrome.extension.sendRequest({message: myMessage}, function(response)
{});


===============
popup.html
===============
<style type="text/css">
a {text-decoration:underline;}
</style>

<div id="quoted">Placeholder</div>


<a onclick="click()">Scrape</a>

<script type="text/javascript">


function click() {
chrome.tabs.executeScript(null, {file: "content_script.js"});

document.getElementById("quoted").innerHTML = myMessage;
window.close();
}

chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
if (request.message) {

document.getElementById("quoted").innerHTML =
myMessage;
}
sendResponse({});
}
);
</script>


===============
manifest.json
===============
{
"name": "A browser action with a popup that grabs a quote.",
"version": "1.0",
"permissions": [
"tabs", "http://*/*", "https://*/*"
],
"browser_action": {
"default_title": "Grab a quote.",
"default_icon": "icon.png",
"popup": "popup.html"
}
}

> > To post to this group, send email to chromium-extensi...@chromium.org.


> > To unsubscribe from this group, send email to

> > chromium-extensions+unsubscr...@chromium.org<chromium-extensions%2Bunsubscr...@chromium.org>

birdofprey

unread,
May 26, 2010, 6:30:09 PM5/26/10
to Chromium-extensions
(This is when using slashdot.org as an example page.)

Arne Roomann-Kurrik

unread,
May 26, 2010, 6:34:49 PM5/26/10
to birdofprey, Chromium-extensions
Try changing the code from:

if (request.message) {
    document.getElementById("quoted").innerHTML = myMessage;
}

to 

if (request.message) {
    document.getElementById("quoted").innerHTML = request.message;
}

To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.

birdofprey

unread,
May 26, 2010, 7:19:22 PM5/26/10
to Chromium-extensions
Reply all
Reply to author
Forward
0 new messages