I am trying to dynamically get permission to certain sites (Eg amazon.fr) using optional permission and once I get the permission I am injecting some JS into the sites for which I requested permissions using declarative content API's RequestContentScript function.
All this works fine for the first time till I either close the browser or close all the pages where scripts are injected initially. But after the page inject JS is not injected into those sites. When I check the permission and sites of the extension I see all the permission I requested optionally are still present via the get permission API.
The background js code I am using to request permission and inject scripts is as follow
const conditions = [];
chrome.permissions.request({
permissions: ['declarativeContent'],
origins:['https://www.amazon.fr/'],
}, (granted) => {
console.log({ granted });
if (granted) {
conditions.push(new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostEquals: 'www.amazon.fr', schemes: ['https'] },
css: ['div'],
}));
const rule2 = {
conditions,
actions: [
new chrome.declarativeContent.RequestContentScript({
js: ['page_injects/index.js'],
css: ['page_injects/index.css'] })],
};
chrome.declarativeContent.onPageChanged.removeRules(undefined,() => {
chrome.declarativeContent.onPageChanged.addRules([rule2]);
});
console.log('chrome.declarativeContent.onPageChanged.addRules done');
}
});
Since this works for the first time I am not sure what could be issue in having the same behavior the second time and after.
Any tips on how to proceed with this?
I used this SO question to arrive at injecting scripts optionally.