您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
This script automatically tracks and restores your YouTube playback position. This user script remembers where you left off in any video—allowing you to seamlessly continue watching even after navigating away or reloading the page. It saves your progress for each video for up to 3 days (configurable) and automatically cleans up outdated entries, ensuring a smooth and uninterrupted viewing experience every time.
当前为
// ==UserScript== // @name YouTube Auto-Resume // @icon https://www.youtube.com/img/favicon_48.png // @author ElectroKnight22 // @namespace electroknight22_youtube_auto_resume_namespace // @version 1.2.3 // @match *://*.youtube.com/* // @match *://www.youtube-nocookie.com/* // @grant GM.getValue // @grant GM.setValue // @grant GM.deleteValue // @grant GM.listValues // @grant GM_getValue // @grant GM_setValue // @grant GM_deleteValue // @grant GM_listValues // @license MIT // @description This script automatically tracks and restores your YouTube playback position. This user script remembers where you left off in any video—allowing you to seamlessly continue watching even after navigating away or reloading the page. It saves your progress for each video for up to 3 days (configurable) and automatically cleans up outdated entries, ensuring a smooth and uninterrupted viewing experience every time. // ==/UserScript== /*jshint esversion: 11 */ (function () { "use strict"; let moviePlayer; let videoElement; let videoId; let useCompatibilityMode = false; // script will save playback time for 3 days by default, edit this value to change. This will be overwritten if the script is updated. const daysToRemember = 3; // Greasemonkey API Compatibility Layer const GMCustomGetValue = useCompatibilityMode ? GM_getValue : GM.getValue; const GMCustomSetValue = useCompatibilityMode ? GM_setValue : GM.setValue; const GMCustomDeleteValue = useCompatibilityMode ? GM_deleteValue : GM.deleteValue; const GMCustomListValues = useCompatibilityMode ? GM_listValues : GM.listValues; async function resumePlayback() { try { const playbackStatus = await GMCustomGetValue(videoId); if (!playbackStatus) return; const lastPlaybackTime = playbackStatus.timestamp; if (!isNaN(lastPlaybackTime) && lastPlaybackTime !== 0) { moviePlayer?.seekTo(lastPlaybackTime); } } catch (error) { throw ("Failed to resume playback due to this error. Error: " + error); } } async function handleVideoLoad(event) { moviePlayer = event.detail; videoElement = event.target?.querySelector('video'); videoId = event.detail?.getVideoData().video_id; const isLive = moviePlayer.getVideoData().isLive; const isCued = moviePlayer.getPlayerState() === 5; let resumed = false; if (!isCued && !isLive) { resumed = true; await resumePlayback(); } videoElement?.addEventListener('timeupdate', async () => { if (!resumed && !isLive) { resumed = true; await resumePlayback(); } updatePlaybackStatus(); }, true); } function updatePlaybackStatus() { if (!videoId) return; try { const currentPlaybackTime = videoElement.currentTime; if (currentPlaybackTime && currentPlaybackTime !== 0) { const currentPlaybackStatus = { timestamp: currentPlaybackTime, lastUpdated: Date.now() }; GMCustomSetValue(videoId, currentPlaybackStatus); } } catch (error) { throw ("Failed to update playback status due to this error. Error: " + error); } } async function cleanUpStoredPlaybackStatuses() { const keys = await GMCustomListValues(); for (const key of keys) { const storedStatus = await GMCustomGetValue(key); if (storedStatus && Date.now() - storedStatus.lastUpdated > daysToRemember * 24 * 60 * 60 * 1000) { await GMCustomDeleteValue(key); } } } function hasGreasyMonkeyAPI() { if (typeof GM !== 'undefined') return true; if (typeof GM_info !== 'undefined') { useCompatibilityMode = true; console.warn("Running in compatibility mode."); return true; } return false; } async function initialize() { try { if (!hasGreasyMonkeyAPI()) throw "Did not detect valid Grease Monkey API"; await cleanUpStoredPlaybackStatuses(); window.addEventListener('yt-player-updated', async (event) => { await handleVideoLoad(event); }, true); window.addEventListener('yt-autonav-pause-player-ended', () => { if (videoId) GMCustomDeleteValue(videoId); }, true); } catch (error) { console.error(`Error when initializing script: ${error}. Aborting script.`); } } initialize(); })();