0

I'm trying to use a jquery userscript on chrome in tampermonkey on a pc. I found a script on here and tried to modify it to linkify the contents of a table. I think the reason it's not working is because I need to specify that inputText is the link on the webpage. The link is always the 2nd child of the same table. The table doesn't have an id or class so I think I have to get to t the parent/child way. How can I specify that is what I want to be linkified? It is the restaurant name on each webpage, always in the same place on the webpage, and it's supposed to search google for that. https://www.mturk.com/mturk/return?groupId=35DNGIKWRF46YCSNYBRS2YNM4DY173&requesterId=&hitId=39O5D9O87TSA1G1HKT8DKE5ZXMMC3H&externalHit=true&canAccept=

//var $inputText= $(t).children('td').eq(1);

function linkify(inputText) {
    var replacedText, replacePattern1, replacePattern2, replacePattern3;

    //URLs starting with http://, https://, or ftp://
    replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
    replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');

    //URLs starting with "www." (without // before it, or it'd re-link the ones done above).
    replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
    replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');

    return replacedText;
}

Thank you very much but I can't get the right object. The table doesn't have an id or class. It is the first table in a section that has an id. Here are two of the things I have tried with the answerer's code below. var $element = $('#Other').find('t').eq(1).find('tr').eq(1).find('td').eq(1); var $element = $('#Other').find('tr').eq(1).find('td').eq(1);

It's still not working with the 2nd answer below. Could someone please go the link and look at it? I can't post code now here for some reason.

2 Answers2

0

What you want is the find() method ie var element = $('#myTable').find('tr').eq(1).find('td').eq(1); Additionally, I would pass the element itself to the function rather than the text.

Here is a contrived example:

function linkify($element) {
    var inputText= $element.html();
    var replacedText, replacePattern1, replacePattern2, replacePattern3;

    //URLs starting with http://, https://, or ftp://
    replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
    replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');

    //URLs starting with "www." (without // before it, or it'd re-link the ones done above).
    replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
    replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');
    
    // set the replaced value to the cell rather than retunrning it
    $element.html( replacedText );
}

//get the second cell on the first actual row to the table  
// remember that the header row is really the first row
var element = $('#myTable').find('tr').eq(1).find('td').eq(1);
// call your linkify function but pass it the element rather than it's text
// give the element a juery wraper first though
linkify( $(element) )
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="myTable" width="600" border="1">
  <tbody>
    <tr>
      <th scope="col">Column 1</th>
      <th scope="col">Column 2</th>
    </tr>
    <tr>
        <td>http://example.com/home.html</td>
      <td>http://example.com/home.html.</td>
    </tr>
    <tr>
      <td>http://example.com/home.html</td>
      <td>http://example.com/home.html</td>
    </tr>
  </tbody>
</table>
Wesley Smith
  • 19,401
  • 22
  • 85
  • 133
0

Assuming the second child is the first <td> of the table element.

var link = $('section selector').find('table td:first-of-type')[0].innerHTML;

or

var link = $('section selector').find('table td:nth-of-type(1)')[0].innerHTML;

or

var link = $('section selector').find('table td:first-of-type').html();

It would help to see the HTML structure you are dealing with.

eyegropram
  • 672
  • 4
  • 11