Local storage across domains using a Greasemonkey script

我与影子孤独终老i 提交于 2019-12-22 05:23:08

问题


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:

  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.




回答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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!