2

On injecting the script by using command

chrome.tabs.executeScript(
  null, {file: "dialog.js"});

throwing error

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

manifiest.json
{
  "key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcBHwzDvyBQ6bDppkIs9MP4ksKqCMyXQ/A52JivHZKh4YO/9vJsT3oaYhSpDCE9RPocOEQvwsHsFReW2nUEc6OLLyoCFFxIb7KkLGsmfakkut/fFdNJYh0xOTbSN8YvLWcqph09XAY2Y/f0AL7vfO1cuCqtkMt8hFrBGWxDdf9CQIDAQAB",
  "name": "TerminusProLink",
  "version": "1.0",
  "manifest_version": 2,
  "description": "Link to ProLaw App",
  "background": {
  "scripts": [ "background.js", "background.html"]
  },
  "content_scripts": [
    {
      "all_frames": true,
      "js": [ "jquery-1.5.1.js", "jquery-ui-1.8.11.js", "content.js" ],
      "matches": [ "http://*/*", "https://*/*" ]
    }
  ],

  "permissions": [
    "background", "tabs", "http://*/*", "https://*/*",

  ]
}

Any one having solution please suggest.

Vishwajeet Bose
  • 430
  • 3
  • 12

1 Answers1

2

Please explicitly set the tabId parameter of executeScript, which by default would be the active tab of the current window.

If you couldn't get tabId directly, use chrome.tabs.query to query tab state.

chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
    for(var i = 0; i<tabs.length;i++) {
        chrome.tabs.executeScript(tabs[i].id, {"file": "dialog.js"});
    }
});

And don't forget add "web_accessible_resources": ["dialog.js"] in your manifest.json

Haibara Ai
  • 10,703
  • 2
  • 31
  • 47
  • Thanks, it is working. But need to pass message to dialog.js. How to pass message to the dialog.js method? – Vishwajeet Bose Apr 21 '16 at 07:32
  • @VishwajeetBose, if you mean passing parameters when calling `executeScript`, take a look at http://stackoverflow.com/questions/17567624/pass-parameter-using-executescript-chrome – Haibara Ai Apr 21 '16 at 07:35
  • After use that not even execute dialog.js code. chrome.tabs.executeScript(tabs[i].id, { code: "var msg=1;" }, function () { chrome.tabs.executeScript(tabs[i].id, { "file": "dialog.js" }); }); } In dialog.js function callCheck() { debugger; var DialogBox = document.createElement("div"); var TextPara = document.createElement("p"); TextPara.innerHTML = "ss"; DialogBox.appendChild(TextPara); $(DialogBox).dialog({ }); } alert(msg); callCheck(); – Vishwajeet Bose Apr 21 '16 at 08:44
  • @VishwajeetBose, please be aware when inner `executeScript` is executed, `i` has been equal with `tabs.length` and obviously `tabs[i]` is undefined. You could define a local variable like `var tabId = tabs[i].id`, then use it in `executeScript`. – Haibara Ai Apr 21 '16 at 09:01