需求:开发Tampermonkey脚本实现定时点击刷新按钮并可暂停
Hey there, I've put together a Tampermonkey script that fits your exact needs—auto-clicking the "Refresh" button every minute, plus an optional pause feature when you're interacting with the page. Let's break this down:
Complete Tampermonkey Script
// ==UserScript== // @name Auto-Refresh Customer Requests // @namespace http://tampermonkey.net/ // @version 0.1 // @description Auto-clicks Refresh button every minute, pauses on user input (optional) // @author Your Name // @match https://your-work-website-url-here.com/* // Replace with your actual work site URL // @grant none // ==/UserScript== (function() { 'use strict'; // Configuration const REFRESH_INTERVAL = 60000; // 1 minute in milliseconds const RESUME_DELAY = 30000; // Resume auto-refresh 30 seconds after last user input (optional) let refreshIntervalId = null; let isPaused = false; let resumeTimeoutId = null; // Function to locate the Refresh button - adjust selector based on your page function findRefreshButton() { // Try multiple ways to find the button - pick what works for your site const buttonByText = Array.from(document.querySelectorAll('button')).find(btn => btn.textContent.trim().toLowerCase() === 'refresh'); if (buttonByText) return buttonByText; const buttonById = document.getElementById('refresh-button'); // If button has an ID if (buttonById) return buttonById; const buttonByClass = document.querySelector('.refresh-btn'); // If button has a specific class if (buttonByClass) return buttonByClass; // If none found, log a message to console console.log('Refresh button not found - adjust the selector in the script'); return null; } // Function to trigger refresh function triggerRefresh() { if (isPaused) return; const refreshBtn = findRefreshButton(); if (refreshBtn) { refreshBtn.click(); console.log('Auto-refreshed at:', new Date().toLocaleTimeString()); } } // Pause auto-refresh when user interacts with the page function pauseRefresh() { isPaused = true; console.log('Auto-refresh paused due to user input'); // Clear existing resume timeout if any if (resumeTimeoutId) clearTimeout(resumeTimeoutId); // Resume after RESUME_DELAY if no further user input resumeTimeoutId = setTimeout(() => { isPaused = false; console.log('Auto-refresh resumed'); }, RESUME_DELAY); } // Initialize auto-refresh function initAutoRefresh() { refreshIntervalId = setInterval(triggerRefresh, REFRESH_INTERVAL); console.log('Auto-refresh started - interval:', REFRESH_INTERVAL / 1000, 'seconds'); } // Add event listeners for user input (optional - comment out to disable pause feature) const userEvents = ['keydown', 'input', 'click', 'scroll', 'touchstart']; userEvents.forEach(event => { document.addEventListener(event, pauseRefresh, true); }); // Start the auto-refresh when the script loads initAutoRefresh(); // Optional: Add a manual toggle button to control pause/resume function createToggleButton() { const toggleBtn = document.createElement('button'); toggleBtn.textContent = 'Pause Auto-Refresh'; toggleBtn.style.position = 'fixed'; toggleBtn.style.top = '10px'; toggleBtn.style.right = '10px'; toggleBtn.style.padding = '8px 12px'; toggleBtn.style.zIndex = '9999'; toggleBtn.style.backgroundColor = '#ff4444'; toggleBtn.style.color = 'white'; toggleBtn.style.border = 'none'; toggleBtn.style.borderRadius = '4px'; toggleBtn.style.cursor = 'pointer'; toggleBtn.addEventListener('click', () => { isPaused = !isPaused; toggleBtn.textContent = isPaused ? 'Resume Auto-Refresh' : 'Pause Auto-Refresh'; toggleBtn.style.backgroundColor = isPaused ? '#4CAF50' : '#ff4444'; console.log(isPaused ? 'Auto-refresh manually paused' : 'Auto-refresh manually resumed'); }); document.body.appendChild(toggleBtn); } // Uncomment below to enable the manual toggle button // createToggleButton(); })();
Key Features Explained
- Auto-Refresh Core: Uses
setIntervalto trigger a button click every 60 seconds. It checks if the script is paused before executing the click, so you don't get unwanted refreshes when you're actively working. - Smart Pause (Optional): Listens for common user interactions (typing, clicking, scrolling, etc.) and pauses the auto-refresh automatically. It resumes after 30 seconds of inactivity—you can tweak the
RESUME_DELAYvalue to make this shorter or longer. - Flexible Button Detection: The script tries multiple methods to find your Refresh button (by text, ID, or class). If it can't locate the button, it logs a message to your browser's console—just adjust the selectors in
findRefreshButton()to match your work site's button. - Manual Toggle (Optional): There's a commented-out function to add a floating toggle button on the page. Uncomment the
createToggleButton();line if you want to manually pause/resume auto-refresh whenever you need to.
Setup Instructions
- Open Tampermonkey in your browser and create a new script.
- Replace the default code with the script above.
- Update the
@matchline to your work website's URL (use a wildcard likehttps://your-site.com/*to cover all pages on the site). - Adjust the button selectors in
findRefreshButton()if the script can't locate the Refresh button (check the browser console for a message if this happens). - Save the script—it will automatically run when you visit your work site.
内容的提问来源于stack exchange,提问作者Dante




