您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
在YouTube影片載入後的6秒內檢測是否暫停,如果暫停則自動刷新頁面。每2秒檢查一次,最多檢查3次。在主頁和搜索結果頁面不執行。
// ==UserScript== // @name YouTube 影片自動刷新(僅在前6秒內檢查3次,排除主頁和搜索頁面) // @namespace http://tampermonkey.net/ // @version 2.0 // @license MIT // @description 在YouTube影片載入後的6秒內檢測是否暫停,如果暫停則自動刷新頁面。每2秒檢查一次,最多檢查3次。在主頁和搜索結果頁面不執行。 // @match https://www.youtube.com/* // @grant none // ==/UserScript== (function() { 'use strict'; // 檢查是否在主頁或搜索頁面 if (window.location.href === "https://www.youtube.com/" || window.location.href.includes("https://www.youtube.com/results?search_query=")) { return; // 如果是主頁或搜索頁面,直接結束腳本執行 } function checkVideoStatus(videoId) { // 檢查是否已經對該影片執行過檢查 if (sessionStorage.getItem('checked_' + videoId)) { return; // 如果已檢查過,直接返回 } const video = document.querySelector('video'); if (video) { // 如果影片已經播放超過6秒,不執行檢查 if (video.currentTime > 6) { sessionStorage.setItem('checked_' + videoId, 'true'); return; } let checkCount = 0; const checkInterval = setInterval(() => { checkCount++; if (checkCount > 3 || video.currentTime > 10) { clearInterval(checkInterval); sessionStorage.setItem('checked_' + videoId, 'true'); return; } if (video.paused) { // 如果影片處於暫停狀態,刷新頁面 clearInterval(checkInterval); location.reload(); } }, 2000); // 每2秒檢查一次 // 如果影片開始播放,停止檢查並標記為已檢查 video.addEventListener('play', () => { clearInterval(checkInterval); sessionStorage.setItem('checked_' + videoId, 'true'); }, { once: true }); } } function getVideoId(url) { const urlParams = new URLSearchParams(new URL(url).search); return urlParams.get('v'); } function handleUrlChange() { const currentUrl = window.location.href; if (currentUrl.includes('watch?v=')) { const videoId = getVideoId(currentUrl); if (videoId) { // 重置檢查狀態 sessionStorage.removeItem('checked_' + videoId); checkVideoStatus(videoId); } } } // 使用 MutationObserver 監聽 URL 變化 const observer = new MutationObserver((mutations) => { for (let mutation of mutations) { if (mutation.type === 'childList') { handleUrlChange(); break; } } }); observer.observe(document.body, { childList: true, subtree: true }); // 初始檢查 handleUrlChange(); })();