YouTube Force Link Open in New Tab (Updated)

Opens YouTube links in new tabs without simulating Ctrl+Click, keeping the original page unchanged. Also pauses video on opening.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);
})();