// ==UserScript== // @name YouTube 稍后再看重定向 // @name:en YouTube Watch Later Redirect // @name:zh-CN YouTube 稍后再看重定向 // @name:zh-TW YouTube 稍後再看重定向 // @namespace http://tampermonkey.net/ // @version 1.1.20250326 // @author JerryYang // @description 重定向YouTube稍后再看的视频链接到原始视频链接,并在新标签页中打开视频。 // @description:en Redirect YouTube Watch Later video links to their original video and open in a new tab. // @description:zh-CN 重定向YouTube稍后再看的视频链接到原始视频链接,并在新标签页中打开视频。 // @description:zh-TW 重定向YouTube稍後再看的影片連結到原始影片,並在新標籤頁中開啟影片。 // @license GPL-3.0 License // @homepage https://github.com/JerryYang-30/Open-UserScripts // @supportURL https://github.com/JerryYang-30/Open-UserScripts/issues // @match https://www.youtube.com/* // @icon https://www.google.com/s2/favicons?sz=64&domain=YouTube.com // @grant GM_setValue // @grant GM_getValue // @grant GM_registerMenuCommand // @run-at document-idle // @downloadURL https://update.greasyfork.icu/scripts/507417/YouTube%20%E7%A8%8D%E5%90%8E%E5%86%8D%E7%9C%8B%E9%87%8D%E5%AE%9A%E5%90%91.user.js // @updateURL https://update.greasyfork.icu/scripts/507417/YouTube%20%E7%A8%8D%E5%90%8E%E5%86%8D%E7%9C%8B%E9%87%8D%E5%AE%9A%E5%90%91.meta.js // ==/UserScript== /* 支持多语言 */ // 语言支持:英语 (en), 简体中文 (zh-CN), 繁体中文 (zh-TW) const userLanguage = navigator.language || navigator.userLanguage; // 多语言翻译资源 const translations = { "en": { "notificationMessage": "All videos have been successfully redirected!", "enableNotification": "Enable Notification", "disableNotification": "Disable Notification", "adjustNotificationStyle": "Adjust Notification Style", "resetDefaults": "Reset to Default Settings", "confirmSave": "Are you sure you want to save the changes?", "confirmReset": "Are you sure you want to reset to default settings? This action cannot be undone!", "savedMessage": "Settings saved, click OK to refresh the page.", "resetMessage": "Default settings restored, click OK to refresh the page.", "enableNotificationMessage": "Notification has been enabled, click OK to refresh for changes to take effect.", "disableNotificationMessage": "Notification has been disabled, click OK to refresh for changes to take effect.", "adjustPanelH2Title": "Adjust the style of the prompt box(leave blank to not modify)", "adjustPanelPosition": "Select prompt box location", "adjustPanelWidth": "Prompt box width (can also be auto)", "adjustPanelHeight": "Prompt box length (can also be auto)", "adjustPanelHideAfter": "The display time of the prompt box (milliseconds, just enter the number)", "adjustPanelCustomMessage": "Custom prompt words", "adjustPanelSave": "Save", "adjustPanelCancel": "Cancel", "adjustPanelReset": "Restore default settings", "adjustPanelPlaceholder": "Currently ", "exportList": "Export Watch Later List", "exportSuccess": "Export successful! The file has been saved to your downloads folder.", "noVideosFound": "No videos found in the Watch Later list." }, "zh-CN": { "notificationMessage": "全部视频已成功重定向!", "enableNotification": "启用提示框", "disableNotification": "禁用提示框", "adjustNotificationStyle": "调整提示框样式", "resetDefaults": "恢复默认设置", "confirmSave": "确定要保存修改吗?此操作不可撤销!", "confirmReset": "确定要恢复默认设置吗?此操作不可撤销!", "savedMessage": "设置已保存,点击确定刷新页面以生效。", "resetMessage": "默认设置已恢复,点击确定刷新页面以生效。", "enableNotificationMessage": "提示框已启用,点击确定刷新后生效。", "disableNotificationMessage": "提示框已禁用,点击确定刷新后生效。", "adjustPanelH2Title": "调整提示框样式(留空则不修改)", "adjustPanelPosition": "选择提示框位置", "adjustPanelWidth": "提示框宽度(可以填auto)", "adjustPanelHeight": "提示框长度(可以填auto)", "adjustPanelHideAfter": "提示框显示时间(毫秒,输入数字即可)", "adjustPanelCustomMessage": "自定义提示词", "adjustPanelSave": "保存", "adjustPanelCancel": "取消", "adjustPanelReset": "恢复默认设置", "adjustPanelPlaceholder": "当前为", "exportList": "导出稍后观看列表", "exportSuccess": "导出成功!文件已保存到下载文件夹。", "noVideosFound": "稍后观看列表中未找到视频。" }, "zh-TW": { "notificationMessage": "全部影片已成功重新導向!", "enableNotification": "啟用提示框", "disableNotification": "禁用提示框", "adjustNotificationStyle": "調整提示框樣式", "resetDefaults": "恢復預設設定", "confirmSave": "確定要保存修改嗎?此操作不可撤銷!", "confirmReset": "確定要恢復預設設定嗎?此操作不可撤銷!", "savedMessage": "設定已保存,點擊確定刷新頁面以生效。", "resetMessage": "預設設定已恢復,點擊確定刷新頁面以生效。", "enableNotificationMessage": "提示框已啟用,點擊確定刷新後生效。", "disableNotificationMessage": "提示框已禁用,點擊確定刷新後生效。", "adjustPanelH2Title": "调整提示框样式(留空则不修改)", "adjustPanelPosition": "選擇提示框位置", "adjustPanelWidth": "提示框寬度(可以填auto)", "adjustPanelHeight": "提示框長度 (可以填auto)", "adjustPanelHideAfter": "提示框顯示時間 (毫秒,輸入數字即可)", "adjustPanelCustomMessage": "自定義提示詞", "adjustPanelSave": "保存", "adjustPanelCancel": "取消", "adjustPanelReset": "恢復默認設置", "adjustPanelPlaceholder": "當前爲", "exportList": "導出稍後觀看列表", "exportSuccess": "導出成功!文件已保存到下載文件夾。", "noVideosFound": "稍後觀看列表中未找到影片。" } }; // 根据用户语言设置获取翻译 const lang = userLanguage.startsWith('zh-TW') ? 'zh-TW' : userLanguage.startsWith('zh') ? 'zh-CN' : 'en'; const t = translations[lang]; // 主函数 (function() { 'use strict'; /* 先判断当前是否为稍后再看页面 */ let lastUrl = window.location.href; // 保存上一次的 URL // 初次加载时初始化 if (window.location.href == "https://www.youtube.com/playlist?list=WL") { redirector(); } const observer = new MutationObserver((mutations) => { mutations.forEach(mutation => { mutation.addedNodes.forEach(node => { if (node.nodeType === Node.ELEMENT_NODE) { // 检查 Shadow DOM 中的元素 if (node.shadowRoot) { const targetElement = node.shadowRoot.querySelector('yt-formatted-string.title.style-scope.ytd-guide-entry-renderer'); if (targetElement) { targetElement.addEventListener('click', () => { //console.log('YouTube稍后再看脚本 - 点击了“稍后观看”按钮'); redirector(); }); } } // 处理非 Shadow DOM 元素 if (node.matches('yt-formatted-string.title.style-scope.ytd-guide-entry-renderer')) { node.addEventListener('click', () => { //console.log('YouTube稍后再看脚本 - 点击了“稍后观看”按钮'); redirector(); }); } } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); // 监听 URL 变化 (pushState 和 popstate) const originalPushState = history.pushState; history.pushState = function() { originalPushState.apply(this, arguments); onUrlChange(); }; window.addEventListener('popstate', onUrlChange); // 处理前进和后退按钮 /* 对稍后再看页面作重定向处理 */ // 重定向 function redirector() { const observer = new MutationObserver((mutationsList, observer) => { // 查询稍后再看列表中的视频元素 const items = document.querySelectorAll('div#content.style-scope.ytd-playlist-video-renderer'); if (items.length > 0) { showNotification(items.length); observer.disconnect(); items.forEach(item => { if (!item.dataset.redirectBound) { item.dataset.redirectBound = true; // 获取缩略图和标题区域 const thumbnail = item.querySelector('ytd-thumbnail#thumbnail'); const metaArea = item.querySelector('div#meta'); if (thumbnail && metaArea) { // 处理缩略图区域 const videoTitle = metaArea.querySelector('a#video-title'); if (videoTitle) { const href = videoTitle.getAttribute('href'); if (href && href.includes('list=WL')) { const videoId = href.match(/v=([^&]+)/)[1]; const originalUrl = `https://www.youtube.com/watch?v=${videoId}`; // 存储原始URL到缩略图元素 thumbnail.setAttribute('data-original-url', originalUrl); // 为缩略图内的所有可点击元素添加事件处理 thumbnail.querySelectorAll('a, img, .yt-core-image').forEach(element => { // 左键点击缩略图 element.addEventListener('click', function(event) { event.preventDefault(); event.stopPropagation(); window.open(originalUrl, '_blank'); return false; }); // 中键点击缩略图 element.addEventListener('auxclick', function(event) { if (event.button === 1) { event.preventDefault(); event.stopPropagation(); window.open(originalUrl, '_blank'); return false; } }); }); } } // 处理标题区域 metaArea.addEventListener('mouseover', function() { const videoTitle = metaArea.querySelector('a#video-title'); if (videoTitle) { const href = videoTitle.getAttribute('href'); if (href && href.includes('list=WL')) { const videoId = href.match(/v=([^&]+)/)[1]; const originalUrl = `https://www.youtube.com/watch?v=${videoId}`; videoTitle.setAttribute('data-original-url', originalUrl); } } }); // 左键点击标题 metaArea.addEventListener('click', function(event) { handleLinkClick(event); }); // 中键点击标题 metaArea.addEventListener('auxclick', function(event) { if (event.button === 1) { handleLinkClick(event); } }); } } }); } }); // 监听 DOM 变化 observer.observe(document.body, { childList: true, subtree: true }); } function handleLinkClick(event) { // 处理左键和中键点击稍后再看视频的行为 const target = event.target; if (target.hasAttribute('href') && target.getAttribute('href').includes('@')) { return; // 点击的是作者主页链接,直接跳转,不重定向 } const linkElement = target.closest('div#meta.style-scope.ytd-playlist-video-renderer').querySelector('a#video-title'); if (linkElement) { const originalUrl = linkElement.getAttribute('data-original-url'); if (originalUrl) { event.preventDefault(); // 阻止默认跳转行为 event.stopPropagation(); // 阻止事件冒泡 window.open(originalUrl, '_blank'); // 在新标签页中打开原视频链接 return false; // 防止进一步的默认跳转行为 } } } // 页面跳转或 URL 变化时触发重新初始化脚本 function onUrlChange() { if (window.location.href !== lastUrl) { //console.log('YouTube稍后再看脚本 - 检测到 URL 变化: ', window.location.href); if (window.location.href == "https://www.youtube.com/playlist?list=WL") { redirector(); } lastUrl = window.location.href; // 更新 lastUrl } } /* 提示框及其菜单 */ // 默认设置 const defaultSettings = { showNotification: true, position: 'bottom-right', width: 'auto', height: 'auto', hideAfter: 3000, customMessage: t.notificationMessage // 默认提示词 }; // 注册菜单项 function registerMenuCommands() { if (!getSettings().showNotification) { GM_registerMenuCommand(t.enableNotification, toggleNotification); } else { GM_registerMenuCommand(t.disableNotification, toggleNotification); GM_registerMenuCommand(t.adjustNotificationStyle, createStyleAdjustmentPanel); } // 添加导出列表菜单项 GM_registerMenuCommand(t.exportList, exportWatchLaterList); } // 打开提示框开关菜单 function toggleNotification() { const settings = getSettings(); const newShowNotification = !settings.showNotification; saveSettings({ ...settings, showNotification: newShowNotification }); // 更新菜单项显示状态 updateMenuCommands(); } // 更新菜单项显示状态 function updateMenuCommands() { const settings = getSettings(); const newShowNotification = !settings.showNotification; alert(`${newShowNotification ? t.enableNotificationMessage : t.disableNotificationMessage}`); location.reload(); // 刷新页面 } // 初次注册菜单项 registerMenuCommands(); // 创建样式调整面板并使用 Shadow DOM function createStyleAdjustmentPanel() { const settings = getSettings(); // 创建容器,并为其附加 Shadow DOM const panelContainer = document.createElement('div'); panelContainer.style.position = 'fixed'; panelContainer.style.top = '0'; panelContainer.style.left = '0'; panelContainer.style.width = '100vw'; panelContainer.style.height = '100vh'; panelContainer.style.zIndex = '10001'; panelContainer.style.backgroundColor = 'rgba(0, 0, 0, 0.5)'; // 添加遮罩效果 const shadowRoot = panelContainer.attachShadow({ mode: 'open' }); shadowRoot.innerHTML = `