0

I am trying to get localhost permission in my manifest.json so I can use chrome.tabs.executeScript but it is not working. I have tried nearly everything you can think of. Here is an example:

{
    "manifest_version": 2,
    "name": "extName",
    "version": "1",
    "description": "extDesc",
    "content_scripts": [
        {
            "matches": [
                "http://localhost:3000/*",
                "http://127.0.0.1/*",
                "http://*/*",
                "https://*/*"
            ],
            "css": [
                "style.css"
            ],
            "js": [
                "contentScript.js"
            ]
        }
    ],
    "background": {
        "scripts": [
          "background.js"
        ]
    },
    "permissions": [
        "activeTab",
        "tabs"
    ]
}

But no matter what I try I keep getting

Error during tabs.executeScript: Cannot access contents of url .... 
Extension manifest must request permission to access this host. 

Actually, I think it's happening also not on localhost

Here is my background.js:

chrome.runtime.onMessage.addListener(
    function(message, callback) {
      if (message == "runContentScript"){
        chrome.tabs.executeScript(null, {
            code: 'console.log(window.angular)'
          });
      }
   });
itamar
  • 3,837
  • 5
  • 35
  • 60
  • 2
    You already have host permissions for all http and https URLs via content_scripts declaration. It also makes the content script automatically running on all these URLs. It means you don't need executeScript at all. The error you see is most likely due to the fact you're trying to inject into a new tab which internally is `chrome://newtab` or into an extension page tab. Also note, instead of `null` in executeScript you must specify the actual tab id that sent the message e.g. (msg, sender, sendResponse) => chrome.tabs.executeScript(sender.tab.id, ........) – wOxxOm Nov 07 '18 at 08:24
  • Ah okay - so what do I use instead of executeScript? I need to access the window object in the active tab. – itamar Nov 07 '18 at 08:27
  • Like I said you already have a content script running. The content script can access the window object. However, if `window.angular` is defined by a web page script, not your content script, you need a different approach: [example](https://stackoverflow.com/a/46870005). – wOxxOm Nov 07 '18 at 08:29
  • Your example was great - I upvoted it there. – itamar Nov 13 '18 at 06:28

0 Answers0