如何根据链接的标题属性的不同短语来更改链接的文本? [英] How to change a link's text based on different phrasing of the link's title attribute?
问题描述
在我的梦幻足球页面上,它提供了有关对方球队的统计信息,当您将鼠标悬停在其上时,它会告诉您他们放弃了多少分(参见图片).
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>
如何创建Greasemonkey脚本,该脚本会将#添加到团队名称的末尾(即"Was"变成"Was-3"
How do I create a Greasemonkey script that will add the # to the end of the team name (i.e. "Was" becomes "Was - 3"
有一个问题是,有时排名会说它放弃了第二最少的分数",在这种情况下,您必须做32-2才能获得绝对排名.
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.
推荐答案
使用基于周围文本进行切换的正则表达式从title
属性中提取数字.
Extract the number from the title
attribute, using regex that switches based on the surrounding text.
以下完整但未经测试的Greasemonkey脚本说明了该过程:
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);
}
}
这篇关于如何根据链接的标题属性的不同短语来更改链接的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!