w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Scriptish / Greasemonkey GM_setValue and GM_getValue equivalents for Google Chrome Extensions

There is no synchronous cross-domain storage mechanism for content scripts.

However, if you really want to get such a feature, then you could implement such a storage yourself, by maintaining a local copy of the storage backed by chrome.storage.

  1. Initialization: Use chrome.storage.local.get(null, callback) to fetch all stored data and store it in a local variable.
  2. Maintenance:
    • Use the chrome.storage.onChanged event to watch for changes and update the local copy when needed.
    • Whenever GM_setValue (etc.) is used, immediately update the local copy, so that calling GM_getValue immediately after GM_setValue gives the expected result.

Ideally, you should defer the execution of the other scripts until the storage is initialized. If this is not possible, create a separate script, and let it "run_at": "document_start". Using this method, it's likely (although not guaranteed) that the storage is ready when the rest of your code runs at "document_end"`.

There is another way to synchronously get/set storage values. I strongly, strongly discourage you from using this method. Synchronous XMLHttpRequest together with the webRequest API (chrome.webRequest.onBeforeRequest) can be used to communicate data from the background page to the content script, and vice versa.
Sending data is trivial (e.g. set a value in the URL),
getting data back is a bit more difficult. You can use URL.createObjectURL to get a blob: URL of your data, and return {redirectUrl: ...} to reply the just-created URL in the content script.

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.