1

Background page:

chrome.tabs.create({ url: 'http://google.com' }, tab => {
  chrome.tabs.executeScript({ code: '2+2;' }, (r) => {
    console.log(`url: ${tab.url}, result: ${r[0]}`);
  });
});

I open background page to see output:

url: http://google.com/, result: 4

Looks good, but now I press F5 or Ctrl+F5:

Unchecked runtime.lastError while running tabs.executeScript: Cannot access contents of url "chrome-devtools://devtools/bundled/inspector.html?&remoteBase=https://chrom…om/serve_file/@e8926f681fbb840b4f389e7e692343d4505722ce/&dockSide=undocked". Extension manifest must request permission to access this host.
at Object.callback (chrome-extension://laaoiaaacchfpefjhklpmnfjbeamjfli/background.js:2:15)

In 'manifest.json' I have <all_urls> permission.

Dizzy
  • 892
  • 3
  • 12
  • 24

1 Answers1

4

When the first parameter, tabId, of chrome.tabs.executeScript is omitted the code is injected into the active tab of the active window. In your case the active window is the devtools debugger of the background page and it doesn't allow injection of code.

Specify tabId explicitly: chrome.tabs.executeScript(tab.id, { code: .......

wOxxOm
  • 65,848
  • 11
  • 132
  • 136