问题
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.
回答1:
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 defaultGM_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.
回答2:
http://wiki.greasespot.net/GM_setValue
foo = "This is a string";
GM_setValue('myEntry', foo);
http://wiki.greasespot.net/GM_getValue
bar = GM_getValue('myEntry');
bar = GM_getValue('myOtherEntry', "default value if no value was found");
http://wiki.greasespot.net/GM_deleteValue
GM_deleteValue('myEntry');
GM_deleteValue('myOtherEntry');
https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage
foo = "this is a string";
localStorage.setItem('myEntry', foo);
bar = localStorage.getItem('pointer') || "default value";
localStorage.removeItem('myEntry');
or just...
localStorage.myEntry = "this is a string";
bar = localStorage.myEntry;
来源:https://stackoverflow.com/questions/13889995/local-storage-across-domains-using-a-greasemonkey-script