简体   繁体   English

如何使Greasemonkey自动单击特定站点上的“怪异”链接?

[英]How to make Greasemonkey click automatically on a “weird” link on a specific site?

I'm not really a coder; 我不是真正的编码员; I can hack some scripts but no more. 我可以修改一些脚本,但不能再修改了。 Talk to me like a REAL NEWBIE. 像真正的NEWBIE跟我说话。 ;-) ;-)

I want to automatically expand two links on a page, using a GM Script. 我想使用GM脚本自动展开页面上的两个链接。 It's a French dating site and, in the profile section, you have two places where the default is to show only partial information. 这是一个法国交友网站,在个人资料部分,您有两个位置,默认位置仅显示部分信息。 So, to have all the information, you need to click on 2 additional links. 因此,要获取所有信息,您需要单击2个其他链接。

Here's a screenshot of the page with the links that I want to trigger: 这是该页面的屏幕截图,其中包含我要触发的链接:

(Click for larger image) (点击查看大图)
Screenshot of links


The target page is reseaucontact.com/profil/3604181 目标页面是reseaucontact.com/profil/3604181

IMPORTANT: When I was testing the Script I was logged in the site... so if you click on the link & you are NOT logged-in, the site will tell you "Register to see full content" in French. 重要信息:当我测试脚本时,我已登录网站...因此,如果您单击链接而您尚未登录,则该网站将以法语告诉您“注册以查看全部内容”。

I tried to make a Script to open the first link but since the URL of the link is the SAME URL that the page ( reseaucontact.com/profil/3604181 ) the page was just reloading in an infinite loop ;-) 我试图制作一个脚本来打开第一个链接,但是由于链接的URL是页面( reseaucontact.com/profil/3604181 )只是在无限循环中重新加载的相同URL ;-)

Here's my First attempt to make a script the result was an infinite loop: 这是我第一次尝试使脚本的结果是无限循环:

// ==UserScript==
// @name Reseau Contact Automatic Expander
// @version 1.0
// @author Mikha
// @include  http://reseaucontact.com/profil/*
// @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('Voir tous les détails')")

if (TargetLink.length)
    window.location.href = TargetLink[0].href


Here's the data/print-screen from Firefox Web Developer tools I have collected (When I'm logged) to find the targets/links: 这是我(在登录时)收集的Firefox Web Developer工具中的数据/打印屏幕,用于查找目标/链接:

The first link: 第一个链接:

(Click for larger image) (点击查看大图)
First link HTML


The second link: 第二个链接:

(Click for larger image) (点击查看大图)
First link HTML


I understand that when I click on the 2 links it's a GET command for "authentication request" or something like that... but I don't understand how make a script that will really "mimic" the click of my mouse to make expand the 2 sections of the profile without clicking each time. 我了解,当我单击2个链接时,这是一个用于“身份验证请求”或类似内容的GET命令...但是我不明白如何制作一个能够真正“模仿”鼠标单击以进行扩展的脚本个人资料的2个部分,无需每次都单击。

I'm pretty sure it is simple/easy to do but after 4 hours of trying to find the information with that "kind of links" I need helps. 我敢肯定这很简单/容易,但是经过4个小时的尝试,我发现需要“那种链接”的信息。

If you need more information or clarification just ask me. 如果您需要更多信息或澄清,请问我。

These are AJAX-driven links with dummy values in the href . 这些是AJAX驱动的链接,其中的href有虚拟值。 Clicking the link fires off javascript events. 点击链接会触发javascript事件。

The easiest thing to do in this case is to send the mouse event(s) that the javascript expects (usually, but not always a click event). 在这种情况下,最简单的方法是发送javascript期望的鼠标事件(通常,但并不总是click事件)。 See Choosing and activating the right controls on an AJAX-driven site . 请参阅在AJAX驱动的网站上选择和激活正确的控件

Also, as a practical matter, having a GM script immediately fire on two jQuery-AJAX driven links, like those two are, might run into timing or "race" problems. 另外,实际上,让GM脚本立即在两个jQuery-AJAX驱动的链接上触发,就像这两个一样, 可能会遇到计时或“竞赛”问题。 For sites like this, I recommend a slight delay before clicking. 对于这样的网站,建议您在点击之前稍加延迟。 Use setTimeout() Doc for the delay. 使用setTimeout() Doc作为延迟时间。

Putting it all together, the complete script could would be like: 放在一起,完整的脚本可能像:

// ==UserScript==
// @name     Reseau Contact Automatic Expander
// @version  1.0
// @author   Mikha
// @include  http://reseaucontact.com/profil/*
// @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.
*/

//-- Delay the link clicks 333 mS and 777 mS, respectively.
setTimeout (clickLinkWithText, 333, "Voir tous les détails");
setTimeout (clickLinkWithText, 777, "Lire la suite");

function clickLinkWithText (linkText) {
    var targetLink = $("a:contains('" + linkText + "')");
    if (targetLink.length) {
        triggerMouseEvent (targetLink[0], "click");
    }
}

function triggerMouseEvent (node, eventType) {
    var clickEvent = document.createEvent('MouseEvents');
    clickEvent.initEvent (eventType, true, true);
    node.dispatchEvent (clickEvent);
}


Adjust the delays if needed, and beware that there is a small chance that the site uses more, or different, events than click . 如果需要,请调整延迟时间,并注意该站点使用的事件比click或不同的可能性很小。

If the functionality of clicking on these truncated links is to expand the element or grab more content with an AJAX call or just show a hidden element below, then you don't probably need the link's HREF at all since its probably preventing the default behavior of following the href to a new URL. 如果单击这些截断的链接的功能是扩展元素或通过AJAX调用获取更多内容,或者仅在下面显示隐藏元素,则您根本不需要链接的HREF,因为它可能阻止了默认行为。在href后面跟随一个新的URL。

Instead of messing with the href try triggering the click event on these links instead: 与其惹上href,不如尝试在这些链接上触发click事件:

TargetLink.click();

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

相关问题 如何使Greasemonkey单击具有特定文本的链接? - How do I make Greasemonkey click a link that has specific Text? 你如何让 Greasemonkey 单击具有指定文本的链接? - How do you make Greasemonkey Click a link that has specified text? 如何使用Greasemonkey脚本自动单击AJAX按钮? - How to automatically click the AJAX button with a Greasemonkey script? Greasemonkey脚本。 如何自动单击之间的空格数量未知的链接 - Greasemonkey scripting. How to automatically click on a link that has unknown amount of white space in between 使用greasemonkey自动单击一个按钮 - click a button automatically using greasemonkey 如何使用Greasemonkey自动选择特定的单选按钮? - How can I automatically select specific radio buttons with Greasemonkey? 如何制作将在iframe链接内自动点击的JavaScript? - How to make javascript that'll click automatically inside iframe's link? 如何单击GreaseMonkey - How to click in GreaseMonkey 如何使用GreaseMonkey中的javascript模拟具有特定文本的锚点单击? - How to simulate click on anchor with specific text using javascript in GreaseMonkey? Greasemonkey脚本自动选择并单击值/选择器中包含非英文字符的特定按钮? - Greasemonkey script to automatically select, and click, a specific button with non-English characters in the value/selector?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM