【问题标题】:Google Chrome Extensions - Clicking an icon calls a functionGoogle Chrome 扩展程序 - 单击图标调用函数
【发布时间】:2012-05-05 09:16:43
【问题描述】:

我一直在尝试制作一个带有图标的扩展程序,单击该图标会停止(所有)选项卡的加载。

我有这个清单文件:

{
  "name": "Stop Loading",
  "version": "1.0",
  "manifest_version": 2,
  "description": "Stop loading all tabs in Chrome",
  "browser_action": {
    "default_icon": "greyclose.png"
  },
  
  "background_page": "background.html",
   "content_scripts": [ {
      "all_frames": true,
      "js": [ "kliknuto.js" ],
      "matches": [ "http://*/*", "https://*/*" ]
   } ],
   
  "permissions": [ "tabs", "http://*/*", "https://*/*" ]
}

在 background.html 我有这个代码:

chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.extension.sendRequest({reqtype: "get-settings"}, function(response) {
    window.setTimeout("window.stop();", 0);
  });
});

我不确定是否应该将 background.html 代码放入 JavaScript 文件“kliknuto.js”或其他文件中。点击 Chrome 中的扩展按钮会调用哪个函数?

【问题讨论】:

    标签: javascript google-chrome google-chrome-extension dom-events


    【解决方案1】:

    在您的代码中,background.html 正在向自身发送查询(chrome.extension.sendRequest 不会发送到内容脚本),然后,如果/当它收到回复时,后台页面会在 window.stop() >本身,而不是标签。你真正需要的是:

    background.html

    ...
    chrome.browserAction.onClicked.addListener(function(tab) {
        // get all tabs in all windows
        chrome.windows.getAll({"populate":true}, function(winArray) {
            for(var i = 0; i < winArray.length; ++i) {
                for(var j = 0; j < winArray[i].tabs.length; ++j) {
                    var t = winArray[i].tabs[j];
                    // push code to each tab
                    chrome.tabs.executeScript(t.id, {"code":"window.stop();", "allFrames":"true"});
                }
            }
        });
    });
    ...
    

    此解决方案使用executeScript 代替内容脚本。

    使用替代解决方案进行编辑:

    将内容脚本附加到每个选项卡以侦听来自背景页面的订单。

    background.html

    ...
    chrome.browserAction.onClicked.addListener(function(tab) {
        // get all tabs in all windows
        chrome.windows.getAll({"populate":true}, function(winArray) {
            for(var i = 0; i < winArray.length; ++i) {
                for(var j = 0; j < winArray[i].tabs.length; ++j) {
                    var t = winArray[i].tabs[j];
                    // push code to each tab
                    chrome.tabs.sendRequest(t.id, {"order":"stop"});
                }
            }
        });
    });
    ...
    

    kliknuto.js:

    chrome.extension.onRequest(function(request) {
        if(request.order == "stop") {
            window.stop();
        }
    });
    

    确保将"run_at":"document_start" 添加到清单中的content_script 块中,以便内容脚本尽快开始侦听。

    【讨论】:

    • 感谢apsillers的解释!
    • 顺便说一句,我的代码之前缺少括号。我已经修复了它,所以请确保您使用当前版本。
    • 我现在已经调整了扩展并将它加载到 Chrome 中。我为一堆标签重新打开了 Chrome 以开始重新加载,但扩展的按钮似乎没有做任何事情。
    • 尝试注入window.alert,看看它在页面加载期间和/或之后是否有效。我有另一个使用内容脚本的解决方案,我将很快发布。
    • 另外,我刚刚意识到:manifest_version: 2 需要分离 HTML 文件和脚本,因此您的 background.html 中不能有内联脚本。将背景页面更改为纯 JS (background.js) 或将脚本移动到另一个文件并使用 &lt;script src="..."&gt;&lt;/script&gt; 标签将其加载到 background.html
    猜你喜欢
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 2010-10-04
    • 2018-03-09
    • 2012-12-26
    • 2021-03-21
    • 2018-05-25
    相关资源
    最近更新 更多