admin管理员组文章数量:1415467
i have a link that i need to click using greasemonkey
<a href="/attack/index/1016587">Attack This Player</a>
that is the code
the thing is that the href is changeable depending on the page
and the only static code is the text of 'attack this player
i currently have
function click_element(element)
{
var event = document.createEvent("MouseEvents");
event.initMouseEvent("click", true, true, document.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
element.dispatchEvent(event);
return;
};
click_element( document.evaluate("//a[I DON'T KNOW WHAT TO PUT HERE]",document,null,9,null).singleNodeValue );
this currently works for links with ids, classes e.t.c. (sorry for all caps but its just to draw attention to the area)
thanks for any help i am not that advanced in JavaScript
Reblerebel
i have a link that i need to click using greasemonkey
<a href="/attack/index/1016587">Attack This Player</a>
that is the code
the thing is that the href is changeable depending on the page
and the only static code is the text of 'attack this player
i currently have
function click_element(element)
{
var event = document.createEvent("MouseEvents");
event.initMouseEvent("click", true, true, document.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
element.dispatchEvent(event);
return;
};
click_element( document.evaluate("//a[I DON'T KNOW WHAT TO PUT HERE]",document,null,9,null).singleNodeValue );
this currently works for links with ids, classes e.t.c. (sorry for all caps but its just to draw attention to the area)
thanks for any help i am not that advanced in JavaScript
Reblerebel
Share Improve this question edited Jun 16, 2011 at 23:05 lunixbochs 22.4k2 gold badges41 silver badges48 bronze badges asked Jun 16, 2011 at 22:51 reblerebelreblerebel 432 silver badges8 bronze badges 1- 1 use the {} to make your code pretty when you post it in a question or answer – lunixbochs Commented Jun 16, 2011 at 22:56
1 Answer
Reset to default 6Rather than use XPath, use jQuery -- it will be simpler.
If clicking that link merely takes you to the appropriate page then the whole script could be as simple as:
// ==UserScript==
// @name Attack!
// @include http://YOUR_SITE/YOUR_PATH/*
// @require http://ajax.googleapis./ajax/libs/jquery/1.5.1/jquery.min.js
// ==/UserScript==
//--- contains is case-sensitive.
var attackLink = $("a:contains('Attack This Player')");
//--- "Click" the link the easy way.
window.location.href = attackLink[0].href;
Or, if the link action is controlled by the page's JavaScript (doesn't seem likely based on the example link given):
// ==UserScript==
// @name Attack!
// @include http://YOUR_SITE/YOUR_PATH/*
// @require http://ajax.googleapis./ajax/libs/jquery/1.5.1/jquery.min.js
// ==/UserScript==
//--- contains is case-sensitive.
var attackLink = $("a:contains('Attack This Player')");
//--- Click the link if its action was overridden by JavaScript.
var evt = document.createEvent ("HTMLEvents");
evt.initEvent ("click", true, true);
attackLink[0].dispatchEvent (evt);
Update for ment(s):
Is there always just one link? Do the links always start with "/attack/index/"?
If so, then you can use:
var attackLink = $("a[href^='/attack/index/']");
If it's more plicated, open another question and give full details of the target pages' code. Links to the pages are best.
Selecting elements of a target page is an art and highly page specific.
jQuery makes it easier with CSS-type selection; Here's a handy jQuery selector reference.
本文标签: javascriptClicking a link with greasemonkey from the content inside linkStack Overflow
版权声明:本文标题:javascript - Clicking a link with greasemonkey from the content inside link - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745161353a2645454.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论