基于chrome.storage中的数据返回chrome.webRequest.onBeforeRequest的值 [英] return value of chrome.webRequest.onBeforeRequest based on data in chrome.storage

查看:101
本文介绍了基于chrome.storage中的数据返回chrome.webRequest.onBeforeRequest的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据存储在chrome.storage.local中的数据在我的Google Chrome扩展程序中阻止某些webrequests。
但是我找不到一种方法来返回{cancel:true};在onBeforeRequest.addListener的回调函数中。或者由于chrome.Storage.local.get()的异步方式而从其storage.local的各自回调函数外部访问数据。



这是我的相关代码。

  chrome.webRequest.onBeforeRequest.addListener(函数(信息){

chrome.storage。 local.get({requests:[]},function(result){

//取决于result.requests的值。[0] .item我想返回{cancel:true} ;为了阻止webrequest
if(result.requests。[0] .item == 0)return {cancel:true}; //然而这显然在错误的地方

});

//如果我在这里放置return {cancel:true},它应该是,我无法再访问storage.local.get的数据
/ / if(result.requests。[0] .item == 0)return {cancel:true};

});

有没有人可以解决这个问题?感谢您的帮助。

解决方案

您可以交换回调:

  chrome.storage.local.get({requests:[]},function(cache){
chrome.webRequest.onBeforeRequest.addListener(function(request){
if(cache.requests [0] .item === 0)
return {cancel:true};
});
});

这是有道理的,因为不是每个请求都要求存储,而是只有在拥有存储在内存中。




这种方法唯一的缺点是,如果您在开始监听后更新存储空间,它不会生效。



为了解决这个问题,删除侦听器并重新添加它:

  var currentCallback; 

function startListening(){
chrome.storage.local.get({requests:[]},function(cache){
chrome.webRequest.onBeforeRequest.addListener(function (request){
currentCallback = this;

if(cache.requests [0] .item === 0)
return {cancel:true};
});
});


函数update(){
if(typeof currentCallback ===function){
chrome.webRequest.onBeforeRequest.removeListener(currentCallback);
currentCallback = null;
}

startListening();
}


I am trying to to block certain webrequests in my google chrome extension based on data stored in chrome.storage.local. However I can't find a way to return "{cancel: true };" inside the callback function of onBeforeRequest.addListener. Or to access data from storage.local outside of it's respective callback function due to the asynchronous way of chrome.Storage.local.get().

Here is my relevant code.

chrome.webRequest.onBeforeRequest.addListener( function(info) {

    chrome.storage.local.get({requests: []}, function (result) {

        // depending on the value of result.requests.[0].item I want to return "{cancel:  true };" in order to block the webrequest
        if(result.requests.[0].item == 0) return {cancel: true}; // however this is obviously in the wrong place

    });

    // if I put return {cancel: true} here, where it should be, I can't access the data of storage.local.get anymore
    // if(result.requests.[0].item == 0) return {cancel: true};

});

Has anyone a solution for this problem? Thanks for your help.

解决方案

You can just swap the callbacks:

chrome.storage.local.get({requests: []}, function (cache) {
    chrome.webRequest.onBeforeRequest.addListener(function (request) {
        if(cache.requests[0].item === 0)
            return { cancel: true };
    });
});

This makes sense because instead of requesting storage on each request, you only listen to requests after you have the storage in the memory.


The only downside to this method is that if you are updating the storage after starting listening, it won't take effect.

To solve this, remove the listener and add it again:

var currentCallback;

function startListening() {
    chrome.storage.local.get({requests: []}, function (cache) {
        chrome.webRequest.onBeforeRequest.addListener(function (request) {
            currentCallback = this;

            if(cache.requests[0].item === 0)
                return { cancel: true };
        });
    });
}

function update() {
    if (typeof currentCallback === "function") {
        chrome.webRequest.onBeforeRequest.removeListener(currentCallback);
        currentCallback = null;
    }

    startListening();
}

这篇关于基于chrome.storage中的数据返回chrome.webRequest.onBeforeRequest的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆