YouTube Mobile Open Videos and Shorts in New Tab

Open all YouTube videos and Shorts in a new tab on the mobile website

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         YouTube Mobile Open Videos and Shorts in New Tab
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Open all YouTube videos and Shorts in a new tab on the mobile website
// @author       Agreasyforkuser
// @match        *://m.youtube.com/*
// @icon         https://www.google.com/s2/favicons?domain=youtube.com
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Function to modify all video and Shorts links
    function updateLinks() {
        // Select all anchor tags for regular videos and Shorts
        const videoLinks = document.querySelectorAll('a[href*="/watch"], a[href*="/shorts/"]');
        videoLinks.forEach(link => {
            link.setAttribute('target', '_blank'); // Ensure they open in a new tab
        });
    }

    // Run the function initially
    updateLinks();

    // Observe changes to the page for dynamic content loading
    const observer = new MutationObserver(() => {
        updateLinks(); // Update links whenever the page changes
    });

    // Start observing the body for changes
    observer.observe(document.body, { childList: true, subtree: true });
})();