Chrome lock request

I am trying to block some requests in a Chrome app.

I created a JavaScript listener that does this check:

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

But requests are not blocked. Am I missing something in this code?

My manifest:

 "background": { "scripts": ["listener.js"], "persistent": true }, "permissions": ["tabs", "http://*/*"], "manifest_version": 2, 
+10
google-chrome google-chrome-extension
source share
4 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' (allowed only for certain events), the callback function is processed synchronously. This means that the request is blocked until the callback function returns. In this case, the callback may return a BlockingResponse, which determines the further life cycle of the request.

To block the request (cancel it), return {cancel: true} to the 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.

+19
source share

In Chrome 59, you can block certain requests from the Developer tab’s Network tab.

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

Right-click the request in the Network panel and select Block URL Request. A new request blocking tab appears in the box, which allows you to manage blocked requests.

Sample

+6
source share

You can do the following:

  • Create a new directory.
  • Create the files below.
  • Download the unpacked extension in developer mode with chrome://extensions/

background.js

 chrome.webRequest.onBeforeRequest.addListener( function(){ return {cancel: true}; }, { urls: ["<all_urls>"], // Change this to a more specific pattern types: ["script"] }, ["blocking"] ); 

manifest.json

 { "name": "Block request", "version": "1.0", "manifest_version": 2, "background": { "scripts": ["background.html"] }, "permissions": [ "webRequest", "webRequestBlocking", "<all_urls>" ] } 
+4
source share

You can use the already built Google Chrome extension,

In my case, I also wanted to close the unwanted tabs:

To block requests there is http-request-blocker (the github project is here ), you just need to configure unwanted requests:

enter image description here

To automatically close tabs, I use the ad-close-gold extension, it works in much the same way.

0
source share

All Articles