-
Notifications
You must be signed in to change notification settings - Fork 5
/
anti_shorts.user.js
341 lines (285 loc) · 10.8 KB
/
anti_shorts.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// ==UserScript==
// @name YouTube Anti-Shorts Script
// @version 1.0.202203200743
// @description A YouTube script that replaces shorts links with regular videos.
// @author YukisCoffee
// @match *://www.youtube.com/*
// @grant none
// @run-at document-start
// @require https://cdnjs.cloudflare.com/ajax/libs/arrive/2.4.1/arrive.min.js
// ==/UserScript==
/**
* Shorts URL redirect.
*
* This is called on initial visit only. Successive navigations
* are managed by modifying the YouTube Desktop application.
*/
(function(){
/** @type {string} */
var path = window.location.pathname;
if (0 == path.search("/shorts"))
{
// Extract the video ID from the shorts link and redirect.
/** @type {string} */
var id = path.replace(/\/|shorts|\?.*/g, "");
window.location.replace("https://www.youtube.com/watch?v=" + id);
}
})();
/**
* YouTube Desktop Shorts remover.
*
* If the initial URL was not a shorts link, traditional redirection
* will not work. This instead modifies video elements to replace them with
* regular links.
*/
(function(){
/**
* @param {string} selector (CSS-style) of the element
* @return {Promise<Element>}
*/
async function querySelectorAsync(selector)
{
while (null == document.querySelector(selector))
{
// Pause for a frame and let other code go on.
await new Promise(r => requestAnimationFrame(r));
}
return document.querySelector(selector);
}
/**
* Small toolset for interacting with the Polymer
* YouTube Desktop application.
*
* @author Taniko Yamamoto <[email protected]>
* @version 1.0
*/
class YtdTools
{
/** @type {string} Page data updated event */
static EVT_DATA_UPDATE = "yt-page-data-updated";
/** @type {Element} Main YT Polymer manager */
static YtdApp;
/** @type {bool} */
static hasInitialLoaded = false;
/** @return {Promise<bool>} */
static async isPolymer()
{
/** @return {Promise<void>} */
function waitForBody() // nice hack lazy ass
{
return new Promise(r => {
document.addEventListener("DOMContentLoaded", function a(){
document.removeEventListener("DOMContentLoaded", a);
r();
});
});
}
await waitForBody();
if ("undefined" != typeof document.querySelector("ytd-app"))
{
this.YtdApp = document.querySelector("ytd-app");
return true;
}
return false;
}
/** @async @return {Promise<void|string>} */
static waitForInitialLoad()
{
var updateEvent = this.EVT_DATA_UPDATE;
return new Promise((resolve, reject) => {
if (!this.isPolymer())
{
reject("Not Polymer :(");
}
function _listenerCb()
{
document.removeEventListener(updateEvent, _listenerCb);
resolve();
}
document.addEventListener(updateEvent, _listenerCb);
});
}
/** @return {string} */
static getPageType()
{
return this.YtdApp.data.page;
}
}
class ShortsTools
{
/** @type {MutationObserver} */
static mo = new MutationObserver(muts => {
muts.forEach(mut => {
Array.from(mut.addedNodes).forEach(node => {
if (node instanceof HTMLElement) {
this.onMutation(node);
}
});
});
});
/** @return {void} */
static watchForShorts()
{
/*
this.mo.observe(YtdTools.YtdApp, {
childList: true,
subtree: true
});
*/
var me = this;
YtdTools.YtdApp.arrive("ytd-video-renderer, ytd-grid-video-renderer", function() {
me.onMutation(this);
// This is literally the worst hack I ever wrote, but it works ig...
(new MutationObserver(function(){
if (me.isShortsRenderer(this))
{
me.onMutation(this);
}
}.bind(this))).observe(this, {"subtree": true, "childList": true, "characterData": "true"});
});
}
/** @return {void} */
static stopWatchingForShorts()
{
this.mo.disconnect();
}
/**
* @param {HTMLElement} node
* @return {void}
*/
static onMutation(node)
{
if (node.tagName.search("VIDEO-RENDERER") > -1 && this.isShortsRenderer(node))
{
this.transformShortsRenderer(node);
}
}
/** @return {bool} */
static isShortsRenderer(videoRenderer)
{
return "WEB_PAGE_TYPE_SHORTS" == videoRenderer?.data?.navigationEndpoint?.commandMetadata?.webCommandMetadata?.webPageType;
}
/** @return {string} */
static extractLengthFromA11y(videoData)
{
// A11y = {title} by {creator} {date} {*length*} {viewCount} - play Short
// tho hopefully this works in more than just English
var a11yTitle = videoData.title.accessibility.accessibilityData.label;
var publishedTimeText = videoData.publishedTimeText.simpleText;
var viewCountText = videoData.viewCountText.simpleText;
var isolatedLengthStr = a11yTitle.split(publishedTimeText)[1].split(viewCountText)[0]
.replace(/\s/g, "");
var numbers = isolatedLengthStr.split(/\D/g);
var string = "";
// Remove all empties before iterating it
for (var i = 0; i < numbers.length; i++)
{
if ("" === numbers[i])
{
numbers.splice(i, 1);
i--;
}
}
for (var i = 0; i < numbers.length; i++)
{
// Lazy 0 handling idc im tired
if (1 == numbers.length)
{
string += "0:";
if (1 == numbers[i].length)
{
string += "0" + numbers[i];
}
else
{
string += numbers[i];
}
break;
}
if (0 != i) string += ":";
if (0 != i && 1 == numbers[i].length) string += "0";
string += numbers[i];
}
return string;
}
/**
* @param {HTMLElement} videoRenderer
* @return {void}
*/
static transformShortsRenderer(videoRenderer)
{
/** @type {string} */
var originalOuterHTML = videoRenderer.outerHTML;
/** @type {string} */
var lengthText = videoRenderer.data?.lengthText?.simpleText ?? this.extractLengthFromA11y(videoRenderer.data);
/** @type {string} */
var lengthA11y = videoRenderer.data?.lengthText?.accessibility?.accessibilityData?.label ?? "";
/** @type {string} */
var originalHref = videoRenderer.data.navigationEndpoint.commandMetadata.webCommandMetadata.url;
var href = "/watch?v=" + originalHref.replace(/\/|shorts|\?.*/g, "");
var reelWatchEndpoint = videoRenderer.data.navigationEndpoint.reelWatchEndpoint;
var i;
videoRenderer.data.thumbnailOverlays.forEach((a, index) =>{
if ("thumbnailOverlayTimeStatusRenderer" in a)
{
i = index;
}
});
// Set the thumbnail overlay style
videoRenderer.data.thumbnailOverlays[i].thumbnailOverlayTimeStatusRenderer.style = "DEFAULT";
delete videoRenderer.data.thumbnailOverlays[i].thumbnailOverlayTimeStatusRenderer.icon;
// Set the thumbnail overlay text
videoRenderer.data.thumbnailOverlays[i].thumbnailOverlayTimeStatusRenderer.text.simpleText = lengthText;
// Set the thumbnail overlay accessibility label
videoRenderer.data.thumbnailOverlays[i].thumbnailOverlayTimeStatusRenderer.text.accessibility.accessibilityData.label = lengthA11y;
// Set the navigation endpoint metadata (used for middle click)
videoRenderer.data.navigationEndpoint.commandMetadata.webCommandMetadata.webPageType = "WEB_PAGE_TYPE_WATCH";
videoRenderer.data.navigationEndpoint.commandMetadata.webCommandMetadata.url = href;
videoRenderer.data.navigationEndpoint.watchEndpoint = {
"videoId": reelWatchEndpoint.videoId,
"playerParams": reelWatchEndpoint.playerParams,
"params": reelWatchEndpoint.params
};
delete videoRenderer.data.navigationEndpoint.reelWatchEndpoint;
//var _ = videoRenderer.data; videoRenderer.data = {}; videoRenderer.data = _;
// Sometimes the old school data cycle trick fails,
// however this always works.
var _ = videoRenderer.cloneNode();
_.data = videoRenderer.data;
for (var i in videoRenderer.properties)
{
_[i] = videoRenderer[i];
}
videoRenderer.insertAdjacentElement("afterend", _);
videoRenderer.remove();
}
}
/**
* Sometimes elements are reused on page updates, so fix that
*
* @return {void}
*/
function onDataUpdate()
{
var videos = document.querySelectorAll("ytd-video-renderer, ytd-grid-video-renderer");
for (var i = 0, l = videos.length; i < l; i++) if (ShortsTools.isShortsRenderer(videos[i]))
{
ShortsTools.transformShortsRenderer(videos[i]);
}
}
/**
* I hope she makes lotsa spaghetti :D
* @async @return {Promise<void>}
*/
async function main()
{
// If not Polymer, nothing happens
if (await YtdTools.isPolymer())
{
ShortsTools.watchForShorts();
document.addEventListener("yt-page-data-updated", onDataUpdate);
}
}
main();
temp1.onGuideElementChanged(function(){console.log("a")})
})();