[英]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. 我是编程新手,对Java语言的了解非常有限。 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: 在Scriptish中,您可以使用简化的XPath函数:
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. 记住要在元数据头中@grant GM_xpath
。
Polyfill 填充工具
When the API of your userscript engine does not provide GM_xpath, you can implement it with document.evaluate()
: 当用户脚本引擎的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;
};
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. 我认为正则表达式方法更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.