![](/img/trans.png)
[英]How to make Greasemonkey click automatically on a “weird” link on a specific site?
[英]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.