Search code examples
javascripttampermonkey

How can I create a Tampermonkey script that will prevent certain parts of a page from being links?


I need to quickly select, copy and paste text (the content descriptors, specifically) from pages like this: https://www.esrb.org/search/?searchKeyword=&searchType=LatestRatings&timeFrame=PastWeek&pg=1&platform[]=All%20Platforms&rating[]=E&rating[]=E10%2B&rating[]=T&rating[]=M&rating[]=AO&descriptor[]=All%20Content

I can't easily select the text I want because the whole game box is a link and I guess click and dragging makes it think you are trying to drop a link somewhere. I need to do a lot of this, so going to each individual game page and grabbing the text there would be very cumbersome.

I tried blocking links to "https:www.esrb.org/ratings/" as detailed on this page: https://scottlilly.com/greasemonkey-block-links-to-annoying-websites/ but there was no change.


Array.prototype.forEach.call (linkList, function (link) {
    if (link.hostname.includes("https://www.esrb.org/ratings/")
) {
        //-- Block the link
        link.href = "javascript:void(0)";
    }
} );


Solution

  • Changing the URL does not make it not a link. However, in your case the link has no text and is overlayed over the box via CSS, so all you need is to hide it. I would use this function:

    function setLinks(state) {
        const links = document.querySelectorAll("#results .game>a");
        for(const link of links) {
            // false means hide, true means show
            link.style.display = !!state ? "" : "none";
        }
    }
    

    Call setLinks(false) to disable them and setLinks(true)` to activate them again. You could even make it controllable, for example only disable links if you're holding a key. This would disable the links when you hold S, as in select:

    window.addEventListener("keydown", (e)=>{if(e.which==83)setLinks(false);});
    window.addEventListener("keyup", (e)=>{if(e.which==83)setLinks(true);});