0

In my chrome extension with manifest V2 I was using chrome.webRequest.onBeforeRequest listener to get all images for current tab. Here is my code.

background.js

const dataSet = {};
chrome.webRequest.onBeforeRequest.addListener(function (details) {
    const tabId = details.tabId;
    if (details.url && details.type == "image") {
        if (!dataSet[tabId]) {
            dataSet[tabId] = new Set([]);
        }
        // keep all image urls for current tab
        dataSet[tabId].add(details.url);
    }


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

In manifest version 3 webRequest is deprecated and as mentioned in documentation we can use declarativeNetRequest instead. I tried to use declarativeNetRequest by creating dynamic rule but I didn't understand how to get and keep all urls. So how can I achive same functionality in manifest version 3 ?

chrome.declarativeNetRequest.updateDynamicRules({
    removeRuleIds: [1],
    addRules: [{
        id: 1,
        condition: {
            regexFilter: '*',
            resourceTypes: ['image'],
        },
        action: {
            type: 'allow',

        },
    }]
})
Armen Stepanyan
  • 1,618
  • 13
  • 29
  • 3
    webRequest is **not** deprecated. Only its blocking mode is. You can use your listener as before, but there's a bug in Chrome with waking up the background script, see [the workaround here](https://stackoverflow.com/a/71189817/). – wOxxOm Jul 30 '22 at 07:24

0 Answers0