【问题标题】:Local storage across domains using a Greasemonkey script使用 Greasemonkey 脚本跨域进行本地存储
【发布时间】:2012-12-15 06:32:26
【问题描述】:

是否可以使用 Greasemonkey 脚本跨域存储数据?我想允许从使用相同 Greasemonkey 脚本的多个网站访问 Javascript 对象。

【问题讨论】:

  • 我认为可以使用GM_setvalue 存储数据,但我不确定是否可以跨域共享。
  • GM_setvalue 的相关文档可以在这里找到:wiki.greasespot.net/GM_setValue
  • 脚本之间可能会以某种方式共享数据:google.com/…

标签: greasemonkey


【解决方案1】:

是的,这是GM_setvalue() 的目的之一,它存储数据,每个脚本,并且跨域。

请注意,沼泽标准 GM_setValue() 有点问题。它可能会使用大量全局资源或导致脚本实例崩溃。

以下是一些准则:

  1. 不要使用GM_setValue() 来存储字符串以外的任何内容。对于其他任何事情,请使用诸如GM_SuperValue 之类的序列化程序。即使是看起来很无辜的整数也可能导致默认的GM_setValue() 崩溃。

  2. 与其存储大量小变量,不如将它们包装在一个对象中并使用其中一个序列化程序进行存储。


最后注意localStorage在javascript中有特定的含义,而localStorage域特定的。

【讨论】:

    【解决方案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');
    

    或者只是……

    localStorage.myEntry = "this is a string";
    
    bar = localStorage.myEntry;
    

    【讨论】:

    • 您没有解决问题的跨域问题。
    猜你喜欢
    • 2014-04-29
    • 2016-03-01
    • 2013-06-14
    • 2022-10-26
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    相关资源
    最近更新 更多