在Chrome中阻止请求 [英] Blocking request in Chrome

查看:1426
本文介绍了在Chrome中阻止请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在Chrome应用中阻止一些请求。



我创建了一个可以进行此验证的JavaScript侦听器:

  chrome.webRequest.onBeforeRequest.addListener(
{
url:[*://site.com / test / *]
},
[blocking]
);

但请求没有被阻止。我在这段代码中错过了什么吗?

我的清单:

 背景:{
scripts:[listener.js],
persistent:true
},
permissions:[tabs,http :// * / *],
manifest_version:2,


解决方案

看起来你在这里误解了阻塞的含义。



https://developer.chrome.com/extensions/webRequest.html#subscription


如果可选opt_extraInfoSpec数组包含字符串'blocking'
(只允许特定事件),则回调函数将同步处理
。这意味着请求被阻塞,直到
回调函数返回。在这种情况下,回调可以返回一个
BlockingResponse,它决定了
请求的进一步生命周期。


要阻止请求(取消它),请在事件处理程序中返回 {cancel:true}



例如:
$ b

  chrome.webRequest.onBeforeRequest.addListener(
function(){
return {cancel:true};
},
{
url:[*://site.com/test/*]
},
[blocking]
);

这将阻止匹配 *://site.com/test的所有网址/ *



还要记住声明 webRequest webRequestBlocking 清单中的权限。


I'm trying to block some requests in a Chrome app.

I created a JavaScript listener that does this validation:

chrome.webRequest.onBeforeRequest.addListener(
    {
        urls: ["*://site.com/test/*"]
    },
    ["blocking"]
);

But the requests are not blocking. Did I miss something in this code?

My manifest:

"background": {
        "scripts": ["listener.js"],
        "persistent": true
    },
"permissions": ["tabs", "http://*/*"],
    "manifest_version": 2,

解决方案

It looks like you misunderstood the meaning of "blocking" here.

https://developer.chrome.com/extensions/webRequest.html#subscription

If the optional opt_extraInfoSpec array contains the string 'blocking' (only allowed for specific events), the callback function is handled synchronously. That means that the request is blocked until the callback function returns. In this case, the callback can return a BlockingResponse that determines the further life cycle of the request.

To block a request (cancel it), return {cancel: true} in your event handler.

For example:

chrome.webRequest.onBeforeRequest.addListener(
    function() {
        return {cancel: true};
    },
    {
        urls: ["*://site.com/test/*"]
    },
    ["blocking"]
);

This will block all URLs matching *://site.com/test/*.

Also remember to declare both webRequest and webRequestBlocking permissions in your manifest.

这篇关于在Chrome中阻止请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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