从链接内的内容中单击一个带有润滑脂猴子的链接 [英] Clicking a link with greasemonkey from the content inside link

查看:53
本文介绍了从链接内的内容中单击一个带有润滑脂猴子的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个链接,我需要使用滑脂猴子点击 <a href="/attack/index/1016587">Attack This Player</a> 那是代码 问题是href随页面而变化 而唯一的静态代码是攻击此播放器

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

我目前有

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 );

这目前适用于ID为e.t.c的ID的链接. (对不起,所有大写字母不过是为了吸引人们注意该区域)

this currently works for links with ids, classes e.t.c. (sorry for all caps but its just to draw attention to the area)

感谢您的帮助 我的JavaScript不够先进

thanks for any help i am not that advanced in JavaScript

Reblerebel

Reblerebel

推荐答案

使用jQuery而不是使用XPath,这样会更简单.

Rather 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.com/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;


或者,如果链接操作是由页面的JavaScript控制的(根据给出的示例链接看来不太可能):


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.com/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);


更新评论:

是否总是只有一个链接?链接是否始终以"/attack/index/"开头?


Update for comment(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 complicated, open another question and give full details of the target pages' code. Links to the pages are best.

选择目标页面的元素是一种艺术,并且是特定于页面的.
jQuery使CSS类型选择变得更加容易.这是方便的jQuery选择器参考.

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.

这篇关于从链接内的内容中单击一个带有润滑脂猴子的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆