Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.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 使我的浏览器操作充当开/关开关(带有适当的图标)_Javascript_Google Chrome_Google Chrome Extension - Fatal编程技术网

Javascript 使我的浏览器操作充当开/关开关(带有适当的图标)

Javascript 使我的浏览器操作充当开/关开关(带有适当的图标),javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,我正在开发一个Google Chrome扩展,我基本上希望浏览器的动作能起到开关的作用。无论何时“打开”,它都会有一个特定的图标,脚本会在页面上执行。每当它处于“关闭”状态时,它都有一个特定的图标,并且脚本不会执行 这是我的manifest.json: { "manifest_version": 2, "name": "Resource Control", "description": "Controls what resources load from a websit

我正在开发一个Google Chrome扩展,我基本上希望浏览器的动作能起到开关的作用。无论何时“打开”,它都会有一个特定的图标,脚本会在页面上执行。每当它处于“关闭”状态时,它都有一个特定的图标,并且脚本不会执行

这是我的
manifest.json

{
    "manifest_version": 2,
    "name": "Resource Control",
    "description": "Controls what resources load from a website.",
    "version": "1.0",
    "icons": {
        "16": "locked_16.png",
        "48": "locked_48.png",
        "128": "locked_128.png"
    },
    "browser_action": {
        "default_icon": "locked_16_off.png" //icon is "off" by default
    },
    "background": {
        "scripts": ["main.js"] 
    }, 
    "permissions": [
    "tabs", "http://*/*", "https://*/*"
    ]
}
下面是
main.js

var toggled = false;

chrome.browserAction.onClicked.addListener(function(tab) {
  toggled = !toggled;
  if(toggled){    
    chrome.browserAction.setIcon({path: "locked_16_on.png", tabId:tab.id});
    chrome.tabs.executeScript(null,{code:"document.body.style.backgroundColor='red'"});
  }

  else{
    chrome.browserAction.setIcon({path: "locked_16_off.png", tabId:tab.id});
    chrome.tabs.executeScript(tab.id, {code:"alert()"});
  }

});

if (toggled) {
    chrome.tabs.executeScript(null,{code:"document.body.style.backgroundColor='red'"});
}

现在,我只是在继续之前让这个功能正常工作,所以为了测试,我只是将页面背景设置为红色。但是,每当我加载一个新网页时,图标都会恢复为“关闭”图标,即使扩展仍处于“打开”状态,脚本也不会插入到新页面中。有人能帮我找出哪里出了问题吗?

当您将
选项卡ID
传递到
chrome.browserAction.setIcon
时,它只设置特定选项卡处于活动状态时的图标。如果切换到其他选项卡,图标将恢复为默认值


当点击
browserAction
时,您只能运行
chrome.tabs.executeScript
。如果希望在每个页面上注入代码,您需要使用在所有选项卡上注入的内容脚本,或者每次都要侦听新选项卡的创建和执行情况。

谢谢,我现在可以在多个选项卡上使用它。我还通过使用
chrome.tabs.onUpdate.addListener
在每个页面上注入代码,然后在该方法的参数函数中执行脚本。但是,当我想关闭扩展时,有没有办法删除该侦听器?没关系,我知道了。再次感谢您的回答!:)