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',
},
}]
})