Posts
Search
Contact
Cookies
About
RSS

GreaseMonkey gottcha - script not triggering ?

Added 30 Sep 2014, 2:16 p.m. edited 18 Jun 2023, 1:12 a.m.
In the unlikely event you haven’t discovered GreaseMonkey it's ideal for tweaking the presentation of a website to suit your own taste. A good example of this would be the ad sponsored block of links typically at the top of the list of search results. These are often presented in a box with a subtly different background colour, alas this colour is usually so subtle that especially on a laptop screen you can't see it! (if I was suspicious I'd think it was done that way on purpose!) Doing a quick hack for this rather than searching for someone else’s script is probably quicker and certainly good practice. My search engine of choice is startpage and I was scratching my head trying to figure out why the script wasn't being triggered - then I looked again at the URL (check the page info when right clicking on the page with firefox)
https://startpage.com/do/metasearch.pl
The thing here is the second slash after the domain name I'd assumed that the filter https://startpage.com/* would basically capture everything - turns out this isn't sufficient and https://startpage.com/*/* is what’s required. Just one more minor irritation is that ID attributes are *supposed* to be unique within a document, however the div at the bottom of the results also shares the same ID. While there are facilities to get arrays of all tags with shared class attributes, as far as I'm aware this is not the case with ID's. I decided to just brute force through all the div tags in a document searching for the same target ID, there may well be a more elegant way to do this but what the hey it works! I've included the script here for anyone who's not used GreaseMonkey but fancies a play...
// ==UserScript==
// @name        startpage ad
// @namespace   codifies@bedroomcoders.co.uk
// @description makes the background for the ad links actually visible
// @include     https://startpage.com/*/*
// @version     1
// @grant       none
// ==/UserScript==

//var el = document.getElementById("sponsored_container");
//el.style.backgroundColor = '#8080ff';

// *sigh* all id's *should* be unique

var all = document.getElementsByTagName("div");

for (var i=0, max=all.length; i < max; i++) {
     if (all[i].id=="sponsored_container") {
       all[i].style.backgroundColor = '#a0a0ff';
     }
}