Search code examples
javascriptjqueryuserscripts

How can my userscript select this link based on the contained image src?


I'd like to write a userscript automate a click on a certain image on a web page. The target URL is dynamic, but the image name is fixed.

So far I have the following Userscript

//--- Note that the contains() text is case-sensitive.
var TargetLink          = $("a:contains('Click link.jpg')")

if (TargetLink  &&  TargetLink.length) 
    window.location.href    = TargetLink[0].href

Following is an extract of the web page, which I need a user script

<a target="_blank" href="http://www.movshare.net/video/0zq2u9732nvdf"><img border="0" src="http://img.movie2k.to/img/click_link.jpg" alt="view Rise of the Guardians" title="view Rise of the Guardians" width="742"></a>

PS: alt and title of the image attributes are dynamic. Img SCR is fixed.


Solution

    1. :contains() searches for text; it will not match against the image source.
    2. The image source does not contain Click link.jpg. It contains click_link.jpg. The underscore and correct case are critical.

    Given that, this code will select the right link:

    var TargetLink = $("a:has(img[src*='click_link.jpg'])");