Greasemonkey JQuery 搜索动态 table 文本并替换为动态 link
Greasemonkey JQuery search dynamic table text and replace with dynamic link
我有一个网站,其中包含有关工作项目信息的表格。这是一个例子:
<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 等,并将其替换为直接动态 link 的条目管理软件中的错误。到目前为止,这是我的代码:
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+'">-</a>')); //replaces the text with a link to the Bugzilla entry
});
}
标签的搜索功能有效,但替换仍然有问题。一个问题是 link 是可变的,应找到的标签编号必须是 link 的一部分。大多数标签都有自己的 link 结构。我想要一个优雅的解决方案,不是很长,但我认为我的代码不是很合适。我该如何解决这个问题?
PS:这是我第一次使用Greasemonkey,Javascript和JQuery。
好吧,我犯了一个非常愚蠢的错误。我现在已经解决了这个问题。这是代码。
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">-</a>'); //search word|Link
query[1] = new Array("table td:contains('WTS')",'<a href="https://example.com">-</a>');
query[2] = new Array("table td:contains('PIDT')",'<a href="https://example.com">-</a>');
query[3] = new Array("table td:contains('ATH')",'<a href="https://example.com">-</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
});
}
我有一个网站,其中包含有关工作项目信息的表格。这是一个例子:
<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 等,并将其替换为直接动态 link 的条目管理软件中的错误。到目前为止,这是我的代码:
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+'">-</a>')); //replaces the text with a link to the Bugzilla entry
});
}
标签的搜索功能有效,但替换仍然有问题。一个问题是 link 是可变的,应找到的标签编号必须是 link 的一部分。大多数标签都有自己的 link 结构。我想要一个优雅的解决方案,不是很长,但我认为我的代码不是很合适。我该如何解决这个问题?
PS:这是我第一次使用Greasemonkey,Javascript和JQuery。
好吧,我犯了一个非常愚蠢的错误。我现在已经解决了这个问题。这是代码。
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">-</a>'); //search word|Link
query[1] = new Array("table td:contains('WTS')",'<a href="https://example.com">-</a>');
query[2] = new Array("table td:contains('PIDT')",'<a href="https://example.com">-</a>');
query[3] = new Array("table td:contains('ATH')",'<a href="https://example.com">-</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
});
}