Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blocking request in Chrome

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,
like image 386
Berneck Avatar asked Aug 10 '13 03:08

Berneck


People also ask

What is blocking request?

It means, when you make a request to the server, you wait until you hear back from it (blocking). The advantage of this approach is that code that expects the request to complete will be ensured that the request has completed.

How do I unblock a request on Chrome?

To unblock web push from a specific website in the desktop version: Open a website in Chrome. Click on the information icon to the left of the URL; Next to “Notifications”, select either “Ask” or “Allow”.


2 Answers

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.

like image 164
方 觉 Avatar answered Sep 19 '22 11:09

方 觉


From Chrome 59 you can block specific requests from Network tab of developer tools itself.

https://developers.google.com/web/updates/2017/04/devtools-release-notes#block-requests

Right-click on the request in the Network panel and select Block Request URL. A new Request blocking tab pops up in the Drawer, which lets you manage blocked requests.

Sample

like image 28
Asim K T Avatar answered Sep 20 '22 11:09

Asim K T