0xff0000
0xff0000

Reputation: 917

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++;

Upvotes: 10

Views: 11556

Answers (2)

Xavi Esteve
Xavi Esteve

Reputation: 1162

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.

Upvotes: 14

Johan
Johan

Reputation: 3212

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.

Upvotes: 8

Related Questions