How to disable webRequest in chrome mode

I am resubmitting my question from the google chromium-extensions group.

In my extension, I want to undo some webRequests based on the url template. My problem is that if I return to {cancel: true} in the onBeforeRequest event listener, the browser redirects to a page telling me that the request is blocked by some extension, But I just want to cancel the request silently (since nothing happened).

I also tried returning {redirectUrl: "} to the onBeforeRequest event listener, the console would log an error indicating that" "is not a valid URL, and a panel appeared at the bottom of the browser that said: Waiting for extension . To reject this bar , I then run the contents of the script " window.stop () " on this web page. This works sometimes, but not always. Therefore, I am wondering if anyone has a better solution. Thanks !!

+7
javascript google-chrome google-chrome-extension
source share
3 answers

Instead, you should use the javascript: URL:

{ redirectUrl:"javascript:" } 
+15
source share

Redirecting to a page that responds with HTTP status code 204, for example. https://robwu.nl/204 This is my site, and I do not register traffic to this URL.

The specification requires the following behavior for a response with an HTTP status of 204:

If the client is a user agent, he SHOULD NOT change the look of his document from what caused the request to be sent. This answer is primarily intended to allow entry for actions without causing a change in the presentation of the active user document, although any new or updated meta information MUST apply to the document currently in the active view of the user agent.

Here's a simple example - an extension that silently blocks all requests on YouTube:

 chrome.webRequest.onBeforeRequest.addListener(function(details) { var scheme = /^https/.test(details.url) ? 'https' : 'http'; return { redirectUrl: scheme + '://robwu.nl/204' }; }, { urls: ['*://www.youtube.com/*'] // Example: Block all requests to YouTube }, ['blocking']); 

This example redirects to http://robwu.nl/204 or https://robwu.nl/204 depending on the request scheme in order to avoid mixed content warnings.

For this example to work, you need to declare the permissions of webRequest, webRequestBlocking, and the host for the site in the manifest file.

+6
source share
 return {redirectUrl: 'javascript:void(0)'}; 
+3
source share

All Articles