【问题标题】:How to change a link's text based on different phrasing of the link's title attribute?如何根据链接标题属性的不同措辞更改链接的文本?
【发布时间】:2014-11-14 11:22:49
【问题描述】:

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

这是与此相关的相关代码:

<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 才能获得绝对排名。

【问题讨论】:

  • 不要向已按原样解决的问题添加其他问题。为新问题打开一个新问题,即使它们是松散相关的。编辑是为了澄清现有问题,而不是提出新问题。
  • 好的,我问了new question

标签: javascript jquery greasemonkey tampermonkey


【解决方案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);
    }
}

【讨论】:

  • 您的代码看起来很可靠,但似乎不起作用。我在delinkChangeStat() 函数中添加了console.debug("test");,但我没有看到它。
  • 链接到目标页面。
  • 现在可以使用了!我的问题是我将它安装为 Chrome 扩展,但我必须安装 Tampermonkey 扩展:)
  • 我不得不稍作修改,因为如果团队放弃最多或最少,它不会说“放弃第一多..”。一个简单的 else if 语句解决了这个问题:)
  • 所以我很好地预测了不在问题中的问题? ;) 真高兴你做到了。请点赞。
猜你喜欢
  • 2014-07-08
  • 1970-01-01
  • 2017-09-29
  • 1970-01-01
  • 1970-01-01
  • 2013-11-06
  • 1970-01-01
  • 2015-03-24
  • 2018-01-09
相关资源
最近更新 更多