1

On a certain homepage I visit I want to hide all links that I click. My idea was to use a Greasemonkey script like this:

var blocklist = JSON.parse(GM_getValue("blocklist"));
var as = document.getElementsByTagName('a');
var alength = as.length;   
for(var i=0; i<alength; i++) {
    var a = as[i];
    if(blocklist.indexOf(a.href) >= 0) {
        a.style.display='none';
    } else {
        a.setAttribute('onclick', 'alert("HELP"); return true;');
    }
}

Inside the script I can call this, no problem:

blocklist = blocklist.concat('http://someurl');
GM_setValue("blocklist", JSON.stringify(blocklist));

But in the website itself (read where it says alert("HELP");) I cannot call this function because neither the function nor the blocklist do exist.

Is there a way to access the function from the website? (probably not?) Where else could I store the values to get them back on the next load of the website? The firefox browser is set to sanitize on shutdown, so can't use a:visited or similar.

Ikarus
  • 43
  • 6

2 Answers2

1

You should use localStorage so that you can retain your list on subsequent page loads. It's really not too different from GM_setValue.

localStorage.setItem("blocklist", JSON.stringify(blocklist));

var blocklist = JSON.parse(localStorage.getItem("blocklist"));

Rob M.
  • 35,491
  • 6
  • 51
  • 50
  • No. localStorage can be erased: by the site, by Firefox settings, and/or by cleaners like *CCleaner*. `GM_setValue` is more robust and is cross-domain, to boot. – Brock Adams May 08 '14 at 02:42
  • Thx, I got it running with localStorage, but closing Firefox erased all data. Will try Brock Adams' solution now. Will give you a +1 for learning something new asap. – Ikarus May 08 '14 at 17:30
  • I wasn't aware that localStorage got blown away in GM scripts, so I learned something new as well! :) good luck to you @Ikarus – Rob M. May 08 '14 at 17:32
  • It's a Firefox setting, probably "sanitizeOnShutdown" in combination with "clearOnShutdown.offlineApps". – Ikarus May 08 '14 at 18:05
1
  1. Don't try to call GM_ functions from a webpage. (1) It's not directly possible, (2) it's a security risk, (3) it's almost never really necessary.

  2. Never use onclick in a Greasemonkey script (or at all, really). A simple alert("HELP"); return true; might work, but anything more will crash and it's bad form anyway.

  3. Also, if you use querySelectorAll versus getElementsByTagName, you can fine-tune what links you process, EG: document.querySelectorAll ("div.main a.user") -- which would get only those links with the CSS class user that were inside the <div> with the class main.

In this case, use addEventListener (or use jQuery) to handle the links so your script code would become like:

var blocklist = JSON.parse (GM_getValue ("blocklist") );
var targlinks = document.querySelectorAll ('a');
for (var J = targlinks.length - 1;  J >= 0;  --J) {
    var targlink = targlinks[J];

    if (blocklist.indexOf (targlink.href) >= 0) {
        targlink.style.display = 'none';
    } else {
        targlink.addEventListener ('click', virginLinkHandler, false);
    }
}

function virginLinkHandler (zEvent) {
    var newURL  = zEvent.target.href;
    blocklist   = blocklist.concat (newURL);
    GM_setValue ("blocklist", JSON.stringify (blocklist) );
}
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Thanks, looks weird but works great, probably because the handler is defined in the GM scope. And nice one with querySelectorAll. Sidenote: In case you want a return false you need to use zEvent.preventDefault(); instead. – Ikarus May 08 '14 at 18:08
  • Yeah, I thought about mentioning that, but it wasn't clear if it would apply in your case. Glad you got it working. – Brock Adams May 08 '14 at 18:11
  • Userscripts.org looks down, so if anyone is interested, this is the result on [pastebin](http://pastebin.com/RiqT0C98). I just love reading the [Hot Network Questions](http://stackexchange.com/questions?pagesize=50) and was looking for a way to keep track which ones are new. Thx again! – Ikarus May 08 '14 at 23:22