0

I'm working on building chrome extension. I have an error - Unchecked runtime.lastError while running tabs.executeScript: Cannot access a chrome:// URL at Object.callback (). I don't know what to do.

manifest.json

{
    "manifest_version": 2,

    "name": "tabs",
    "description": "testing tabs",
    "version": "1.0.4",
    "icons": { "128": "icon_128.png" },
    "browser_action": {
      "default_icon": "icon.png",
      "default_title": "A popup will come here"
    },
    "background": {
      "scripts": ["background.js"]
    },
     "permissions": ["tabs", "*://*/*", "background"]
  }

background.js

chrome.tabs.query({}, function(tabs) {
        for(var i= 0; i < tabs.length; i++) { 
            chrome.tabs.executeScript(null, {
            code: "console.log('wow');"
        });
    }

});

Thanks in advance.

user9671162
  • 9
  • 1
  • 3
  • 1
    You can't inject scripts in chrome:// tabs. Check `tabs[i].url` first. – wOxxOm Apr 19 '18 at 17:29
  • You mean manifest file is wrong? or background.js file is wrong? Could you give me some detail how to fix? – user9671162 Apr 19 '18 at 17:43
  • 1
    No, I mean you can't do it. – wOxxOm Apr 19 '18 at 17:44
  • Can you specify what I can't? – user9671162 Apr 19 '18 at 17:53
  • 1
    You can't inject scripts in chrome:// tabs. – wOxxOm Apr 19 '18 at 17:53
  • You mean chrome.tabs.executeScript is wrong? – user9671162 Apr 19 '18 at 18:04
  • 1
    I don't know how else to explain. See [Can you access chrome:// pages from an extension?](//stackoverflow.com/q/19042857) – wOxxOm Apr 19 '18 at 18:07
  • 2
    Possible duplicate of [Can you access chrome:// pages from an extension?](https://stackoverflow.com/questions/19042857/can-you-access-chrome-pages-from-an-extension) – wOxxOm Apr 19 '18 at 18:08
  • I don't understand why you can't explain? If you know where I am wrong, you can fix the code. please tell me how to fix it. – user9671162 Apr 19 '18 at 18:19
  • 1
    Well, I thought I've explained in the first comment. You can check `tabs[i].url` - and don't inject the script if it starts with `chrome`. Or you can hide the error by simply accessing chrome.runtime.lastError in the executeScript's callback ([example1](https://stackoverflow.com/a/45690180), [example2](https://stackoverflow.com/a/45603880)). – wOxxOm Apr 19 '18 at 18:44
  • I already tried example2, and I still have the same error message -Cannot access a chrome:// URL. – user9671162 Apr 19 '18 at 19:06
  • When you edit the code of the background script you need to reload the extension. – wOxxOm Apr 19 '18 at 19:18
  • Of course, I did. But I still have the same error message. – user9671162 Apr 19 '18 at 19:28
  • If you see this error, the only reason is that you either didn't write the callback correctly as shown in example2 or the extension wasn't reloaded. The latter might be a bug in the browser so try restarting it. – wOxxOm Apr 19 '18 at 19:31
  • BTW using `null` in executeScript makes no sense because it'll inject in the active tab. You probably want to replace it with `tabs[i].id`. – wOxxOm Apr 19 '18 at 19:43
  • I tried example1 and example2 both. I changed to tabs[i].id And I closed browser, then restarted. I still have the same error. – user9671162 Apr 19 '18 at 19:44
  • Well, like I said, there are no other explanations. Without seeing your current code I can't help anyway. – wOxxOm Apr 19 '18 at 19:46
  • Thanks for trying to help. Do you think if I publish the extension, it will work? – user9671162 Apr 19 '18 at 19:48
  • I think this link is correct answer. https://stackoverflow.com/questions/24600495/chrome-tabs-executescript-cannot-access-a-chrome-url – user9671162 Apr 20 '18 at 13:16

0 Answers0