How to change a link's text based on different

2019-06-05 03:36发布

In my Fantasy football page, it gives stats about the opposing team and when you mouseover it tells you how many points they give up (See picture).

Here is the relevant code pertaining to this:

<a class="Inline F-rank-good" title="WAS gives up the 3rd most fantasy points to the QB position." target="_blank" href="/f1/777400/pointsagainst?pos=QB&ntid=28">Was</a>

How do I create a Greasemonkey script that will add the # to the end of the team name (i.e. "Was" becomes "Was - 3"

One problem there is, is that sometimes the rank says it gives up the "2nd fewest points", in which case you would have to do 32-2 to get the absolute rank.

link showing mouseover

1条回答
虎瘦雄心在
2楼-- · 2019-06-05 04:07

Extract the number from the title attribute, using regex that switches based on the surrounding text.

The following, complete but untested, Greasemonkey script illustrates the process:

// ==UserScript==
// @name     FF, stat delinker
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/
waitForKeyElements ("a.Inline", delinkChangeStat);

function delinkChangeStat (jNode) {
    var rawText     = jNode.attr ("title")  ||  "";
    var deltaText   = "";
    var mtchResult  = null;

    //-- IMPORTANT: the single =, in the if() statements, is deliberate.

    //-- Like "gives up the 3rd most"
    if (mtchResult  = rawText.match (/gives up the (\d+)[a-t]{2} most/i) ) {
        deltaText   = mtchResult[1]; 
    }
    //-- Like "gives up the 2nd fewest points"
    else if (mtchResult = rawText.match (/gives up the (\d+)[a-t]{2} fewest/i) ) {
        deltaText   = 32 - parseInt (mtchResult[1], 10); 
    }
    //-- ADD ADDITIONAL else if() CLAUSES HERE AS NEEDED.

    //-- Change the link
    if (deltaText) {
        jNode.text (jNode.text () + " - " + deltaText);
    }
}
查看更多
登录 后发表回答