5

我有这个简单的扩展,它在 chrome 的工具栏上显示图标并显示启用/禁用按钮。我想向按钮添加功能以禁用或启用content_script.js在访问谷歌网站时触发的功能:

popup.js

var setLayout = function(){
    var list = document.createElement('ul');

    var enable = document.createElement('li');
    enable.appendChild(document.createTextNode('Enable Script'));
    enable.onclick = function(){toggle(0)};

    var disable = document.createElement('li');
    disable.appendChild(document.createTextNode('Disable Script'));
    disable.onclick = function(){toggle(1)};

    list.appendChild(disable);
    document.body.appendChild(list);

    function toggle(n){
        list.removeChild( n == 0 ? enable : disable);
        list.appendChild(n == 0 ? disable : enable);
    }
};

document.addEventListener('DOMContentLoaded', setLayout, false);

清单.json

{
  "manifest_version": 2,

  "name": "test",
  "description": "test",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "content_scripts": [
    {
      "matches": ["https://www.google.co.uk/"],
      "js": ["content_scripts.js"]
    }
  ]
}

content_scripts.js

(function(){
    alert('hello');
})();

我是谷歌扩展的新手,不知道该怎么做,我想在单击禁用/启用按钮后更改清单,但在阅读谷歌网站上的文档后找不到正确的命令!

任何帮助将不胜感激。

4

2 回答 2

2

经过一些研究,我想出了如何使用backround pages,sendMessagelocalstorage.

background pagespopup.js作为和之间的沟通content_scripts.js者,它们位于两个不同的文档上,不可能直接在它们之间传递变量。

要在 mamifest 中启用背景页面,我添加了:

"background": {
"scripts": ["background.js"],
"persistent": true
},

localstorage将变量保存在本地并记住它们,即使在浏览器关闭并再次打开时也会记住它们,因此当单击启用/禁用按钮时,它设置localStorage['status'] = 1/0可以通过访问background.js并传递给content_scripts.js.

要设置我添加到的 localStorage 变量popup.js

if(!localStorage.status) localStorage['status'] = 1;
toggle(2);
enable.onclick = function(){toggle(0)};
disable.onclick = function(){toggle(1)};
function toggle(n){
    if((n == 0) && (enable.parentNode == list)){
        list.removeChild(enable);
        list.appendChild(disable);
        localStorage.status = 1;
    }else if((n == 1) && (disable.parentNode == list)){
        list.removeChild(disable);
        list.appendChild(enable);
        localStorage.status = 0;
    }else if((n == 2) && (!list.hasChildNodes())){
        list.appendChild((localStorage.status == 1) ? disable : enable);
        chrome.browserAction.setIcon({path: (localStorage.status == 1) ? "icons/icon19.png" : "icons/icon19_disabled.png"});
    }else{
        return;
    }
}

要传递localStorage.statuscontent_scripts.js我必须在加载的发送请求消息上使用哪个,以及sendMessage在哪个上侦听请求并使用该值发送响应。content_scrips.jsbackground.jsonMessagebackground.jscontent_scripts.jslocalStorage.status

背景.js:

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if (request.type == "status") sendResponse({status: localStorage.status});
});

content_scripts.js

var fn = function(){...};
chrome.runtime.sendMessage({type: "status"}, function(response) {
    if(response.status == 1) fn();
    return;
});

就是这样,希望有人觉得它有用。

于 2013-06-20T17:17:28.233 回答
1

尝试使用代码而不是清单文件注入内容脚本,如下所示:

chrome.tabs.executeScript(null, {file: "content_script.js"});

然后,您可以使用后台页面和内容脚本之间的消息传递来决定是否注入内容脚本,或者可能是内容脚本本身中的 if 语句,该语句仅在扩展程序的操作按钮设置标志时运行。

您可以使用浏览器操作事件来检测何时按下操作按钮。

于 2013-06-19T07:04:15.170 回答