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++;
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With