Greasy Fork

Greasy Fork is available in English.

YouTube强制新标签页打开链接(更新版)

让YouTube链接在新标签页中打开,不使用模拟Ctrl+点击,同时保持原页面不变。同时,打开视频时保持视频暂停。

当前为 2024-11-22 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         YouTube Force Link Open in New Tab (Updated)
// @name:zh-CN   YouTube强制新标签页打开链接(更新版)
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  Opens YouTube links in new tabs without simulating Ctrl+Click, keeping the original page unchanged. Also pauses video on opening.
// @description:zh-CN  让YouTube链接在新标签页中打开,不使用模拟Ctrl+点击,同时保持原页面不变。同时,打开视频时保持视频暂停。
// @author       YourName
// @match        *://*.youtube.com/*
// @grant        none
// @license      MIT
// @run-at       document-start
// @icon         https://img.icons8.com/?size=64&id=121211&format=png
// ==/UserScript==

(function() {
    'use strict';

    const YOUTUBE_PATTERNS = ['/watch', '/channel', '/user', '/playlist', '/shorts'];

    function isYouTubeLink(url) {
        return url.includes('youtube.com') || url.startsWith('/') || url.startsWith('https://youtu.be/');
    }

    function shouldOpenInNewTab(url) {
        return YOUTUBE_PATTERNS.some(pattern => url.includes(pattern));
    }

    function handleClick(event) {
        if (event.ctrlKey || event.metaKey) return;

        const anchor = event.target.closest('a');
        if (!anchor || !anchor.href) return;

        if (isYouTubeLink(anchor.href) && shouldOpenInNewTab(anchor.href)) {
            event.preventDefault();
            event.stopPropagation();
            // Open the link in a new tab
            const newTab = window.open(anchor.href, '_blank');
            // Pause the video in the new tab
            newTab.addEventListener('load', function() {
                let video = newTab.document.querySelector('video');
                if (video) {
                    video.pause();
                }
            }, false);
        }
    }

    // Pause video on current page load
    function pauseVideo() {
        let video = document.querySelector('video');
        if (video) {
            video.pause();
        } else {
            // If video is not immediately found, try again after a short delay
            setTimeout(pauseVideo, 100);
        }
    }

    // Execute pause function when document is loaded
    document.addEventListener('DOMContentLoaded', pauseVideo);

    document.addEventListener('click', handleClick, true);
})();