Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local storage across domains using a Greasemonkey script

Tags:

greasemonkey

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.

like image 230
Anderson Green Avatar asked Dec 15 '12 06:12

Anderson Green


People also ask

How do I access local storage between domains?

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).

Does localStorage work across domains?

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.

Does local storage work across subdomains?

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).

How do Greasemonkey scripts work?

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.


1 Answers

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:

  1. 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.

  2. 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.

like image 152
Brock Adams Avatar answered Sep 22 '22 11:09

Brock Adams