Search code examples
greasemonkeytampermonkey

Add a user-style to all links with a certain content?


Newbie question:

I'd like to scan a page for all links where the href starts with hide? and then I'd like to add style="float:left" to the link like:

<a href="hide?6765765" style="float:left">

How do I do this?
Thank you!


Solution

  • Here's one way. Google any unfamiliar terms (and also "jQuery selectors").

    // ==UserScript==
    // @name     _Float "hide" links
    // @match    *://YOUR_SERVER.COM/YOUR_PATH/*
    // @require  https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
    // @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
    // @grant    GM_addStyle
    // @grant    GM.getValue
    // ==/UserScript==
    //- The @grant directives are needed to restore the proper sandbox.
    
    waitForKeyElements ("a[href^='hide?']", floatNodeLeft);
    
    function floatNodeLeft (jNode) {
        jNode.css ("float", "left");
    }