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

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

问题描述

我正在尝试阻止 Chrome 应用中的某些请求.

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

chrome.webRequest.onBeforeRequest.addListener({网址:["*://site.com/test/*"]},[阻止"]);

但是请求并没有阻塞.我在这段代码中遗漏了什么吗?

我的清单:

"background": {脚本":[listener.js"],持久":真实},"权限": ["tabs", "http://*/*"],清单版本":2,

解决方案

您似乎误解了此处阻塞"的含义.

https://developer.chrome.com/extensions/webRequest.html#subscription><块引用>

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

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

例如:

chrome.webRequest.onBeforeRequest.addListener(功能() {返回{取消:真};},{网址:["*://site.com/test/*"]},[阻止"]);

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

还要记住在清单中声明 webRequestwebRequestBlocking 权限.

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天全站免登陆