切换chrome扩展以执行脚本不起作用

时间:2019-03-17 09:03:03

标签: javascript

manifest.json文件的内容

{ 
   "name": "da-editor",
   "version": "1.0",
   "description": "edit any webpage anytime",
   "manifest_version": 2,

   "browser_action": 
    {
      "default_icon":"off.png",
      "default_title": "icon"   
    },
      "icons":
     {
       "32":"off.png",
       "32":"on.png"
     },
     "background": {
      "scripts":["background.js"]
     },

     "content_scripts": 
     [ {
         "matches": ["<all_urls>"],
          "js":["content.js","stop.js"]
        }  
     ]  

        }

background.js的内容

 var toggle = false;
  chrome.browserAction.onClicked.addListener(function(tab) {
    toggle = !toggle;
     if(toggle){
      chrome.browserAction.setIcon({path: "on.png", tabId:tab.id});
      chrome.tabs.executeScript(tab.id, {file:"content.js"});
      }
     else{
      chrome.browserAction.setIcon({path: "off.png", tabId:tab.id});
      chrome.tabs.executeScript(tab.id, {file:"stop.js"});
      }
    });

content.js的内容

document.body.contentEditable = true

stop.js的内容

 document.body.contentEditable = false

起初,我只在content_script中放了一个js文件,它是content.js,然后它正在执行,但没有停止。现在图标会切换,但脚本无法执行

0 个答案:

没有答案