Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LocalStorage in Greasemonkey

I've started writing a greasemonkey script, and am facing problems performing localstorage in the Greasemonkey script. The only way I could figure out localstorage in GM is by creating another instance of Javascript in the newElement.innerHTML DOM property, but there the rest of my variables are inaccessible.

Any ideas ? Here's the Greasemonkey code fragment I'm working on.

        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++;
like image 643
0xff0000 Avatar asked Oct 31 '10 12:10

0xff0000


2 Answers

If you just need to store values you can go the classic Greasemonkey way using GM_getValue() and GM_setValue() functions which work pretty well.

  • http://wiki.greasespot.net/GM_getValue
  • http://wiki.greasespot.net/GM_setValue
like image 81
Xavi Esteve Avatar answered Oct 05 '22 22:10

Xavi Esteve


hmm, unsafeWindow.localStorage doesn't work I guess? I know it's not a problem for chrome to get the localStorage, never tried it on firefox to be honest.

like image 33
Johan Avatar answered Oct 05 '22 23:10

Johan