繁体   English   中英

Greasemonkey脚本。 如何自动单击之间的空格数量未知的链接

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

我想要一个可以自动单击具有特定单词的链接的润滑脂脚本。 在这种情况下,我希望脚本单击显示“单击此处接受!”的链接。 我找到了一个可行的脚本,但是,我想在其上使用的网站链接异常。 在“此处”和“至”之间有未知数量的空格

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

我发现此脚本似乎在具有正常链接的网站上有效。 但是,我怀疑由于未知数量的空格,脚本无法在上述链接上运行。

// ==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

我是编程新手,对Java语言的了解非常有限。 请帮助我找到解决方案。 任何帮助将不胜感激。

谢谢。

在Scriptish中,您可以使用简化的XPath函数:

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

当您想要获得所有事件的列表时,请使用:

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

并遍历返回的链接。 记住要在元数据头中@grant GM_xpath

填充工具
当用户脚本引擎的API不提供GM_xpath时,可以使用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;
  };

您可以使用简单的正则表达式过滤链接:

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

或者,您可以这样:

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

但这将匹配,即使单词的顺序不符合要求也是如此。 我认为正则表达式方法更好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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