YouTube Mobile Open Videos and Shorts in New Tab

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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 });
})();