Search code examples
javascriptgreasemonkey

How can I make greasemonkey search only within a table


Below is my code and currently it searches the whole webpage. I'm trying to figure out how to make it search only within a table. (There is only one table on the page).

Any help would be appreciated.

var TargetLink              = $("a:contains('gg')");
var TargetSink              = $("a:contains('u')");

if (TargetLink  &&  TargetLink.length)
{
window.location.href    = TargetLink[0].href;
} 
else if (TargetSink  &&  TargetSink.length) 
{
window.location.href    = TargetSink[0].href;
}

Solution

  • var TargetLink              = $("table a:contains('gg')");
    var TargetSink              = $("table a:contains('u')");
    

    EDIT: You say there is only one table on the page. Do you absolutely know there will only ever be one table? Even if you think the answer is yes, I would try and add an id or class selector so that things won't break in the future.

    Also, the following code can be simplified:

    if (TargetLink  &&  TargetLink.length)
    

    to:

    if (TargetLink.length)
    



    Re: "could I combine those 2 variables into 1":

    Use a comma in the selector, like so:

    //--- Need more of the HTML structure for a better selector.
    var TargetLink              = $("table")
                                .find ("a:contains('gg'), a:contains('u')")
                                ;
    if (TargetLink.length) {
        window.location.href    = TargetLink[0].href;
    } 
    

    If both kind of links are found, 'gg' will be used (first).