0

In my chrome extension with manifest V2 I was using chrome.webRequest.onBeforeRequest to get all requests of current tab. Here is

const dataSet = {};
chrome.webRequest.onBeforeRequest.addListener(function (details) {

    if (details && details.url && details.type == "image") {
        if (!dataSet[tabId]) {
            dataSet[tabId] = new Set([]);
        }
        const currentSet = dataSet[tabId];
        currentSet.add(details.url);
    }


}, {
    urls: ["<all_urls>"]
});

I'm trying same code in manifest version 3 but event didn't triggers. Also I've tried this workaround but it still didn't works.

chrome.webNavigation.onBeforeNavigate.addListener(function(){

   // this event is not being triggered
   chrome.webRequest.onBeforeRequest.addListener(function(details){

   },{urls: ["<all_urls>"],types: ["main_frame"]});


},{
    url: [{hostContains:"domain"}]
});

Also tried to use webNavigation.onHistoryStateUpdated but still onBeforeRequest didn't triggers

chrome.webNavigation.onHistoryStateUpdated.addListener((details) => {
    console.log('wake me up', details);
    chrome.webRequest.onBeforeRequest.addListener(
        (details) => {
          console.log(details);
        },
        {
          urls: ['<all_urls>'],
         
        },
        
      );
  });

Console output of background page

Armen Stepanyan
  • 1,618
  • 13
  • 29
  • 1
    Assuming you're using modern Chrome 108 which fixed webRequest wake-up bug, it can be another known bug in registration of the service worker. Try uninstalling the extension first, then install it again. Also note that console.log is cleared when the service worker is terminated so you need to look at it immediately. – wOxxOm Dec 11 '22 at 20:48
  • Is this literally the code you're using? Because the first code block contains one closing curly bracket too many. – Thomas Mueller Dec 11 '22 at 22:45
  • @wOxxOm yes, my current chrome version is 108.0.5359.99. I've also tried to put debugger in chrome.webRequest.onBeforeRequest event, but still it didn't fired. I've also tried to woke up background page with alarms(https://stackoverflow.com/a/70003493/6823766), I see that background is restarted, but still onBeforeRequest is not triggers – Armen Stepanyan Dec 12 '22 at 07:28
  • Did you open [background.js console?](/a/10258029) – wOxxOm Dec 12 '22 at 08:57
  • @wOxxOm sure, I've opened background console, I can see other console logs from other events (chrome.webNavigation.onBeforeNavigate or chrome.webNavigation.onHistoryStateUpdated) https://prnt.sc/Qs1LA4SBdumb – Armen Stepanyan Dec 12 '22 at 14:29
  • 1
    1) Don't use chrome.webNavigation.onBeforeNavigate listener, it's not necessary anymore and actually it may be the reason of the problem. 2) Try uninstalling the extension first, then install it again. – wOxxOm Dec 12 '22 at 14:43
  • @wOxxOm I've deleted and added extension, here is my code https://prnt.sc/iO_V7mDXCWtd I can see 'wake me up' output of onHistoryStateUpdated event, but still no output from onBeforeRequest – Armen Stepanyan Dec 12 '22 at 14:54
  • Don't use chrome.webNavigation listener, it's not necessary anymore and actually it may be the reason of the problem. – wOxxOm Dec 12 '22 at 16:19
  • @wOxxOm I tried without any other event , just chrome.webRequest.onBeforeRequest.addListener(....) like in manifest V2 but it don't works ( – Armen Stepanyan Dec 12 '22 at 18:33
  • Well, then it's a bug in Chrome. – wOxxOm Dec 12 '22 at 21:06

0 Answers0