开关图标chrome扩展名不起作用

时间:2018-11-10 13:01:53

标签: javascript jquery google-chrome-extension firefox-addon manifest

当我单击更改为打开和关闭的扩展名图标时,脚本将起作用,如果关闭,则该脚本将起作用!我尝试了大约2个小时,但仍未找到解决方案,因此请有人可以帮助我解决此问题,非常感谢。

manifest.json

    "name":"title",
"description":"des",
"version":"1.1.1",
"manifest_version":2,
"icons": { 
    "16": "icons/icon16.png",
    "48": "icons/icon48.png",
    "128": "icons/icon128.png"
},
"browser_action": {
    "default_title": "On/Off",
    "default_icon": "icons/off.png"

},
"background": {
    "scripts": ["js/background.js"],
    "persistent": true
},
"content_scripts": [
    {
    "matches": ["<all_urls>"],   
    "js": ["js/content.js", "js/jquery.min.js"],
    "css": ["css/style.css"],
    "run_at": "document_start",
    "all_frames": true
    }
],
"applications": {
"gecko": {
    "id": "{3b5e61ae-bda1-46c5-89bb-5085cc5e9bc}",
    "strict_min_version": "57.0"
        }
    }, 
"content_security_policy": "script-src 'self'",
"permissions": [ 
    "<all_urls>",
    "webRequestBlocking",
    "webNavigation",
    "activeTab",
    "storage"
  ]
}

background.js

document.addEventListener('DOMContentLoaded', function() {
var enable=false;
chrome.browserAction.onClicked.addListener(function (tab) {
 enable = enable ? false : true;
 if(enable){
  //turn on...
  chrome.browserAction.setIcon({ path: 'icons/on.png' });
  chrome.browserAction.setBadgeText({ text: 'ON' });
  chrome.tabs.executeScript(null, { file: 'content.js' }); 
 }else{
  //turn off...
  chrome.browserAction.setIcon({ path: 'icons/off.png'});
  chrome.browserAction.setBadgeText({ text: 'Off' });
      }
   });
});

0 个答案:

没有答案