Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用chrome扩展调试器会中断应用程序_Javascript_Google Chrome_Google Chrome Extension - Fatal编程技术网

Javascript 使用chrome扩展调试器会中断应用程序

Javascript 使用chrome扩展调试器会中断应用程序,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,当我通过单击扩展图标运行下面的代码时,我会看到Mainscript.js中的“Mainscript”警报 当我使用Dev Tools调试器并中断时: chrome.tabs.executeScript(null, {file: "mainscript.js"}); in background.js 然后单击“继续”,我将收到错误消息: Unchecked runtime.lastError while running tabs.executeScript: Cannot access cont

当我通过单击扩展图标运行下面的代码时,我会看到Mainscript.js中的“Mainscript”警报

当我使用Dev Tools调试器并中断时:

chrome.tabs.executeScript(null, {file: "mainscript.js"}); in background.js
然后单击“继续”,我将收到错误消息:

Unchecked runtime.lastError while running tabs.executeScript: Cannot access contents of the page. Extension manifest must request permission to access the respective host.
我想这是因为我现在正在调试器窗口的上下文中运行executeScript

如何停止导致错误的调试器

舱单:

{
   "name": "Enable/Disable",
   "version": "1.0",
   "manifest_version": 2,
   "description": "Enables/Disables",

   "permissions": 
   [
     "http://xxx.xxx.xxx.xxx/*",
     "activeTab"
   ],

   "browser_action": 
   {
      "default_icon": 
      {
         "16": "images/Icon_ENABLED.png"
      }
   },

  "background": 
  {
     "scripts": ["background.js"]
  }
}
background.js:

chrome.browserAction.onClicked.addListener
(
   function(tab) 
   {
      chrome.tabs.executeScript(null, {file: "mainscript.js"});
   }
);
mainscript.js

alert('Mainscript');

指定
tab.id
而不是
null
。我再一次欠你的债。为什么调用脚本会关心选项卡呢?我的代码处于浏览器级别,而不是浏览器级别。代码在后台页面中执行。在弹出窗口内执行它将在浏览器级别内工作,因为弹出窗口“绑定”到其打开时的特定页面,而背景页面没有默认选项卡。