-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.user.js
377 lines (335 loc) · 12 KB
/
main.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// ==UserScript==
// @name Youtube Subscription Exporter
// @namespace http://tampermonkey.net/
// @version 1.0
// @description produces an OPML (RSS) file of the subscriptions of a logged in YouTube account
// @author antidipyramid
// @match https://www.youtube.com/*
// @icon https://www.google.com/s2/favicons?domain=github.com
// @require https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js
// @require https://unpkg.com/micromodal/dist/micromodal.min.js
// @resource microModalStyle https://gist.github.com/ghosh/4f94cf497d7090359a5c9f81caf60699/raw/d9281f3298b46d9cf991b674bc6e1c1ed14e91cc/micromodal.css
// @updateURL https://github.com/antidipyramid/sub-scraper/raw/main/main.user.js
// @downloadURL https://github.com/antidipyramid/sub-scraper/raw/scrape-subscriptions/main.user.js
// @grant GM_registerMenuCommand
// @grant GM_addStyle
// @grant GM_getResourceText
// ==/UserScript==
const directChannelPrefix = "channel",
altChannelURLPrefixes = new Set(["c", "user"]),
excludedPrefixes = new Set(["feed"]);
/**
* Fetches the channel ID given the URL of a YouTube channel.
*
* @param {string} channelURL - a channel URL
* @return {Promise} - resolves to the channel ID string
*/
function getChannelID(channelURL) {
const regex =
/https:\/\/www.youtube.com\/(?<prefix>\w+)\/*(?<channelName>[\w-]*)/,
match = channelURL.match(regex);
let prefix, channelName;
try {
[prefix, channelName] = [match.groups.prefix, match.groups.channelName];
} catch (e) {
if (e instanceof TypeError) {
console.error("TypeError while processing " + channelURL);
} else {
console.error(e + " while processing " + channelURL);
}
return Promise.resolve("");
}
if (prefix == directChannelPrefix) {
return Promise.resolve(channelName);
} else if (altChannelURLPrefixes.has(prefix)) {
return fetchChannelID(channelURL);
} else if (channelName === "") {
return fetchChannelID(channelURL);
} else {
console.error(
"Unexpected channel URL prefix while processing " + channelURL
);
return Promise.resolve("");
}
}
/**
* In a user's subscription list, some channel URLs use an alias or an
* account username instead of a direct channel ID. So this function
* fetches the HTML of a channel to retrieve the channel ID.
*
* @param {string} channelURL - the URL of a YouTube channel
* @return {Promise} - a Promise of the channel ID string
*/
async function fetchChannelID(channelURL) {
return fetch(channelURL)
.then((response) => response.text())
.then((channelPageSource) => {
let parser = new DOMParser(),
doc = parser.parseFromString(channelPageSource, "text/html");
return doc
.querySelector("meta[itemprop='channelId']")
.getAttribute("content");
});
}
/**
* Given the HTML element of a subscription from the YouTube sidebar,
* return the title and link of the channel.
*
* @param {HTMLElement} subscriptionHTMLElement - contains the relevant
* information about a subscription
* @return {Promise} - resolve to an object containing the name of the channel
* and the link to the channel's page
*/
function getNamesAndLinks(subscriptionHTMLElement) {
return Promise.resolve({
channelName: subscriptionHTMLElement.title,
channelLink: subscriptionHTMLElement.href,
});
}
/**
* Given an object with the channel's name and link, add the channel's ID.
*
* @param {Object} channelNameAndLink - an object with the channel's name and link
* @return {Promise} - reoslves to the input object with an added attribute
* for the channel ID
*/
function addChannelID(channelNameAndLink) {
return R.pipe(
R.prop("channelLink"),
getChannelID,
R.andThen(R.objOf("channelID"))
)(channelNameAndLink).then((channelID) => {
return R.mergeLeft(channelNameAndLink, channelID);
});
}
/**
* Given the channelID of a subscription, adds the link to it's RS feed.
*
* @param {Object} channelInfo - an object containing the channel's info
* @return {string} - the input object with an added attribute for the channel's
* RSS link
*/
function addChannelRSSLink(channelInfo) {
return R.assoc(
"channelRSSLink",
"https://www.youtube.com/feeds/videos.xml?channel_id=" +
channelInfo.channelID,
channelInfo
);
}
/**
* Given an object with the channel's info, add an attribute containing the
* XML element to be added to the OMPL file.
*
* @param {Object} channelInfo - an object containng the channel's info
* @return {Object} the input object with an added attribute for the XML element
*
*/
function addChannelXMLString(channelInfo) {
return R.assoc(
"channelXMLString",
"<outline xmlUrl='" + channelInfo.channelRSSLink + "'/>",
channelInfo
);
}
/**
* Generates an empty OPML document that the subscriptions will be added to.
*
* @return {XMLDocument} an empty OPML document
*/
function getEmptyOPMLDocument() {
const doc = document.implementation.createDocument(null, "opml", null);
const body = doc.createElement("body");
const outerOutline = doc.createElement("outline");
outerOutline.setAttribute("text", "My Youtube Subscriptions");
outerOutline.setAttribute("title", "My Youtube Subscriptions");
body.appendChild(outerOutline);
doc.documentElement.appendChild(body);
return doc;
}
/**
* Prompts the user to download the OPML file.
*/
function promptDownload(xmlDocument) {
var filename = "subs.xml";
var pom = document.createElement("a");
var blob = new Blob([new XMLSerializer().serializeToString(xmlDocument)], {
type: "text/plain",
});
pom.setAttribute("href", window.URL.createObjectURL(blob));
pom.setAttribute("download", filename);
pom.dataset.downloadurl = ["text/plain", pom.download, pom.href].join(":");
pom.draggable = true;
pom.classList.add("dragout");
pom.click();
}
/**
* Creates modal to display logging information to user.
*
* @param {string} modalID - the ID attribute for the modal
* @return {HTMLElement} the modal
*/
function makeModal(modalID) {
var modalContainer = document.createElement("div"),
overlay = document.createElement("div"),
dialog = document.createElement("div"),
header = document.createElement("header"),
modalContent = document.createElement("div"),
footer = document.createElement("footer");
modalContainer.setAttribute("id", modalID);
modalContainer.className = "modal micromodal-slide";
modalContainer.setAttribute("aria-hidden", true);
overlay.setAttribute("tabindex", -1);
overlay.setAttribute("data-micromodal-close", "");
overlay.className = "modal__overlay";
overlay.setAttribute(
"style",
overlay.getAttribute("style") + ";z-index:5000;"
);
modalContainer.appendChild(overlay);
dialog.setAttribute("role", "dialog");
dialog.setAttribute("aria-modal", true);
dialog.setAttribute("aria-labelledby", "modal-1-title");
dialog.className = "modal__container";
overlay.appendChild(dialog);
var heading = document.createElement("h2");
heading.setAttribute("id", "modal-1-title");
heading.className = "modal__title";
heading.innerHTML = "YouTube Subscription Exporter";
var closeButton = document.createElement("button");
closeButton.setAttribute("aria-label", "Close modal");
closeButton.setAttribute("data-micromodal-close", "");
closeButton.className = "modal__close";
header.className = "modal__header";
header.appendChild(heading);
header.appendChild(closeButton);
dialog.appendChild(header);
modalContent.setAttribute("id", "modal-1-content");
modalContent.className = "modal__content";
dialog.appendChild(modalContent);
footer.className = "modal_footer";
var continueButton = document.createElement("button"),
closeButtonFooter = document.createElement("button");
continueButton.className = "modal__btn modal__btn-primary";
continueButton.innerHTML = "Continue";
closeButtonFooter.className = "modal__btn";
closeButtonFooter.setAttribute("data-micromodal-close", "");
closeButtonFooter.setAttribute("aria-label", "Close this dialog window");
closeButtonFooter.innerHTML = "Close";
//footer.appendChild(continueButton);
footer.appendChild(closeButtonFooter);
dialog.appendChild(footer);
document.body.appendChild(modalContainer);
return modalContainer;
}
/**
* Opens collapsible side panel on YouTube pages and returns the
* subscriptions element.
*
* @return {Promise} - a promise that resolves to the (potentially unexpanded)
* subscriptions list
*/
function openSidePanel() {
if (!document.getElementById("contentContainer").hasAttribute("opened")) {
document.querySelector("#guide-icon").click();
}
return new Promise((resolve, reject) => {
const interval = setInterval(() => {
let subscriptionHTMLElements = document.querySelectorAll(
"div#sections ytd-guide-section-renderer:nth-child(2) a#endpoint[href]"
);
if (subscriptionHTMLElements.length > 0) {
// there is a delay between the click and when the subscriptions
// section populates
clearInterval(interval);
resolve(
document.querySelector(
"div#sections ytd-guide-section-renderer:nth-child(2)"
)
);
}
}, 500);
});
}
/**
* Expands the subscriptions section to display all subscriptions.
*
* @param {HTMLElement} subscriptionsSection - the subscriptions section
* @return {Promise} - resolves to the NodeList of subscriptions HTMLElements
*
*/
function getAllSubscriptions(subscriptionsSection) {
let remainingSubscriptionsContainer = subscriptionsSection.querySelector(
"ytd-guide-collapsible-entry-renderer"
),
showMoreSubscriptionsButton =
remainingSubscriptionsContainer.querySelector("yt-icon");
if (!showMoreSubscriptionsButton.hasAttribute("expanded")) {
showMoreSubscriptionsButton.click();
}
return new Promise((resolve, reject) => {
// the subscriptions list might take a while to populate
const interval = setInterval(() => {
if (remainingSubscriptionsContainer.hasAttribute("expanded")) {
let expandedSubscriptions =
subscriptionsSection.querySelectorAll("a#endpoint[href]");
clearInterval(interval);
resolve(expandedSubscriptions);
}
}, 500);
});
}
function main() {
const modal = makeModal("modal-1"),
userLog = modal.querySelector("#modal-1-content");
MicroModal.init();
MicroModal.show("modal-1");
const createMessageElement = (msg) => {
let ele = document.createElement("p");
ele.innerHTML = msg;
return ele;
};
const displayMsg = (msg) =>
R.tap((x) => userLog.appendChild(createMessageElement(msg))),
displayMsgPromise = (msg) => (x) =>
Promise.resolve(
R.tap((x) => userLog.appendChild(createMessageElement(msg)), x)
);
const addChild = (element, child) => {
element.appendChild(child);
return element;
};
R.pipeWith(R.andThen)([
// all the async functions
displayMsgPromise("Collecting subscriptions..."),
openSidePanel,
getAllSubscriptions,
R.map(getNamesAndLinks),
displayMsgPromise("Retrieving channel IDs..."),
R.bind(Promise.all, Promise),
R.map(addChannelID),
R.bind(Promise.all, Promise),
])().then((channelInfo) => {
const parser = new DOMParser(),
convertStringToXML = R.curry(R.bind(parser.parseFromString, parser)),
doc = getEmptyOPMLDocument(),
root = doc.querySelector("outline"),
modalContent = modal.querySelector("#modal-1-content"),
addSubscriptionlToXMLDoc = R.curry(addChild)(root);
R.pipe(
R.map(addChannelRSSLink),
displayMsg("Getting RSS links..."),
R.map(addChannelXMLString),
R.map(R.prop("channelXMLString")),
displayMsg("Creating OPML file..."),
R.map(convertStringToXML(R.__, "text/xml")),
R.map((d) => d.querySelector("outline")),
R.map((n) => doc.adoptNode(n)),
R.forEach(addSubscriptionlToXMLDoc),
displayMsg("Done!")
)(channelInfo);
promptDownload(doc);
});
}
GM_addStyle(GM_getResourceText("microModalStyle"));
GM_registerMenuCommand("Export RSS (OPML)", main, "x");