简体   繁体   中英

Greasemonkey scripting. How to automatically click on a link that has unknown amount of white space in between

I want a greasemonkey script that can automatically click on the a link that has specific words. In this case, I want the script to click on the link that says "Click Here To Accept!" I found a script that works, however, the website I wanted to use it on has an abnormal link. It has unknown amount of white spaces between the word 'Here' and the word 'To'

<a
    href="accept.php?eSessID=7773160788107402312135408908242639440395021097128232706420140705121030&eSignupID=413&eEventID=102&sunow=1&ceRef=MOBILE"><b><u>Click Here
    To Accept!</u></b></a>

I found this script is seems to work on website that has a normal link. However, I suspect the script doesn't work on the above link because of the unknown amount of white spaces.

// ==UserScript==
// @name Auto Click
// @version 1.0
// @author Joe
// @include  http://website.com/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/

//--- Note that the contains() text is case-sensitive.
var TargetLink = $("a:contains('Click Here To Accept!')")

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

I am a newbie at programming and have very limited knowledge in Javascripts. Please help me to find a solution. Any help would be greatly appreciated.

Thank you.

Joe

In Scriptish you could use the simplified XPath function:

GM_xpath("//*[normalize-space()='Click Here To Accept!']//ancestor::a").click();

When you want to get a list of all occurrences use:

var myLinks = GM_xpath({path:"//*[normalize-space()='Click Here To Accept!']//ancestor::a", all:true});

and iterate over the returned links. Remember to @grant GM_xpath in the metadata header.

Polyfill
When the API of your userscript engine does not provide GM_xpath, you can implement it with document.evaluate() :

if(!this.GM_xpath)
  this.GM_xpath = function(descriptor)
  {
    if(descriptor === null || typeof descriptor === 'undefined')
      throw new TypeError('GM_xpath(): Descriptor must not be null or undefined.');

    var
      result, item, args,
      array = []
    ;

    args = descriptor.constructor.name === 'String'
      ? {path: descriptor, all:false}
      : {path: descriptor.path, all: descriptor.all}
    ;

    result = document.evaluate
    (
      args.path,
      document,
      document.createNSResolver(document),
      args.all ? XPathResult.ORDERED_NODE_ITERATOR_TYPE : XPathResult.FIRST_ORDERED_NODE_TYPE,
      null
    );

    if(!args.all)
      return result.singleNodeValue;

    while(item = result.iterateNext())
      array.push(item);
    return array;
  };

You can filter the links with a simple regex:

var targetLink = $("a").filter(function() {
    return /click\s+here\s+to\s+accept/i.test($(this).text());
});

Or you could go this way:

var targetLink = $("a:contains('Click'):contains('Here'):contains('To'):contains('Accept')");

But this will match even if the words aren't in the required order. I think the regex method is better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM