如何根据链接标题属性的不同措辞更改链接的文本?

时间:2014-10-23 17:28:47

标签: javascript jquery greasemonkey tampermonkey

在我的幻想足球页面中,它提供有关对方球队的统计数据,当你鼠标悬停时,它会告诉你他们放弃了多少分(见图)。

以下是与此相关的相关代码:

<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>

如何创建一个Greasemonkey脚本,将#添加到团队名称的末尾(即&#34;&#34;变为&#34;是 - 3&#34;

有一个问题是,有时排名表示它放弃了第二个最少的点数#34;在这种情况下你必须做32-2来获得绝对排名。

link showing mouseover

1 个答案:

答案 0 :(得分:1)

使用根据周围文本切换的正则表达式从title属性中提取数字。

以下完整但未经测试的Greasemonkey脚本说明了该过程:

// ==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);
    }
}