我不是真正的编码员;我可以破解一些脚本而不是更多。像真正的NEWBIE一样跟我说话。 ; - )
我想使用GM脚本自动展开页面上的两个链接。 这是一个法国交友网站,在个人资料部分,您有两个地方,默认情况下只显示部分信息。因此,要获得所有信息,您需要点击另外两个链接。
以下是包含我想要触发的链接的页面的屏幕截图:
目标网页为reseaucontact.com/profil/3604181
重要提示:当我测试脚本时,我已登录网站...所以,如果您点击链接&您尚未登录,该网站将以法语告诉您“注册以查看完整内容”。
我尝试创建一个脚本来打开第一个链接,但由于链接的URL是页面(reseaucontact.com/profil/3604181
)页面刚刚在无限循环中重新加载的SAME URL; - )
这是我第一次尝试制作一个脚本结果是一个无限循环:
// ==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
这是我收集的Firefox Web Developer工具(当我登录时)找到目标/链接时的数据/打印屏幕:
第一个链接:
第二个链接:
据我所知,当我点击2个链接时,它是一个GET命令,用于“身份验证请求”或类似的东西...但我不明白如何制作一个真正“模仿”鼠标点击的脚本来扩展配置文件的2个部分,每次都没有单击。
我很确定它很简单/容易,但是在尝试使用那种“链接”找到信息4小时后我需要帮助。
如果您需要更多信息或澄清,请问我。
答案 0 :(得分:1)
这些是带有href
虚拟值的AJAX驱动链接。点击该链接会触发javascript事件。
在这种情况下最简单的方法是发送javascript所期望的鼠标事件(通常但不总是click
事件)。请参阅Choosing and activating the right controls on an AJAX-driven site。
另外,实际上,让GM脚本立即触发两个jQuery-AJAX驱动的链接,就像那两个一样,可能遇到计时或“竞争”问题。对于像这样的网站,我建议稍微延迟点击之前。使用setTimeout()
Doc表示延迟。
总而言之,完整的脚本可能就像:
// ==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);
}
如果需要,请调整延迟,并注意网站使用比click
更多或不同的事件的可能性很小。
答案 1 :(得分:0)
如果单击这些截断链接的功能是扩展元素或使用AJAX调用获取更多内容或只是在下面显示隐藏元素,那么您可能根本不需要链接的HREF,因为它可能会阻止跟随href到新URL的默认行为。
而不是搞乱href尝试触发这些链接上的click事件:
TargetLink.click();