Skip to content

Instantly share code, notes, and snippets.

@eevmanu
Created March 28, 2025 00:16
Show Gist options
  • Save eevmanu/0ae6d2e5f89847cadcaa8695546793cf to your computer and use it in GitHub Desktop.
Save eevmanu/0ae6d2e5f89847cadcaa8695546793cf to your computer and use it in GitHub Desktop.
YouTube Shorts & youtu.be Redirector - tampermonkey userscript
// ==UserScript==
// @name YouTube Shorts & youtu.be Redirector
// @namespace http://tampermonkey.net/
// @version 1.1
// @description Redirects YouTube Shorts and youtu.be links to the standard watch page.
// @author Your Name (or handle)
// @match https://www.youtube.com/shorts/*
// @match https://youtu.be/*
// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
const currentUrl = window.location.href;
let videoId = null;
// Regex for https://www.youtube.com/shorts/<videoId>
const shortsRegex = /^https:\/\/www\.youtube\.com\/shorts\/([^\/?#]+)/;
let match = shortsRegex.exec(currentUrl);
if (match && match[1]) {
videoId = match[1];
console.log('Detected YouTube Shorts URL. Video ID:', videoId);
} else {
// Regex for https://youtu.be/<videoId>
const shortlinkRegex = /^https:\/\/youtu\.be\/([^\/?#]+)/;
match = shortlinkRegex.exec(currentUrl);
if (match && match[1]) {
videoId = match[1];
console.log('Detected youtu.be URL. Video ID:', videoId);
}
}
// If a video ID was successfully extracted, redirect
if (videoId) {
const newUrl = 'https://www.youtube.com/watch?v=' + videoId;
console.log('Redirecting to:', newUrl);
// Use replace() so the back button works as expected (doesn't go back to the short URL)
window.location.replace(newUrl);
} else {
console.log('No matching YouTube Shorts or youtu.be pattern found. No redirection needed.');
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment