我已经遵循了这个主题并且非常出色:How do I click on this button with Greasemonkey?
但我需要Greasemonkey上的脚本:
在文本出现时单击按钮的代码脚本,它可以正常工作:
// ==UserScript==
// @name _Click on a specific link
// @include http://www.mypage.com*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a major design change
introduced in GM 1.0.
It restores the sandbox.
*/
//--- Note that contains() is CASE-SENSITIVE.
waitForKeyElements ("span:contains('Eureka')", clickOnFollowButton);
function clickOnFollowButton (jNode) {
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
jNode[0].dispatchEvent (clickEvent);
}
我想我需要使用条件;但我不知道在哪里以及如何。 :(
我试过了:
// ==UserScript==
// @name _Click on a specific link
// @include http://www.mypage.com*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a major design change
introduced in GM 1.0.
It restores the sandbox.
*/
//--- Note that contains() is CASE-SENSITIVE.
var FREQUENCY = 60*1000;
function clickOnFollowButton (jNode) {
if ("span:contains('Eureka')") {
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
jNode[0].dispatchEvent (clickEvent);
}
else {
window.setTimeout(window.location.reload, FREQUENCY);
}
}
答案 0 :(得分:0)
我可以将问题的陈述归结为:
如果这是真的,那么可以使用简化逻辑
但是,如果单击按钮也会导致页面本身重新加载,或者正在进行其他操作,那么您需要使用GM_setValue
将状态信息从一个脚本实例传输到下一个。 (每次重新加载都会启动脚本,与新页面加载相同。)
接下来,第二个代码块由于某些原因而无法工作,它会立即检查元素,而不是允许它进入AJAX-in。它需要检查计时器运行的 end 。
最简单的方法是使用全局状态变量。
这样的代码应该基于这个问题起作用,如果你没有遗漏"但是"上面列出的标准:
// ==UserScript==
// @name _Click on a specific link, refresh if it doesn't appear
// @match *://YOUR_SERVER.COM/YOUR_PATH/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_addStyle
// ==/UserScript==
//- The @grant directive is needed to restore the proper sandbox.
const reloadFrequency = 60*1000; // 60 seconds
let buttonWasFound = false;
//--- Note that contains() is CASE-SENSITIVE.
waitForKeyElements ("span:contains('Eureka')", clickOnFollowButton);
setTimeout (reloadOnlyIfButtonMissing, reloadFrequency);
function clickOnFollowButton (jNode) {
buttonWasFound = true;
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
jNode[0].dispatchEvent (clickEvent);
}
function reloadOnlyIfButtonMissing () {
if ( ! buttonWasFound) {
location.reload; // This kills script instance.
}
}