Blocking request in Chrome

前端 未结 2 1609
無奈伤痛
無奈伤痛 2020-12-05 20:09

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

I created a JavaScript listener that does this validation:



        
2条回答
  •  一向
    一向 (楼主)
    2020-12-05 20:20

    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.

提交回复
热议问题