LocalStorage in Greasemonkey

I started writing a greasemonkey script, and I have to deal with localstorage problems in a Greasemonkey script. The only way I could figure out a local array in GM is to create another Javascript instance in the newElement.innerHTML DOM property, but the rest of my variables are not available.

Any ideas? Here's a snippet of Greasemonkey code 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++; 
+7
javascript local-storage google-chrome-extension greasemonkey
source share
2 answers

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

+9
source share

hmm, unsafeWindow.localStorage is not working, I think? I know this is not a problem for chrome to get localStorage, never tried this on firefox to be honest.

+7
source share

All Articles