Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
chrome.declarativeContent API's RequestContentScript permission does not work after restart of the browser/ closing and reopening site

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.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!