4

I have a Chrome Extension that uses chrome.webRequest.onBeforeRequest.addListener to load patched scripts/ redirect loading of some scripts to a different site. chrome.webRequest ideally suits me because it helps me to load "patched" script instead of original one, not on top of it.

chrome.webRequest.onBeforeRequest.addListener(function(details){
  var originalUrl = details.url;
  console.log('originalUrl: ' + originalUrl);

  blockingResponse = {};

  if (originalUrl fits my condition) {
      blockingResponse.redirectUrl = originalUrl.performSomeLogicToGetNewUrl();
      console.log('redirectUrl: ' + blockingResponse.redirectUrl);
  }

  return blockingResponse;
},
{urls: [ "<all_urls>" ]},['requestBody','blocking']);

I want to move to a solution that would be suitable for more than one browser and researching if this can be achieved with greasemonkey/tampermonkey, but no luck so far. Does anyone have a similar experience?

ursus
  • 121
  • 5
  • [You can do this in Firefox/Greasemonkey](http://stackoverflow.com/a/10468821/331508), but it's not part of the `GM_` API and Chrome doesn't support `beforescriptexecute` (still), so Tampermonkey can't do it. – Brock Adams Sep 01 '14 at 21:58
  • the point is to execute before even the request is done, so you can redirect the request, change the URL etc. not to execute something after request was done but before javascript is executed – Asiel Diaz Benitez Oct 24 '21 at 02:01
  • **`GM_webRequest` is available** in some Tampermonkey versions See [issue #397 at the extenion's official repo](https://github.com/Tampermonkey/tampermonkey/issues/397). – double-beep Jan 30 '23 at 13:12

0 Answers0