chrome.webRequest redirectUrl,URL保存在chrome.storage.local中 [英] chrome.webRequest redirectUrl with URL Saved in chrome.storage.local

查看:382
本文介绍了chrome.webRequest redirectUrl,URL保存在chrome.storage.local中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图拦截网络请求并将其重定向到我保存在本地存储中的网址,但它不起作用。我的代码如下:

  chrome.webRequest.onBeforeRequest.addListener(
function(details){
if(details.url ==='http://myapp.com/theurl'){
chrome.storage.local.get(http://myapp.com/theurl,function(result) {
return {redirectUrl:result.savedUrl}; // savedUrl属性是修改的Url
});
}
},{urls:[< all_urls> ]},[blocking]);

对return语句/ url进行硬编码:

  chrome.webRequest.onBeforeRequest.addListener(
函数(详情){
if(details.url ==='http://myapp.com/theurl '){
return {returnUrl:'http://myapp.com/modifiedurl'};
});
}
},{url:[< all_urls>]},[blocking]);


解决方案

chrome.storage api是异步,因此您在此处采用的方法将无法使用。您拥有的代码发生的基本运行是:


  1. 在webrequest之前,检查url是否为 http://myapp.com/theurl

  2. 如果是,则对 chrome.storage进行异步调用。 local.get

  3. 您的 chrome.storage.local.get 调用中的代码(即。返回语句)不会立即执行,并会在稍后运行

  4. 其余代码继续运行,并且没有任何内容从您的webRequest侦听器返回

有几种方法可以使这项工作成功。最简单的方法是在加载扩展时将本地存储的值存储在某个变量中(称之为 storage_cache )。然后从webRequest监听器中,您可以说 return {redirectUrl:storage_cache.savedUrl};



经常会有比这更好的方法......但要记住,无论您选择何种实现,都不要混淆同步事件和异步事件。


I'm trying to intercept web requests and redirect them to a url I have saved on local storage but it isn't working. My code is as follows:

chrome.webRequest.onBeforeRequest.addListener(
    function (details) {
        if (details.url === 'http://myapp.com/theurl') {
            chrome.storage.local.get("http://myapp.com/theurl", function (result) {
                return { redirectUrl: result.savedUrl }; //savedUrl property is the modified Url
            });
        }
    }, { urls: ["<all_urls>"] }, ["blocking"]);

Hardcoding the return statement/url works:

chrome.webRequest.onBeforeRequest.addListener(
    function (details) {
        if (details.url === 'http://myapp.com/theurl') {
                return { returnUrl : 'http://myapp.com/modifiedurl' };
            });
        }
    }, { urls: ["<all_urls>"] }, ["blocking"]);

解决方案

The chrome.storage api is asynchronous, so the approach that you are taking here will not work. The basic run-through of what is happening with the code you have is:

  1. Before the webrequest, check if the url is http://myapp.com/theurl
  2. If it is, then make an asynchronous call to chrome.storage.local.get
  3. The code inside your chrome.storage.local.get call (ie. the return statement) is not executed immediately and will be run at some later time
  4. The rest of your code continues to run and nothing is returned from your webRequest listener

There are a couple of ways that you can make this work. The easiest would be to store the values of the local storage inside some variable when your extension is loaded (call it, say, storage_cache). Then from the webRequest listener you could say return { redirectUrl: storage_cache.savedUrl };

If your storage will change frequently then there are better approaches than this one...but the key thing to remember for whatever implementation your choose is to not to mix up synchronous and asynchronous events.

这篇关于chrome.webRequest redirectUrl,URL保存在chrome.storage.local中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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