Greasemonkey的LocalStorage

0xf*_*000 9 javascript greasemonkey local-storage google-chrome-extension

我已经开始编写一个greasemonkey脚本,并且遇到了在Greasemonkey脚本中执行localstorage的问题.我可以在GM中找出localstorage的唯一方法是在newElement.innerHTMLDOM属性中创建另一个Javascript实例,但其余的变量都是不可访问的.

有任何想法吗 ?这是我正在研究的Greasemonkey代码片段.

        var testHref = anchorTag[i].href;
    var testHTML = anchorTag[i].innerHTML;
    var patHref = /item\?id=[0-9]*/g;
    var patCaptureId = /item\?id=([0-9]*)/g;
    var testId = patCaptureId.exec(testHref);
    var patHTML = /[0-9]* comment(|s)/g;
    var patHTML2 = /discuss/g;
    if(patHref.test(testHref) && !patHTML.test(testHTML) && !patHTML2.test(testHTML))
    {
        newElement = document.createElement('span');
        newElement.style.color = "#FF0000";
        newElement.innerHTML = "<a href=\"javascript:localStorage.setItem( 'one', 'rishabhVerma' ); var test = localStorage.getItem( 'one' ); console.log( test );\"> B</a>";
        anchorTag[i].parentNode.insertBefore(newElement, anchorTag[i].nextSibling); 
    }
    i++;
Run Code Online (Sandbox Code Playgroud)

Xav*_*eve 11

如果您只需要存储值,您可以使用经典的Greasemonkey方式GM_getValue()GM_setValue()功能非常好.


Joh*_*han 8

嗯,unsafeWindow.localStorage不起作用我猜?我知道chrome获取localStorage并不是一个问题,说实话,从来没有在firefox上尝试过.

  • 我只是在Greasemonkey脚本中使用`unsafeWindow.localStorage`(在Mac上的Firefox 3.6中),这似乎工作正常. (5认同)
  • 请注意,如果能够捕获调用unsafeWindow对象时发生的事件,那么使用unsafeWindow将允许webpages检测并可能使用oligipulate greasemonkey.此信息可在[此处]验证(https://www.safaribooksonline.com/library/view/greasemonkey-hacks/0596101651/ch01s13.html) (2认同)