Is it possible to store data across domains using a Greasemonkey script? I want to allow a Javascript object to be accessed from multiple websites that are using the same Greasemonkey script.
Solution: Use an iframe to save the data in localStorage, and then the other domains ask the iframe for what we already saved. 4- in whatever other domain we ask for the data from the iframe (using postMessage and listening for a response).
As you may know, LocalStorage is domain based. You can't read or write from localstorage that's on different domain, even if that's subdomain. But there is iframe trick that you can use to store data from domain to it's subdomain.
If the data to be stored is needed on multiple subdomains, local storage also does not work – because local storage is subdomain-specific. Cookies, on the other hand, are more flexible in scope – they can be written to work across multiple subdomains (or even all subdomains on the same top-level domain).
It enables users to install scripts that make on-the-fly changes to web page content after or before the page is loaded in the browser (also known as augmented browsing). The changes made to the web pages are executed every time the page is viewed, making them effectively permanent for the user running the script.
Yes, that is one of the purposes of GM_setvalue()
, it stores data, per script, and across domains.
Beware that the bog-standard GM_setValue()
is somewhat problematic. It can use lots of global resources or cause a script instance to crash.
Here are some guidelines:
Do not use GM_setValue()
to store anything but strings. For anything else, use a serializer such as GM_SuperValue. Even innocent looking integers can cause the default GM_setValue()
to crash.
Rather than store lots of small variables, it may be better to wrap them in an object and store that with one of the serializers.
Finally note that localStorage
has a specific meaning in javascript, and localStorage
is domain specific.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With