Greasemonkey JQuery search dynamic table text and replace with dynamic link

294 views Asked by At

I have a Website with tables which have informations about work projects. Here's an example:

    <tr valign="top" bgcolor="#FFFFA0"><td valign="top" bgcolor="#00CC00"></td>
    <td valign="top"><a href="/dsqplan/cpi-ban/dsqplan.pa?C_IPNR=1500&amp;report=Project&amp;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&amp;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>

Now I want to construct a dynamic Greasemonkey script which search for dynamic tags like FEAT-1234,WTS-4567,PIDT-7896 etc. and replace it with a direkt dynamic link to the entry of the Bug in the management software. This is my code so far:

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
    });
}

The search function of the tag works but the replacing still makes problems. One problem is that the links are variable and the Number of the tag that should be found must be part of the link. The most tags have their own link structure. I want to an elegant solution, which is not very long, but I think my code is not very suitable. How can I solve the problem?

PS: This is the first time that I have been working with Greasemonkey,Javascript and JQuery.

1

There are 1 answers

0
TheTuck On BEST ANSWER

Okay i made a realy stupid mistake. I have solved the problem now. Here is the Code.

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
    });
}