0

I created a Chrom extension.

Among the things the extension suppose to do is to run a script in the context of the page after the page loaded.

For some reason, the script isn't running and I'm not sure what am I doing wrong.

Here's the manifest.json

{
"name": "extension sample",
"version": "1.2",
"description": "some desc",
"manifest_version": 2,
"author": "",
"permissions": ["declarativeContent", "storage", "tabs", "activeTab", "https://*/*", "http://*/*"],
 "background": {
    "scripts": 
    ["background.js" ],
    "persistent": false
},
"content_scripts": [
{ "run_at" :"document_end",
  "matches": ["https://*/*""],
  "js": ["snippet.js"],
  "all_frames": true    
} ],
"web_accessible_resources":["snippet.js"],  
"content_security_policy": "script-src 'self' https://www.google.com; object-src 'self'",
"browser_action": {
  "default_popup": "popup.html",
  "default_icon": {
    "16": "images/get_started16.png",
    "32": "images/get_started32.png",
    "48": "images/get_started48.png",
    "128": "images/get_started128.png"
  }
},
"icons": {
  "16": "images/get_started16.png",
  "32": "images/get_started32.png",
  "48": "images/get_started48.png",
  "128": "images/get_started128.png"
}

}

This is the code that runs the script:

chrome.tabs.query({currentWindow: true, active: true}, function (tab) {
   chrome.tabs.executeScript(tab.id, {
                file: 'snippet.js'                  
            }); 

});

I just placed the script execution instead of the reload method just to focus on the script execution.

So my question is, why is the script not running?

Ace
  • 831
  • 2
  • 8
  • 28
  • Your manifest.json already declares the automatically injected content script on all `https` pages so you don't need executeScript. You also don't need to list it in web_accessible_resources. You might want to replace `"https://*/*""` with `""` to run the content script on all URLs, not just https. – wOxxOm Mar 13 '19 at 12:29
  • I tried not running the executeScript but it's still not running. – Ace Mar 13 '19 at 12:34
  • 1
    executeScript is not the problem, it's just not needed here. I'll try to rephrase my comment above. The problem is that the site may be `http` but your manifest runs only on `https` so a better approach would be to use `""`. You can verify a content script is injected in web page devtools - the Sources panel, the Content scripts sub-panel. Also don't forget to reload the extension on chrome://extensions page and the web page itself. That being said, the problem may be inside your snippet.js and the way it interacts with the site, but you didn't show the code. – wOxxOm Mar 13 '19 at 12:40
  • Ok - so looks like the problem was indeed that I didn't reloaded the extension. Now I can see that the script is running but it doesn't run in the context of the main page so it can reference a variable there but in the context of the extension so the variable it tries to reference it not defined – Ace Mar 13 '19 at 13:31
  • 1
    To access the page context see [this answer](https://stackoverflow.com/a/9517879). – wOxxOm Mar 13 '19 at 13:34
  • I looked at the above link and solved the issue. The last thing I'm trying to achieve is pass parameters to the injected script. I looked at "Dynamic values in the injected code" but didn't understand the example. Do I need to use some kind of a postMessage communication? – Ace Mar 13 '19 at 14:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/189955/discussion-between-avi-and-woxxom). – Ace Mar 13 '19 at 14:38

0 Answers0