YouTube Force Link Open in New Tab (Updated)

Opens YouTube links in new tabs without simulating Ctrl+Click, keeping the original page unchanged.

目前為 2024-09-18 提交的版本,檢視 最新版本

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

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 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.2
// @description  Opens YouTube links in new tabs without simulating Ctrl+Click, keeping the original page unchanged.
// @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();
            window.open(anchor.href, '_blank');
        }
    }

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