-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongs.user.js
108 lines (88 loc) · 3.33 KB
/
longs.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// ==UserScript==
// @name Longs
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Remove YouTube Shorts.
// @author mWalrus
// @match https://www.youtube.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant none
// ==/UserScript==
(function() {
// youtube url matcher with relevant match grouping
// taken from: https://stackoverflow.com/a/37704433/16166072
// window.URL_REGEX = "^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube(-nocookie)?\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$"
window.YOUTUBE_REEL_SELECTOR = "ytd-reel-shelf-renderer"
window.BASE_YOUTUBE_URL = "https://www.youtube.com/"
window.MAIN_PAGE_SELECTOR = "[is-shorts='']"
window.NAV_BAR_SELECTOR = "[title='Shorts']"
window.LOG_PREFIX = "LONGS: "
const resizeHandler = () => {
if (clearNavLinks()) window.removeEventListener("resize", resizeHandler)
}
initialCleanup()
window.addEventListener("resize", resizeHandler)
listenForNavigatorEventsAndClear()
})();
function initialCleanup() {
runWhenReady(window.NAV_BAR_SELECTOR, clearNavLinks)
if (userIsOnMainPage()) {
runWhenReady(window.MAIN_PAGE_SELECTOR, clearShortsSection)
} else if (userIsWatching()) {
runWhenReady(window.YOUTUBE_REEL_SELECTOR, () => {
clearShortsSection(window.YOUTUBE_REEL_SELECTOR)
})
}
}
// helper function for running a passed callback function when a selector can be found in the DOM.
// yoinked from https://github.com/Tampermonkey/tampermonkey/issues/1279#issuecomment-875386821
function runWhenReady(readySelector, callback) {
var numAttempts = 0;
var tryNow = function() {
var elem = document.querySelector(readySelector);
if (elem) {
callback();
} else {
numAttempts++;
if (numAttempts >= 34) {
log('Giving up after 34 attempts. Could not find: ' + readySelector);
} else {
setTimeout(tryNow, 250 * Math.pow(1.1, numAttempts));
}
}
};
tryNow();
}
function clearNavLinks() {
const shortsLinks = document.querySelectorAll(window.NAV_BAR_SELECTOR)
if (!shortsLinks || shortsLinks.length === 0) return false
for (let link of shortsLinks) {
link.parentNode.removeChild(link)
log('Removed youtube shorts link from left nav-bar')
}
return true
}
function clearShortsSection(s = window.MAIN_PAGE_SELECTOR) {
let shortsSection = document.querySelector(s)
if (!shortsSection) return
shortsSection.parentNode.removeChild(shortsSection)
log('Removed shorts section from page')
}
function listenForNavigatorEventsAndClear() {
window.addEventListener("yt-navigate-finish", () => {
if (userIsOnMainPage()) {
runWhenReady(window.MAIN_PAGE_SELECTOR, clearShortsSection)
} else if (userIsWatching()) {
runWhenReady(window.YOUTUBE_REEL_SELECTOR, () => clearShortsSection(window.YOUTUBE_REEL_SELECTOR))
}
})
}
function userIsOnMainPage() {
return window.location.href === window.BASE_YOUTUBE_URL
}
function userIsWatching() {
return window.location.href.includes("/watch?v=")
}
function log(msg) {
console.log(window.LOG_PREFIX, msg)
}