简体   繁体   English

如何使用Greasemonkey检测页面中特定链接的点击?

[英]How to detect clicks on specific links in page with Greasemonkey?

Ok... one of my virtual pet sites was down so I decided to tweak a Greasemonkey script for another site... the original script just pops up an alert when you find an egg so you don't click past it - I thought it would be simple to count how many eggs I came across. 好的...我的一个虚拟宠物站点已关闭,所以我决定为另一个站点调整Greasemonkey脚本...当您找到一个鸡蛋时,原始脚本会弹出一个警报,因此您不要单击它-我想计算我遇到的鸡蛋数量很简单。 After 2 hours I finally found a way to keep a variable past page reloads - SUCCESS!! 2小时后,我终于找到了一种方法来保持页面重新加载后的变量-成功! Then I decided I wanted to keep track of how many "steps" I was taking... ARGGGG!!!! 然后,我决定要跟踪我正在执行的“步骤” ... ARGGGG !!!! Should be simple but isn't! 应该很简单,但事实并非如此!
On the page there are three possible explore links to click (shows image not link). 在页面上,有三个可能的浏览链接可供单击(显示图像而不是链接)。 3 hours later I found the event listener and can make an alert popup when I click anywhere on the page, but how do I check for clicks only on 1 of those 3 links? 3小时后,我找到了事件侦听器,当我单击页面上的任意位置时可以弹出警报,但是如何仅检查这3个链接中的1个单击? All 3 links have the same general format: http://unicreatures.com/explore.php?area=***&id=***&key=**** where the * are changeable. 所有3个链接的通用格式都相同: http://unicreatures.com/explore.php?area=***&id=***&key=**** : http://unicreatures.com/explore.php?area=***&id=***&key=**** * id= * & http://unicreatures.com/explore.php?area=***&id=***&key=**** ,其中*可以更改。

Target page http://unicreatures.com/explore.php?area=grassland should be accessible without an account, if not let me know and I'll grab the source code. 目标页面http://unicreatures.com/explore.php?area=grassland应该没有帐户即可访问,如果没有让我知道,我将获取源代码。

// ==UserScript==
// @name Unicreatures Egg pop-up
// @namespace Unicreatures Egg pop-up
// @description Unicreatures Egg pop-up
// @include http://unicreatures.com/explore.php*
// @include http://www.unicreatures.com/explore.php*
// ==/UserScript==

var y = document.body.innerHTML;

//document.addEventListener('click', function(){alert('page clicked')}, true);

if(y.indexOf("You find a Noble")> 0)
{
alert('Noble Egg Alert');
}

if(y.indexOf("You find an Exalted")> 0)
{
localStorage.exaltedEggCount=Number(localStorage.exaltedEggCount)+1;
alert('Exalted Egg Alert '+localStorage.exaltedEggCount);
}

if(y.indexOf("egg nearby!")> 0)
{
localStorage.eggCount=Number(localStorage.eggCount)+1;

alert('Egg Alert '+localStorage.eggCount);
}

I do realize that the way the if statements are written that I get double alerts if either of the first 2 trigger(I didn't write that part, just made the alerts unique)... I'm fine with that for now. 我确实意识到,如果编写if语句的方式是在前两个触发器中的任何一个触发时都会得到双重警报(我没有编写该部分,只是使警报具有唯一性)...现在我可以了。 I did read something about get element id or something but I couldn't find it explained anywhere good enough for me to "get". 我确实读过一些有关获取元素ID的信息,但找不到适合我“获取”的地方。
Please javascript only, no jquery... I'm still barely getting javascript at the moment... please explain answers so I understand how something works - I don't just want code snippets that leave me coming back with a similar question because I don't understand why a bit of code was used. 请只使用javascript,不要使用jquery ...目前我仍然几乎没有使用javascript ...请解释答案,以便我了解某些工作原理-我不只是想要让我回想起类似问题的代码段,因为我不明白为什么要使用一些代码。

And one other question unless I should open another question for it... when you find an egg the text says You find an Exalted *** egg nearby or You find a *** egg nearby . 还有一个问题,除非我要再开一个问题...当您找到一个鸡蛋时,文字会显示You find an Exalted *** egg nearby或者You find a *** egg nearby You find an Exalted *** egg nearby You find a *** egg nearby Is there a way to grab the *** part from the page? 有没有办法从页面中获取***部分?

Thanks! 谢谢!

You can add event handlers to specific elements on a page. 您可以将事件处理程序添加到页面上的特定元素。 If the element has an id attribute, this is easy: 如果元素具有id属性,这很容易:

var element = document.getElementById( 'whatever' );

element.addEventListener( 'click', function () {
    alert( 'element clicked' );
}, true );

This will add the event listener to the element with the ID "whatever". 这会将事件侦听器添加到ID为“ whatever”的元素。

If the element doesn't have an ID attribute, you'll need to be a bit more creative. 如果该元素没有ID属性,则需要更具创意。 Here's one way to do it: 这是一种实现方法:

var links = document.getElementsByTagName( 'a' );

for ( var i = 0; i < links.length; i++ ) {
    var link = links[i];
    if ( /area=grassland/.test( link.href ) ) {
        link.addEventListener( 'click', function () {
             alert( 'grassland link clicked' );
        }, true );
    }
}

The /area=grassland/ is a regexp which matches strings that contain "area=grassland" as a substring. /area=grassland/是一个正则表达式 ,用于匹配包含“ area = grassland”作为子字符串的字符串。 (You can do a lot more with regexps if you want.) We test it against every link ( a element) on the page, and add the click handler to those whose href attribute matches the regexp. (如果需要,您可以对regexp进行更多处理 。)我们针对页面上的每个链接( a元素)对其进行测试,并将点击处理程序添加到href属性与regexp匹配的对象。

By the want, you can get the text inside a link with link.textContent , if you'd prefer to match against that. 如果需要,您可以根据需要在具有link.textContent的链接中获取文本。 It won't work so well with image links, though. 不过,它与图像链接的配合效果不佳。 (But you could always find, say, the first image tag inside the link with link.getElementsByTagName( 'img' )[0] and work from there.) (但是,您始终可以使用link.getElementsByTagName( 'img' )[0]找到链接中的第一个图像标签, link.getElementsByTagName( 'img' )[0]从那里开始工作。)


As for your second question, regexps are your friend there too: 至于第二个问题,正则表达式也是您的朋友:

var regexp = /You find an? (Exalted )?(.*?) egg nearby/;
var match = regexp.exec( document.body.textContent );
if ( match ) {
    if ( match[1] ) {
        alert( "Exalted egg found: " + match[2] );
    } else {
        alert( "Normal egg found: " + match[2] );
    }
}

See the links I gave above for how that regexp works and how to modify it to better suit your needs. 请参阅我在上面给出的链接,了解该正则表达式如何工作以及如何对其进行修改以更好地满足您的需求。

(Ps. Warning: I didn't actually test any of the code above, as I didn't feel like creating a trial account on the game site just for that. I hope it's correct, but there might be bugs or typos in it.) (警告:我实际上并没有测试上面的任何代码,因为我不想为此而在游戏网站上创建一个试用帐户。我希望它是正确的,但是其中可能存在错误或错别字。 )

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

相关问题 如何制作一个油脂猴子脚本,当按下某个键时,该脚本单击页面上的一个按钮? - How to make a greasemonkey script that clicks a button on page when a key is pressed? 转换jQuery代码以检测对具有特定类的链接的单击到本机JavaScript - Converting jQuery Code to detect clicks on links with a specific class to Native JavaScript 在一个页面中单击许多带有Greasemonkey脚本的链接? - Click many links, in one page, with a Greasemonkey script? 我的Greasemonkey脚本找不到页面上的元素(链接)吗? - My Greasemonkey script is not finding the elements (links) on the page? 跟踪将卸载页面的链接的点击 - tracking clicks on links that will unload the page 如何在某些链接上单击以转到目标页面? - How can I perform clicks on some links to get to the target page? 如何检测用户是否查看页面的javascript或单击浏览器中的检查 - How to detect if the user views a page's javascript or clicks on inspect in the browser GreaseMonkey / UserScript仅重新加载页面的特定div? - GreaseMonkey/UserScript to reload only a specific div of a page? 使用jQuery的Greasemonkey,如何编写隔油纸脚本来修改页面DOM元素? - Greasemonkey with jQuery, how to write the greasemonkey script to modify page DOM elements? 如何跟踪出站链接的点击 - How to track clicks on outbound links
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM