0

I have the following code for a chrome extension, the contentscript.py as per another question on stackoverflow:

manifest.js

{
"name": "test script",
"version": "0.1",
"content_scripts": [{
    "js": ["contentscript.js"],
    "matches": ["http://*/*"]
}],
"manifest_version": 2,
"web_accessible_resources": ["script.js"]
}

contentscript.js

var s = document.createElement('script');
s.src = chrome.extension.getURL('script.js');
(document.head||document.documentElement).appendChild(s);
s.onload = function() {
    s.parentNode.removeChild(s);
};

script.js

document.body.innerHTML = document.body.innerHTML.replace(new RegExp("this", "gi"), "that");

In order to simply replace some text. If I put the same expression that is on script.js on a test html file, it works, but it in the extension it doesn't appear as though the code is actually injected. I cannot for the life of me figure out why. The manifest and the contentscript seem in order, so I don't know what to do here.

andrepd
  • 587
  • 6
  • 20
  • 1
    Why don't you do your stuuf from within `contentscript.js` instead of injecting another script ? Content scripts do have access to the DOM. – gkalpak Nov 11 '13 at 12:36
  • BTW, your code also works fine for me. Where is your `scripts.js` located ? Are you sure it is injected ? Again: why bother using another script (which you have to declare as "web-accessible resource", instead of doing the same thing from within the content script ? – gkalpak Nov 11 '13 at 12:45
  • Apparently, though, content scripts are executed in an isolated environment, as per here: http://stackoverflow.com/questions/9515704/building-a-chrome-extension-inject-code-in-a-page-using-a-content-script?lq=1 – andrepd Nov 11 '13 at 16:43

1 Answers1

0

try adding "https://*/*" to matches in manifest.json. I recommend you to put this first line in your contentscript.js:

console.log('Content script loaded and started');

So you can inspect the page (F12, console tab) and see if the content script was injected or not. It'll help a lot with your debugging.

I don't know what is going on in your script.js, but it worth consider that your removechild in the onload event may occur before your task being done.

gkalpak
  • 47,844
  • 8
  • 105
  • 118
Alejandro Silvestri
  • 3,706
  • 31
  • 43
  • "_consider that your removechild in the onload event may occur before your task being done_": What makes you think that ? – gkalpak Nov 11 '13 at 13:05
  • I have put this debug at the start of both .js files, and nothing gets printed to the console. I have no idea why this isn't working. Perhaps a problem with my installation of chrome? – andrepd Nov 11 '13 at 16:47
  • If you don't have an error messages in the console regarding your content script, check if your content_scripts.matches is actually matching, in manifest.json – Alejandro Silvestri Nov 20 '13 at 11:35