scripts/userscripts/yt-noshorts.user.js

121 lines
3.7 KiB
JavaScript
Raw Normal View History

2022-03-18 15:57:34 +00:00
// ==UserScript==
// @name NoShorts for YouTube
2022-03-18 16:04:58 +00:00
// @description Redirects Shorts to normal video webpage
2022-03-18 15:57:34 +00:00
// @namespace tretrauit-dev
2022-03-18 16:52:39 +00:00
// @match *://*.youtube.com/*
2022-03-18 16:04:58 +00:00
// @exclude-match`*://music.youtube.com/*
2022-03-18 17:02:47 +00:00
// @icon https://upload.wikimedia.org/wikipedia/commons/f/fc/Youtube_shorts_icon.svg
2022-03-18 15:57:34 +00:00
// @grant none
2022-12-12 19:18:29 +00:00
// @version 1.3.0
2022-03-18 15:57:34 +00:00
// @author tretrauit
2022-03-18 16:52:39 +00:00
// @require https://raw.githubusercontent.com/naugtur/insertionQuery/master/insQ.min.js
2022-03-18 15:57:34 +00:00
// @homepageURL https://gitlab.com/tretrauit/scripts
// @supportURL https://gitlab.com/tretrauit/scripts/-/issues
2022-03-18 15:59:52 +00:00
// @downloadURL https://gitlab.com/tretrauit/scripts/-/raw/main/userscripts/yt-noshorts.user.js
2022-12-12 19:18:29 +00:00
// @run-at document-start
2022-03-18 15:57:34 +00:00
// ==/UserScript==
2022-12-12 19:18:29 +00:00
const DEBUG = false;
function logDebug(...kwargs) {
if (!DEBUG) {
return;
}
console.log(...kwargs);
}
2023-08-21 18:28:47 +00:00
function getShortsId(videoPathName) {
const shortPath = videoPathName.split("/");
return shortPath[shortPath.length - 1];
2022-03-18 15:57:34 +00:00
}
2022-03-18 16:52:39 +00:00
2022-12-12 19:18:29 +00:00
function redirectReplace() {
2023-08-21 18:28:47 +00:00
window.location.replace(
"https://www.youtube.com/watch?v=" + getShortsId(window.location.pathname),
);
2022-03-18 16:52:39 +00:00
}
2023-08-21 18:28:47 +00:00
function checkCurrentURL() {
if (window.location.pathname.includes("/shorts/")) {
2022-12-12 19:18:29 +00:00
logDebug("Shorts url detected, redirecting...");
redirectReplace();
2022-03-18 16:52:39 +00:00
}
}
2022-12-12 19:18:29 +00:00
// Should be run asap
checkCurrentURL();
2023-08-21 18:28:47 +00:00
function replaceHrefURL(element) {
if (element.href != null && element.href.includes("/shorts/")) {
element.href = "/watch?v=" + getShortsId(element.href);
2022-03-18 16:52:39 +00:00
}
}
2023-08-21 18:28:47 +00:00
function checkElements() {
insertionQ(":is(#video-title, #thumbnail) ").every(function (element) {
2022-12-12 19:18:29 +00:00
replaceHrefURL(element);
2022-03-18 16:52:39 +00:00
});
}
2023-08-21 18:28:47 +00:00
window.addEventListener("yt-navigate-finish", checkCurrentURL);
2022-12-12 19:18:29 +00:00
// Replace addEventListener with our sus one.
const o_addEventListener = window.addEventListener;
const o_shady_addEventListener = window.__shady_addEventListener;
function f_addEventListener(eventName, callback) {
logDebug("Event listener added: ", eventName);
function f_callback(event) {
function cb_dbg() {
if (event instanceof MouseEvent) {
// Event flood in console
return;
} else if (event instanceof PointerEvent) {
// Event flood in console
return;
} else if (event instanceof BeforeUnloadEvent) {
// Event flood in console
return;
}
logDebug("Event callback triggered: ", event, event.data);
logDebug("Page url: ", window.location.href);
}
if (DEBUG) {
cb_dbg();
}
if (event instanceof MessageEvent) {
// This event is made by SponsorBlock not Youtube so by default it will not run.
// But this can speed up the page navigation process so i'll just keep it.
const data = event.data;
2023-08-21 18:28:47 +00:00
if (data.type == "navigation" && data.pageType == "shorts") {
2022-12-12 19:18:29 +00:00
if (data.videoID == undefined) {
return;
}
logDebug("Thank you SponsorBlock for this event :3");
logDebug("Navigating to video...");
if (window.location.pathname.includes("/shorts/")) {
2023-08-21 18:28:47 +00:00
window.location.replace(
"https://www.youtube.com/watch?v=" + data.videoID,
);
2022-12-12 19:18:29 +00:00
return;
}
2023-08-21 18:28:47 +00:00
window.location.assign(
"https://www.youtube.com/watch?v=" + data.videoID,
);
2022-12-12 19:18:29 +00:00
return;
}
}
callback(event);
}
o_addEventListener(eventName, f_callback);
}
function f_sus_addEventListener(a, b, c) {
logDebug("Shady addEventListener triggered.");
logDebug(a, b, c);
o_shady_addEventListener(a, b, c);
}
window.addEventListener = f_addEventListener;
logDebug("Init fake addEventListener successful.");
window.__shady_addEventListener = f_sus_addEventListener;
logDebug("Init fake __shady_addEventListener successful.");
checkElements();
2023-08-21 18:28:47 +00:00
console.warn("Fuck you YouTube - NoShorts loaded.");