Conditionally register blocking webrequest

107 views
Skip to first unread message

Addon Land

unread,
May 23, 2024, 4:57:14 PMMay 23
to chromium-...@chromium.org
In MV3 I want to use webRequestBlocking when it is available, but if not then I want to just use regular webRequest.

I could use chrome.permissions.contains to check for webRequestBlocking, then register the listener as blocking or not depending on the asynchronous result of the chrome.permissions.contains call.  But that won't register the listener at the first run so as I understand it the first event that awoke the SW might be missed?

The only solution I could think of was to make two calls to webRequest.onBeforeRequest.addListener, one requesting blocking, the other not requesting blocking.  Then call chrome.permissions.contains and if the blocking permission is available, remove the non-blocking listener.

Then I have to deal with both listeners potentially being called until I get the response from chrome.permissions.contains.

Am I missing something?  Is there an easier approach?






woxxom

unread,
May 23, 2024, 6:24:46 PMMay 23
to Chromium Extensions, Addon Land
Looks like you can simply register both, first the blocking one, which will unregister the non-blocking one, kind of like "event.stopPropagation()". BTW, just in case, you can enable webRequestBlocking for testing by restarting chrome with --allowlisted-extension-id=blablabla command line.

let blocking; // not necessary, added to show you can use it in other places later
chrome.webRequest.onBeforeRequest.addListener(onBeforeRequestBlocking,
  {urls: ['<all_urls>']}, ['blocking']);
chrome.webRequest.onBeforeRequest.addListener(onBeforeRequest,
  {urls: ['<all_urls>']});
function onBeforeRequestBlocking(info) {
  blocking = true;
  chrome.webRequest.onBeforeRequest.removeListener(onBeforeRequest);
  // ...............
}
function onBeforeRequest(info) {
  chrome.webRequest.onBeforeRequest.removeListener(onBeforeRequestBlocking); // might be superfluous, just in case
  // ...............
}

Addon Land

unread,
May 23, 2024, 7:02:24 PMMay 23
to woxxom, Chromium Extensions
That's a good approach.  The only issue I see is that it will result in a chrome.runtime.lastError message in the console when addListener fails for the blocking one.

I suppose it's guaranteed that the non-blocking one won't be called after removeListener is called, even if the event is already queued to hit the non-blocking callback.  But to be safe, I could also gate the removal of onBeforeRequestQuestBlocking by checking the blocking variable.

let blocking; // not necessary, added to show you can use it in other places later
chrome.webRequest.onBeforeRequest.addListener(onBeforeRequestBlocking,
  {urls: ['<all_urls>']}, ['blocking']);
chrome.webRequest.onBeforeRequest.addListener(onBeforeRequest,
  {urls: ['<all_urls>']});
function onBeforeRequestBlocking(info) {
  blocking = true;
  chrome.webRequest.onBeforeRequest.removeListener(onBeforeRequest);
  // ...............
}
function onBeforeRequest(info) {
if (!blocking)
Reply all
Reply to author
Forward
0 new messages