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

标签 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 脚本,将 # 添加到团队名称末尾(即“Was”变为“Was - 3”

存在的一个问题是,有时排名会放弃“第二少的分数”,在这种情况下,您必须执行 32-2 才能获得绝对排名。

link showing mouseover

最佳答案

使用根据周围文本进行切换的正则表达式从 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);
    }
}

关于javascript - 如何根据链接标题属性的不同措辞更改链接的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26533871/

相关文章:

javascript - 动态添加对象属性到现有对象

javascript - 手机上的网站超链接不起作用

javascript - 动画不透明文本CSS

javascript - 仅在选中时更改所选选项的文本

javascript - GM_xmlhttpRequest 数据被放置在错误的位置

javascript - 如何在 Loopback 中实现 ACID 事务

javascript 在 Firefox 中生成无效的 HTML5 属性

javascript - AWS 签名和 Javascript

javascript - 如何将动态表单附加到表格末尾?

greasemonkey - 用greasemonkey输入一个输入值