0

I've built an extension (for both Chrome and Firefox) that needs to pass a lot of data from the background script to the content script to have it processed before the page loads. That is a hard requirement because of how it has to interact with the DOM.

I currently set a cookie in the background script and read that cookie at document_start from the content script. This works great but I recently found out there is a 4096 byte limit for all cookies for a domain. I can probably reduce the current size of the data by minimizing and even compressing it before setting the cookie but that's a band-aid fix. I'd like to figure out a more robust solution.

It'd really be nice if I could just use localstorage but it's async. So, is there a way to send more than 4kb from the background to the content script so that it is ready and available before the DOM loads?

0xdeadbeef
  • 11
  • 1
  • 3
  • In Chrome and Firefox you can use [webRequest + synchronous XHR](https://stackoverflow.com/questions/45102497/injecting-javascript-variable-before-content-script). In Firefox there's also browser.contentScripts to dynamically register a code string that contains your data e.g. `'var data = ' + JSON.stringify(data)`. – wOxxOm Nov 14 '20 at 15:51
  • Thanks @wOxxOm but the link you provided just talks about cookies which I am currently using. I'm also not familiar with the second technique you mention. Can you expand on that a bit? – 0xdeadbeef Nov 14 '20 at 16:35
  • Looks like I didn't actually read the answer. – wOxxOm Nov 14 '20 at 16:48
  • Anyway, here's an example of using synchronous XHR: https://github.com/openstyles/stylus/pull/1070/files. Another one's [here](https://github.com/violentmonkey/violentmonkey/pull/1100/files) in Violentmonkey and it also [uses](https://github.com/violentmonkey/violentmonkey/blob/master/src/background/utils/preinject.js) browser.contentScripts – wOxxOm Nov 14 '20 at 16:50

0 Answers0