YouTube Mobile Open Videos and Shorts in New Tab

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

Ajankohdalta 5.12.2024. Katso uusin versio.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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 });
})();