1

I have a content script (configured with document_start) which is injecting code into the current page, in order to overwrite/proxy existing objects.

So far, so good, that seems to work just fine and the content script code is executed before any Javascript on the page itself. The problem is the injected code should be configurable and there I only have the option between messaging and local storage, both of which uses callbacks and that "forced" asynchronism executes the callback function at some point after the page's Javascript.

Is there any way to have code injected via a content script configurable and still executed before any code from the currently open page?

  • [Use webRequest to set document.cookie](https://stackoverflow.com/a/45105934) which will be available in a content script at document_start. – wOxxOm Aug 27 '17 at 08:46
  • Provide the code, please – Denis L Aug 27 '17 at 08:46
  • Thanks @wOxxOm, I'll check out the cookie approach. – user8522817 Aug 27 '17 at 08:59
  • Local storage is only available asynchronously in a content script? That seems odd. – T.J. Crowder Aug 27 '17 at 09:02
  • @T.J.Crowder Unfortunately that seems to be the case https://developer.chrome.com/apps/storage#type-StorageArea – user8522817 Aug 27 '17 at 09:10
  • @T.J.Crowder, some articles in chrome documentation ambiguously (retardedly?) name chrome.storage.local as Local Storage. – wOxxOm Aug 27 '17 at 09:11
  • 1
    @user8522817: Ah, that's Chrome Storage, not Local Storage. Unfortunate if the documentation is sometimes lax with its terminology. – T.J. Crowder Aug 27 '17 at 09:27
  • 1
    Sorry, my bad for not using the right terminology. @wOxxOm I am looking right now into the cookie approach and setting the cookie in `onHeadersReceived` as per your example shouldn't be the problem however I am still somewhat stuck at getting the data out of the storage in a synchronous fashion. Would I need to keep it additionally in a "global" variable in order to access it with the `onHeadersReceived` listener in a synchronous fashion? – user8522817 Aug 27 '17 at 09:33
  • 1
    Yep, caching it in a global variable would be required. The only sync storage in the background page of an extension is the classic localStorage. – wOxxOm Aug 27 '17 at 09:35

0 Answers0