如果按钮在一段时间后没有显示,请单击特定按钮或刷新页面

时间:2017-11-13 14:56:14

标签: javascript greasemonkey tampermonkey

我已经遵循了这个主题并且非常出色:How do I click on this button with Greasemonkey?

但我需要Greasemonkey上的脚本:

  1. 启动脚本
  2. 每个1分钟的脚本重新加载页面(所以,* setTimeout(function(){location.reload();},60 * 1000); *)
    • 如果重新加载后,页面上会出现带有文字" Eureka"的按钮,点击按钮并结束脚本
    • 如果没有出现带有文字" Eureka"的按钮,再次重新加载页面,检查具有该特定文本的按钮,如果仍然没有出现重新加载页面等
  3. 在文本出现时单击按钮的代码脚本,它可以正常工作:

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

1 个答案:

答案 0 :(得分:0)

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

  1. 等待按钮1分钟。
  2. 如果找到了,请点击它。
  3. 否则重新加载页面。
  4. 如果这是真的,那么可以使用简化逻辑 但是,如果单击按钮也会导致页面本身重新加载,或者正在进行其他操作,那么您需要使用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.
        }
    }