我有一个网站,其中包含有关工作项目信息的表格。这是一个例子:
<tr valign="top" bgcolor="#FFFFA0"><td valign="top" bgcolor="#00CC00"></td>
<td valign="top"><a href="/dsqplan/cpi-ban/dsqplan.pa?C_IPNR=1500&report=Project&SI_STATUS=0,1,2">1500</a>-779</td>
<td valign="top">140072EAL</td>
<td valign="top">125</td>
<td valign="top">DSQ ABC</td>
<td valign="top">34_FEAT-1234: Object Index: Example</td>
<td valign="top"></td>
<td valign="top">om</td>
<td valign="top"><a href="/dsqplan/cgi-bin/dsqplan.pl?C_arranger=br&report=Prio-List">br</a></td>
<td valign="top"><p title = "individual task is scheduled">edit</p></td>
<td valign="top">9,7</td>
<td valign="top">2,3</td>
<td valign="top">7</td>
<td valign="top">8</td>
<td valign="top">2016 24,4</td>
<td valign="top">2016 35</td>
<td valign="top">0</td>
</tr>
现在我想构建一个动态Greasemonkey脚本,它搜索动态标签,如FEAT-1234,WTS-4567,PIDT-7896等,并将其替换为指向管理软件中Bug条目的direkt动态链接。到目前为止,这是我的代码:
var replacementRegexp = /([A-Z]+)-([0-9]{4})/; //defined regular expression
var searchvar = "";
var link = "";
for (var i = 0; i < 2; i++)
{
if(i == 0)
{
$searchvar = "FEAT";
$link = 'https://vmpolarion.dqa-ac.de/FEAT/workitem?id=FEAT-';
}
else if (i == 1)
{
$searchvar = "WTS";
$link = 'https://vmpolarion.dqa-ac.de/WTS/workitem?id=WTS-';
}
else
{
$searchvar = "PIDT";
$link = 'https://www.vpext.dsa-ac.de/PIDT/show_bug.cgi?id=PIDT-';
}
$("table td:contains('"+searchvar+"')").each(function(index, element) //looking for the tag
{
$(element).html($(element).html().replace(replacementRegexp, '<a href="'+link+'$2">$1-$2</a>')); //replaces the text with a link to the Bugzilla entry
});
}
标签的搜索功能有效但替换仍然会产生问题。一个问题是链接是可变的,应该找到的标记的编号必须是链接的一部分。大多数标签都有自己的链接结构。我想要一个优雅的解决方案,这不是很长,但我认为我的代码不是很合适。我该如何解决这个问题呢?
PS:这是我第一次使用Greasemonkey,Javascript和JQuery。
答案 0 :(得分:0)
好吧,我犯了一个非常愚蠢的错误。我现在已经解决了这个问题。这是守则。
var replacementRegexp = /([A-Z]+)-([0-9]{4})/; //defined regular expression
var query = new Array();
query[0] = new Array("table td:contains('FEAT')",'<a href="https://example.com">$1-$2</a>'); //search word|Link
query[1] = new Array("table td:contains('WTS')",'<a href="https://example.com">$1-$2</a>');
query[2] = new Array("table td:contains('PIDT')",'<a href="https://example.com">$1-$2</a>');
query[3] = new Array("table td:contains('ATH')",'<a href="https://example.com">$1-$2</a>');
for (var i = 0; i <= query.length; i++)
{
$(query[i][0]).each(function(index, element) //looking for variable
{
$(element).html($(element).html().replace(replacementRegexp, query[i][1])); //replaces the text with a link to the Database entry
});
}