1

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.

AnandShiva
  • 906
  • 11
  • 22
  • Add `declarativeContent` to `permissions` in manifest.json and remove it from `request()`. – wOxxOm Oct 05 '21 at 12:19
  • I can't do that. we already published the extension. So now going and changing the manifest permission will make the extension inactive to existing users when we publish and they would have to enable it manually. @wOxxOm – AnandShiva Oct 05 '21 at 12:31
  • i have added declarativeContent as optional permission thought in manifest – AnandShiva Oct 05 '21 at 12:32
  • No, declarativeContent doesn't have its own warning so there's no need to make it optional or request it. The extension won't be disabled. – wOxxOm Oct 05 '21 at 12:41
  • okay. i tried to add declarativeContent in permission and removed it from request(). so request API only has origins: ['sites list']. But still the original issue persists. – AnandShiva Oct 05 '21 at 13:04
  • Then it's either a bug in Chrome or there's something in your js/css. First, verify that the content script is not injected in devtools -> Sources -> Content scripts panel on the left. – wOxxOm Oct 05 '21 at 13:58
  • Thanks for the tips @wOxxOm. Yea the content script is not loaded. Verified the same via Sources -> Content scripts. The declarative content API's RequestContentScript is actually an experimental feature according to docs, but its available on stable build. I will check if I can raise a bug on chromium. – AnandShiva Oct 05 '21 at 14:44
  • It's https://crbug.com/708115 so you'll have to remove `css` injection. – wOxxOm Oct 05 '21 at 14:53
  • It is true that CSS injections are not happening. But even without CSS, it is not working. JS is not loaded either way. – AnandShiva Oct 05 '21 at 15:42
  • The weirdest thing is on hot reload of extension, the injection starts working like initial install time and then stops after we close all similar page/ browser – AnandShiva Oct 05 '21 at 15:44
  • The presence of `css` key is what triggers the bug so you'll have to remove it. – wOxxOm Oct 05 '21 at 15:56
  • Yes. I did remove the key :). Still, the issue persists. – AnandShiva Oct 05 '21 at 16:00
  • You should also reregister the rules i.e. remove all the old ones as they contain `css` key, and register again. – wOxxOm Oct 05 '21 at 16:16
  • I did try that. I have updated the code also with the same feedback. Also filled a chrome bug. https://crbug.com/1256828 – AnandShiva Oct 05 '21 at 16:36

0 Answers0