21

So I am trying to execute a script from external source like www.script.google.com in background.js. But I get this error -

Unchecked runtime.lastError while running tabs.executeScript: Cannot access contents of url "chrome-devtools://devtools/bundled/devtools.html?&remoteBase=https://chrome…&dockSide=undocked&toolbarColor=rgba(223,223,223,1)&textColor=rgba(0,0,0,1)". Extension manifest must request permission to access this host.

What i am doing is sending message from popup.js to background.js In popup.js -

 chrome.runtime.sendMessage({type:"addtask"});

In background.js -

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
    if(request.type == "addtask")
    {
        chrome.tabs.executeScript(null,
                       {file:"https://script.google.com/url of script....."});
    }
});

My manifest.json-

{
    "name": "Extension",
    "version": "0.0.1",
    "manifest_version": 2,
    "browser_action": {
        "default_popup": "popup.html"
    },
     "background": {
        "scripts": ["background.js"],
        "persistent": false
    },
    "content_scripts": [{
        "js": ["jquery.min.js","contentscript.js"],
        "matches": ["http://*/*","https://*/*"],
        "css" : ["feedback.css"]
    }],
    "permissions": [
          "storage","tabs","https://script.google.com"
        ],
    "web_accessible_resources": ["feedback.js","html2canvas.js","event.js"],
    "content_security_policy": "script-src 'self' https://script.google.com/*; object-src 'self'"
}
Siddharth
  • 6,966
  • 2
  • 19
  • 34
  • Your intention is not 100% clear. Are you trying to execute the external script as a content script, or as a background script? – Xan Jun 25 '15 at 14:00
  • @Sid, If you'd want to catch the lastError, see https://stackoverflow.com/a/45603880/632951 – Pacerier Aug 10 '17 at 03:36

3 Answers3

39

Plain and straight. Add *://*/* to permissions.

Hello World
  • 840
  • 10
  • 20
  • 2
    That's incredible. After hours of research, I found out my permission urls were wrong: the trailing asterisk symbol was missing so it only matched homepages. Chrome last time error was somewhat misleading, as it was censoring the urls I was trying to access with "data:text/html,chromewebdata" – Anonymous May 22 '17 at 18:58
  • 3
    Can anyone explain, is this the same as ? I'm just trying to get data on the currently inspected window, and this permission enables me to do it but I don't want to require scary seeming permissions from users. – TheWebTech May 28 '19 at 21:55
  • 2
    why is that not in the docs for the tutorial? https://developer.chrome.com/extensions/getstarted – Omar May 27 '20 at 09:53
  • 1
    `*://*/*` is good, but be careful with `chrome://extensions/` – Ngoc Nam May 15 '21 at 22:33
3

While ArtPip suggestion works in this case, often you want to execute a script on a tab or all tabs and correctly handle the error if your permissions don't allow injection on that tab or some of the tabs.

Here is an example of executing a script on all tabs and correctly handling errors:

tabs.forEach(tab => {
    chrome.tabs.executeScript(tab.id, { file: filename }, result => {
        const lastErr = chrome.runtime.lastError;
        if (lastErr) console.log('tab: ' + tab.id + ' lastError: ' + JSON.stringify(lastErr));
    });
});
kofifus
  • 17,260
  • 17
  • 99
  • 173
  • allows to catch internal errors such as ERR_TOO_MANY_RETRIES ERR_PROXY_CONNECTION_FAILED ERR_EMPTY_RESPONSE ERR_CONNECTION_RESET etc. – BestDeveloper Apr 19 '20 at 23:13
0

It's super easy to inject a content script declaratively via manifest.json. The matches property determines what URLs the script can access which can also include the filesystem URL's.

    "content_scripts": [{

                         "matches": ["<all_urls>"],

                         "js": ["contentScript.js"]

                       }],

To learn more about match patterns visit https://developer.chrome.com/extensions/match_patterns

To learn more about injecting scripts https://developer.chrome.com/extensions/content_scripts#pi

arthas
  • 680
  • 1
  • 8
  • 16