Chrome extension injecting script get error

匿名 (未验证) 提交于 2019-12-03 02:16:02

问题:

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.

回答1:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!