How to change a link's text based on different phrasing of the link's title attribute?

99封情书 提交于 2019-12-08 13:10:38

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);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!