-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.js
296 lines (234 loc) · 7.69 KB
/
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
// ==UserScript==
// @name Watch Later Extractor
// @namespace rbits.watch-later-extractor
// @version 0.0.4
// @description Exports videos from your YouTube Watch Later page to a JSON file
// @author rbits
// @match https://www.youtube.com/playlist*
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant GM_registerMenuCommand
// @license GPL3
// ==/UserScript==
function runScript() {
console.log("Watch Later Extractor script running");
let box = document.createElement("div");
box.style = `
color: white;
background-color: #555555;
border-radius: 2rem;
width: 50rem;
height: 20rem;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 2rem;
padding: 2rem;
`;
let textElement = document.createElement("p");
textElement.innerHTML = "Enter id/url of video to stop at<br>(leave blank to process all videos)"
textElement.style = `
font-size: 2rem;
text-align: center;
`
box.appendChild(textElement);
let videoIdInput = document.createElement("input");
videoIdInput.style = `
font-size: 2rem;
width: 80%;
`
box.appendChild(videoIdInput);
let fileType = document.createElement("select");
fileType.innerHTML = `
<option value="json">JSON</option>
<option value="csv">CSV</option>
`;
fileType.style = `
font-size: 2rem;
`
box.appendChild(fileType);
let button = document.createElement("button");
button.textContent = "Start";
button.style = `
font-size: 2rem;
padding: 0.5rem;
`
box.appendChild(button);
let flex = document.createElement("div");
flex.style = `
width: 100vw;
height: 100vh;
position: fixed;
display: flex;
align-items: center;
justify-content: center;
z-index: 9999;
`
flex.appendChild(box);
document.body.appendChild(flex);
button.onclick = () => {
startProcessing(videoIdInput.value, fileType.value);
document.body.removeChild(flex);
};
}
function startProcessing(stopVideoId, fileType) {
// Convert url to id
const videoIdMatch = stopVideoId.match(/\/watch\?v=([^&]*)/);
if (videoIdMatch) {
stopVideoId = videoIdMatch[1];
}
let videosElement = document.querySelector("ytd-playlist-video-renderer").parentElement;
let signals = {
allLoaded: false,
}
parseVideos(videosElement, stopVideoId, signals)
.then((parsedVideos) => handleParsedVideos(parsedVideos, fileType));
repeatScroll(videosElement, signals);
}
// Parses all videos as they appear in videos
// Once signals.allLoaded is set, it finishes parsing all remaining videos then
// returns list of parsed videos
async function parseVideos(videosElement, stopVideoId, signals) {
console.log("Starting video parsing");
if (stopVideoId !== "") {
console.log("Stopping at %s", stopVideoId);
}
let videos = videosElement.children;
let parsedVideos = [];
let i = 0;
let didFinishEarly = false;
// videos can grow at any time
while (true) {
while (i < videos.length - 1) {
const parsedVideo = parseVideo(videos.item(i));
parsedVideos.push(parsedVideo);
i++;
if (parsedVideo.videoId === stopVideoId) {
didFinishEarly = true;
signals.allLoaded = true;
break;
}
}
if (signals.allLoaded) {
break;
}
console.log("Parsed %d videos, waiting for more videos", i);
while (i >= videos.length - 1 && !signals.allLoaded) {
// Wait 0.1s between checks
await new Promise(executor => setTimeout(executor, 100))
}
}
// Usually last item is ytd-continuation-item-renderer so isn't parsed
// It's probably a video now, so it should be parsed now
if (isEnd(videos) && !didFinishEarly) {
const lastItem = videos.item(videos.length - 1);
parsedVideos.push(parseVideo(lastItem));
} else {
console.log("Exited early: parsing finished but continuation item still exists");
}
return parsedVideos;
}
function parseVideo(videoElement) {
// const thumbnail = videoElement.getElementsByTagName("img")[0].src;
const titleElement = videoElement.querySelector("#video-title");
const videoUrl = titleElement.href;
const videoId = videoUrl.match(/\/watch\?v=([^&]*)/)[1];
const title = titleElement.title;
const channelElement = videoElement.querySelector("#channel-name")
.getElementsByTagName("a")[0];
const channelUrl = channelElement.href;
const channelName = channelElement.textContent;
return {
title,
channelName,
videoUrl,
videoId,
channelUrl,
// thumbnail,
};
}
async function repeatScroll(videosElement, signals) {
let videos = videosElement.children;
// No need to scroll, already loaded
if (isEnd(videos)) {
signals.allLoaded = true;
return;
}
const mutationCallback = (_mutationList, observer) => {
if (isEnd(videos) || signals.allLoaded) {
signals.allLoaded = true;
observer.disconnect();
} else {
scrollToBottom()
}
}
const observer = new MutationObserver(mutationCallback);
observer.observe(videosElement, { childList: true });
scrollToBottom();
}
function scrollToBottom() {
window.scroll(0, document.documentElement.scrollHeight);
console.log("Scrolled to " + document.documentElement.scrollHeight);
}
function isEnd(videos) {
const lastItem = videos.item(videos.length - 1);
if (lastItem.tagName === "YTD-PLAYLIST-VIDEO-RENDERER") {
return true;
} else if (lastItem.tagName === "YTD-CONTINUATION-ITEM-RENDERER") {
return false;
} else {
console.error(lastItem.tagName);
throw new Error("Unknown item in video list");
}
}
function handleParsedVideos(parsedVideos, fileType) {
console.log("All videos parsed, creating file");
let fileString = "";
if (fileType === "json") {
fileString = JSON.stringify(parsedVideos);
} else if (fileType === "csv") {
fileString = objListToCsv(parsedVideos);
}
const base64String = stringToBase64(fileString);
var downloadLink = document.createElement("a");
downloadLink.href = "data:text/plain;base64," + base64String;
downloadLink.download = "playlist." + fileType;
downloadLink.click();
// console.dir(parsedVideos);
}
// From https://developer.mozilla.org/en-US/docs/Glossary/Base64
function stringToBase64(string) {
const bytes = new TextEncoder().encode(string);
const binString = Array.from(bytes, (byte) =>
String.fromCodePoint(byte),
).join("");
return btoa(binString);
}
function objListToCsv(objList) {
const columns = Object.keys(objList[0]);
let rows = [];
for (const obj of objList) {
let row = "";
let first = true;
for (const column of columns) {
if (first) {
first = false;
} else {
row += ",";
}
// Surround in quotes and escape quotes
row += "\"" + obj[column].replaceAll("\"", "\"\"") + "\"";
}
rows.push(row);
}
let csv = columns.join(",") + "\n";
csv += rows.join("\n");
return csv;
}
(function() {
'use strict';
GM_registerMenuCommand(
"Run script",
runScript,
);
})();