简体   繁体   English

需要帮助来切换Google Chrome扩展程序的WebRequest监听器

[英]Need help toggling google chrome extension webRequest listener

Hey guys I'm really new to making google chrome extensions. 大家好,我真的对制作Google chrome扩展程序很陌生。 The first extension I decided to do was a simple website blocker. 我决定做的第一个扩展是一个简单的网站阻止程序。 I have found some code from somebody's answer on here that can toggle the webRequest listener based on tab.id as seen below: 我从这里的某人的答案中找到了一些代码,可以根据tab.id切换webRequest侦听器,如下所示:

chrome.browserAction.onClicked.addListener(function(tab) {
if (listeners[tab.id]) { //If the ID already exists
    chrome.webRequest.onBeforeRequest.removeListener(listeners[tab.id]);
    delete listeners[tab.id];
    chrome.browserAction.setBadgeText({
        text: 'OFF',
        tabId: tab.id
    });
} else {
    listeners[tab.id] = function(details) {
        return {cancel: true};
    };
    chrome.webRequest.onBeforeRequest.addListener(listeners[tab.id], {
        urls: user,
        types: ['main_frame', 'sub_frame'],
        tabId: tab.id
    }, ['blocking']);
    // Show indicator to show that the extension is active.
    chrome.browserAction.setBadgeText({
        text: 'ON',
        tabId: tab.id
    });
}
});

This works great for each tab, however I would like to toggle the listener for ALL tabs. 这对每个选项卡都很好,但是我想为所有选项卡切换监听器。 How I would like it to work: 我希望它如何工作:

  • Click Extension Icon 单击扩展图标
  • Blocks all URLs that are in my 'user' array no matter if I switch tabs or not 无论我是否切换标签页,都将阻止“用户”数组中的所有URL
  • Can click icon again to disable no matter if tab is different from the tab I originally enabled the extension. 无论选项卡是否与我最初启用扩展程序的选项卡不同,都可以再次单击图标以禁用。

I have tried many times to recreate this method, however I cannot remove the listen once it is enabled. 我已经尝试了很多次来重新创建此方法,但是一旦启用侦听,便无法删除它。 Because my implementation always calls an anonymous function, and from what I've read you cannot remove a listener where the call back function was anonymous. 由于我的实现始终调用匿名函数,因此,根据我的阅读,您无法删除回调函数为匿名的侦听器。

Here is some basic implementation I tried on my own: 这是我自己尝试的一些基本实现:

chrome.browserAction.onClicked.addListener(function(tab){
if(toggle===true)
    toggle = false;
else if(toggle===false)
    toggle = true;

if(toggle===true){
    chrome.webRequest.onBeforeRequest.addListener(
    function deny(request) {
        return {cancel: true}; 
    }, { urls: user, types: [] }, ["blocking"]);

    chrome.browserAction.setBadgeText({
        text: 'ON'
    });
}
else{
    chrome.webRequest.onBeforeRequest.removeListener();
    chrome.browserAction.setBadgeText({
        text: 'OFF'
    });
}

});

Basically I would like some help converting the first code segment into one that works for all tabs. 基本上,我希望获得一些帮助,将第一个代码段转换为适用于所有选项卡的代码段。 Thank you! 谢谢! :) :)

You've successfully identified the problem: you cannot remove a listener if the callback function is anonymous. 您已经成功确定了问题:如果回调函数是匿名的,则无法删除侦听器。 Great! 大! This is because you need to pass the exact same object to addListener and removeListener . 这是因为您需要将完全相同的对象传递给addListenerremoveListener

All you have to do, then, is instead of using an anonymous function, keep that function somewhere you can access it. 然后,您要做的就是不要使用匿名函数,而是将该函数放在可以访问它的地方。 For example, you could define the function deny outside of the onClicked listener, then pass deny to both onBeforeRequest.addListener and onBeforeRequest.removeListener . 例如,您可以在onClicked侦听器之外定义函数deny ,然后将deny传递给onBeforeRequest.addListeneronBeforeRequest.removeListener

(A note about your code: you can flip the value of toggle with toggle = !toggle , and test if toggle is true by using if (toggle) rather than if (toggle === true) . These are just common idioms.) (关于您的代码的注释:您可以使用toggle = !toggle翻转toggle的值,并使用if (toggle)而不是if (toggle === true)来测试toggle是否为true。这只是常见的习惯用法。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM