I'm attempting to pull data from a page that a user is viewing and sending that data to the extension popup. I'm running into trouble extracting information from the current page. After reviewing the google code tutorials, I set up my manifest and content script as shown below. When I go to a test page, which has a span with id="comments", my variable from the injected content script always ends up as null. Anyone know what I could be missing? Thanks!
manifest.json
=================
{
"name": "Sample Extension",
"version": "0.0.1",
"description": "Sample extension",
"icons": {"128": "icon.png"},
"permissions": [
"tabs", "<all_urls>"
],
"browser_action": {
"default_icon": "browseraction.png",
"default_title": "Sample",
"popup": "popup.html"
},
"content_scripts": [
{
"matches": ["http://*/*", "<all_urls>" ],
"js": ["scripts/contentscript.js"]
}
]
}
contentscript.js
===================
var comments = document.getElementById("comments");
alert( comments.innerText );
It's possible the comments are loaded after the execution of your script.
Try to set this in the manifest :
"run_at" : "document_end",
Or if you suspect an AJAX loading, call your code later with a setTimeout
.
Of course the best scheme would depend on the precise page and its inner working.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With