簡體   English   中英

我的Greasemonkey腳本如何自動重新加載頁面並自動單擊按鈕?

[英]How can my Greasemonkey script auto-reload a page, and auto-click a button?

我是JavaScript的初學者。 我需要知道這是否可能,然后知道如何做到。

我想創建一個Greasemonkey腳本,該腳本將每小時自動重新加載網頁,然后在重新加載頁面后,我希望它為我單擊一個按鈕。

那可能嗎?

  • 有關單擊內容的信息,請參見此答案

  • 另請參閱javascript參考,以獲取location.reload()

  • 另請參見setTimeout()的更好的javascript參考

  • 由於按鈕可能是通過AJAX加載的(需要更多信息),因此請參考waitForKeyElements()實用程序 ,以處理AJAX延遲/修改。

  • 開始學習jQuery ; 它將為您節省大量的悲傷和精力。

  • 確定所需按鈕的jQuery選擇器。 您可以使用Firebug之類的工具來解決此問題(請注意,jQuery選擇器和CSS選擇器基本相同,但是jQuery具有更多選項/功能)。

    例如,如果頁面的HTML具有如下部分:

     <div id="content"> <p>Blah, blah, blather...</p> <h2>Your available actions:</h2> <button class="respBtn">Like</button> <button class="respBtn">Hate</button> <button id="bestOption" class="respBtn">Nuke them all</button> </div> 

    然后,這三個按鈕的選擇器可能分別是:

    1. $("#content button.respBtn:contains('Like')");
    2. $("#content button.respBtn:contains('Hate')");
    3. $("#bestOption"); (如果元素有一個,則始終使用id 。)


放在一起,這是一個完整的Greasemonkey腳本 ,可以重新加載並單擊:

// ==UserScript==
// @name     _Reload and click demo
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @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.
*/

waitForKeyElements ("#bestOption", clickTargetButton);

function clickTargetButton (jNode) {
    var clickEvent = document.createEvent ('MouseEvents');
    clickEvent.initEvent ('click', true, true);
    jNode[0].dispatchEvent (clickEvent);
}

//--- Reload after 1 hour (1000 * 60 * 60 milliseconds)
setTimeout (location.reload, 1000 * 60 * 60);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM