【问题标题】:Click on a specific button or refresh page if button doesn't appear after a while如果按钮在一段时间后没有出现,请单击特定按钮或刷新页面
【发布时间】:2017-11-13 14:56:14
【问题描述】:

我关注了这个帖子,非常棒:How do I click on this button with Greasemonkey?

但是我需要一个关于 Greasemonkey 的脚本来:

  1. 启动脚本
  2. 每 1 分钟脚本重新加载页面(因此,*setTimeout(function(){ location.reload(); }, 60*1000);* )
    • 如果重新加载后,页面上出现带有文本“Eureka”的按钮,请单击按钮并结束脚本
    • 如果没有出现带有文本“Eureka”的按钮,再次重新加载页面,检查带有特定文本的按钮,如果仍然没有出现重新加载页面等

文本出现时单击按钮的代码脚本,它可以正常工作:

// ==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);
    }
}

【问题讨论】:

    标签: javascript greasemonkey tampermonkey


    【解决方案1】:

    我可以将问题的陈述逻辑归结为:

    1. 等待按钮 1 分钟。
    2. 如果找到,请单击它。
    3. 否则重新加载页面。

    如果是这样,那么可以使用简化的逻辑。
    但是,如果单击按钮也会导致页面本身重新加载,或者发生其他事情,那么您将需要使用GM_setValue 将状态信息从一个脚本实例传输到下一个脚本实例。 (每次重新加载都会重新启动脚本,与新页面加载相同。)

    接下来,由于一些原因,第二个代码块不起作用,它会立即检查元素,不允许它进行 AJAX 输入。它需要在计时器运行的结束处检查。

    最简单的方法是使用全局状态变量。

    根据问题,这样的代码应该可以工作,前提是您没有省略上面列出的“但是”标准:

    // ==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.
        }
    }
    

    【讨论】:

    • 非常感谢 Brock,脚本有效,但我不明白是否有刷新页面。如果找不到按钮,似乎不会刷新页面。相反,如果找到按钮,它会单击并正常工作。单击按钮时,页面本身不会重新加载,但会转到另一个页面(按钮是链接),所以我认为不需要 GM_setValue 。但是当找不到按钮时,也许我需要它来刷新页面?
    • 您没有向我们展示的东西是问题所在。在未刷新的页面上,在控制台中键入 jQuery("span:contains('Eureka')").length。结果如何?
    猜你喜欢
    • 2014-04-10
    • 2014-01-20
    • 1970-01-01
    • 2013-10-10
    • 2015-07-05
    • 2016-07-12
    • 1970-01-01
    • 1970-01-01
    • 2020-03-13
    相关资源
    最近更新 更多