0

I develop a Chrome extension that sometimes updates its settings in background script via websockets. It also uses a content script to inject a custom script in a web page. My manifest.json is like:

...
   "content_scripts": [
   {
     "all_frames":true,
     "match_about_blank":true,
     "run_at": "document_start",
     "matches": ["*://*/*"],
     "js": ["js/cs.js"]
   }
...

The problem is that I want to store settings from background script somewhere to use it in the content script. I'm aware of messaging and chrome.storage API but they are asynchronous so I can't use it in that way. So basically I'm looking for some persistent synchronous storage that can be accessed both from background and content scripts.

Any ideas? Thanks.

  • The only method for that in Chrome is using the deprecated *synchronous XHR* which will be intercepted by webRequest in the background script. A variant of this is setting a cookie via webRequest and then reading it in the content script as `document.cookie`, see [Injecting javascript variable before content script](https://stackoverflow.com/a/45105934) but it won't work in some edge cases like history navigation or a super fast localhost site. – wOxxOm Mar 17 '20 at 09:23
  • @wOxxOm Thank you, it's pretty interesting way. But I ended up with an additional content script like config.js updated by an external app and then with chrome.runtime.reload() call - unfortunately it's more tricky but more reliable than other ways. I can use an unpacked extension so it's not a problem. – user2081736 Mar 18 '20 at 18:07
  • If such a thing works for you then you should be able to use that js file with chrome.declarativeContent API and RequestContentScript action, as it runs the script before the main document_start scripts. Disregard the warning in the documentation about the experimental status as it's quite hard to hit those buggy edge cases e.g. I've never encountered those. – wOxxOm Mar 18 '20 at 18:11

0 Answers0