-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyoutube-link-canonicalizer.user.js
417 lines (384 loc) · 16.3 KB
/
youtube-link-canonicalizer.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// ==UserScript==
// @name MusicBrainz Youtube Link Canonicalizer
// @version 2024.8.12
// @namespace https://github.com/zabe40
// @author zabe
// @description Correct youtube @username artist links to channel IDs
// @homepageURL https://github.com/zabe40/musicbrainz-userscripts#musicbrainz-youtube-link-canonicalizer
// @downloadURL https://raw.github.com/zabe40/musicbrainz-userscripts/main/dist/youtube-link-canonicalizer.user.js
// @updateURL https://raw.github.com/zabe40/musicbrainz-userscripts/main/dist/youtube-link-canonicalizer.user.js
// @supportURL https://github.com/zabe40/musicbrainz-userscripts/issues
// @grant GM_xmlhttpRequest
// @connect youtube.com
// @connect musicbrainz.org
// @match *://*.musicbrainz.org/artist/*
// @match *://*.musicbrainz.org/event/*
// @match *://*.musicbrainz.org/label/*
// @match *://*.musicbrainz.org/place/*
// @match *://*.musicbrainz.org/series/*
// @match *://*.musicbrainz.org/url/*
// @match *://*.musicbrainz.org/dialog*
// @match *://*.musicbrainz.eu/artist/*
// @match *://*.musicbrainz.eu/event/*
// @match *://*.musicbrainz.eu/label/*
// @match *://*.musicbrainz.eu/place/*
// @match *://*.musicbrainz.eu/series/*
// @match *://*.musicbrainz.eu/url/*
// @match *://*.musicbrainz.eu/dialog*
// ==/UserScript==
(function () {
'use strict';
// Adapted from https://stackoverflow.com/a/46012210
const nativeInputValueSetter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value').set;
/**
* Sets the value of an input element which has been manipulated by React.
* @param {HTMLInputElement} input
* @param {string} value
*/
function setReactInputValue(input, value) {
nativeInputValueSetter.call(input, value);
input.dispatchEvent(new Event('input', { bubbles: true }));
}
const nativeTextareaValueSetter = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value').set;
/**
* Sets the value of a textarea input element which has been manipulated by React.
* @param {HTMLTextAreaElement} input
* @param {string} value
*/
function setReactTextareaValue(input, value) {
nativeTextareaValueSetter.call(input, value);
input.dispatchEvent(new Event('input', { bubbles: true }));
}
/**
* Returns the first element that is a descendant of node that matches selectors.
* @param {string} selectors
* @param {ParentNode} node
*/
function qs(selectors, node = document) {
return node.querySelector(selectors);
}
/**
* Extracts the entity type and ID from a MusicBrainz URL (can be incomplete and/or with additional path components and query parameters).
* @param {string} url URL of a MusicBrainz entity page.
* @returns {{ type: CoreEntityTypeT | 'mbid', mbid: MB.MBID } | undefined} Type and ID.
*/
function extractEntityFromURL(url) {
const entity = url.match(/(area|artist|event|genre|instrument|label|mbid|place|recording|release|release-group|series|url|work)\/([0-9a-f-]{36})(?:$|\/|\?)/);
return entity ? {
type: entity[1],
mbid: entity[2]
} : undefined;
}
/**
* Returns a promise that resolves after the given delay.
* @param {number} ms Delay in milliseconds.
*/
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
// Adapted from https://thoughtspile.github.io/2018/07/07/rate-limit-promises/
function rateLimitedQueue(operation, interval) {
let queue = Promise.resolve(); // empty queue is ready
return (...args) => {
const result = queue.then(() => operation(...args)); // queue the next operation
// start the next delay, regardless of the last operation's success
queue = queue.then(() => delay(interval), () => delay(interval));
return result;
};
}
/**
* Limits the number of requests for the given operation within a time interval.
* @template Params
* @template Result
* @param {(...args: Params) => Result} operation Operation that should be rate-limited.
* @param {number} interval Time interval (in ms).
* @param {number} requestsPerInterval Maximum number of requests within the interval.
* @returns {(...args: Params) => Promise<Awaited<Result>>} Rate-limited version of the given operation.
*/
function rateLimit(operation, interval, requestsPerInterval = 1) {
if (requestsPerInterval == 1) {
return rateLimitedQueue(operation, interval);
}
const queues = Array(requestsPerInterval).fill().map(() => rateLimitedQueue(operation, interval));
let queueIndex = 0;
return (...args) => {
queueIndex = (queueIndex + 1) % requestsPerInterval; // use the next queue
return queues[queueIndex](...args); // return the result of the operation
};
}
/**
* Calls to the MusicBrainz API are limited to one request per second.
* https://musicbrainz.org/doc/MusicBrainz_API
*/
const callAPI = rateLimit(fetch, 1000);
/**
* Makes a request to the MusicBrainz API of the currently used server and returns the results as JSON.
* @param {string} endpoint Endpoint (e.g. the entity type) which should be queried.
* @param {Record<string,string>} query Query parameters.
* @param {string[]} inc Include parameters which should be added to the query parameters.
*/
async function fetchFromAPI(endpoint, query = {}, inc = []) {
if (inc.length) {
query.inc = inc.join(' '); // spaces will be encoded as `+`
}
query.fmt = 'json';
const headers = {
'Accept': 'application/json',
// 'User-Agent': 'Application name/<version> ( contact-url )',
};
const response = await callAPI(`https://musicbrainz.org/ws/2/${endpoint}?${new URLSearchParams(query)}`, { headers });
if (response.ok) {
return response.json();
} else {
throw response;
}
}
function fetchURL(url, options){
return new Promise((resolve, reject) => {
GM_xmlhttpRequest({
url: url,
onload: function(response){
if(400 <= response.status){
reject(new Error(`HTTP error! Status: ${response.status}`,
{ cause: response}));
}else {
resolve(response);
}
},
onabort: function(error){
reject(new Error("The request was aborted.",
{ cause: error}));
},
onerror: function(error){
reject(new Error("There was an error with the request. See the console for more details.",
{ cause: error}));
},
ontimeout: function(error){
reject(new Error("The request timed out.",
{ cause: error}));
},
...options,
});
});
}
/**
* Adds the given message and a footer for the active userscript to the edit note.
* @param {string} message Edit note message.
*/
function addMessageToEditNote(message) {
/** @type {HTMLTextAreaElement} */
const editNoteInput = qs('#edit-note-text, .edit-note');
const previousContent = editNoteInput.value.split(editNoteSeparator);
setReactTextareaValue(editNoteInput, buildEditNote(...previousContent, message));
}
/**
* Builds an edit note for the given message sections and adds a footer section for the active userscript.
* Automatically de-duplicates the sections to reduce auto-generated message and footer spam.
* @param {...string} sections Edit note sections.
* @returns {string} Complete edit note content.
*/
function buildEditNote(...sections) {
sections = sections.map((section) => section.trim());
if (typeof GM_info !== 'undefined') {
sections.push(`${GM_info.script.name} (v${GM_info.script.version}, https://github.com/zabe40/musicbrainz-userscripts#musicbrainz-youtube-link-canonicalizer)`);
}
// drop empty sections and keep only the last occurrence of duplicate sections
return sections
.filter((section, index) => section && sections.lastIndexOf(section) === index)
.join(editNoteSeparator);
}
const editNoteSeparator = '\n—\n';
function displayError(element, error, selector = ""){
let p = element.querySelector("p.canonicalizer-error");
if(!p){
p = document.createElement("p");
p.className = "error canonicalizer-error";
p.style.wordBreak = "break-word";
if(selector){
element = element.querySelector(selector) || element;
}
element.insertAdjacentElement("afterend", p);
}
p.textContent = error.message;
}
function clearError(element){
let p = element.querySelector("p.canonicalizer-error");
if(p){
p.remove();
}
}
function isYoutubeLink(link){
return link.match("^https://(www.)?youtube\\.com");
}
function isCanonicalYoutubeLink(link){
return link.match("^https?://(www.)?youtube\\.com/channel/");
}
function getCanonicalizedYoutubeLink(link){
return fetchURL(link).then((response) => {
const html = response.responseText;
const parser = new DOMParser();
let doc = parser.parseFromString(html, "text/html");
return doc.querySelector("link[rel=\"canonical\"]").href;
});
}
function fixLinkOnNonURLPage(span){
const tableRow = span.parentElement.parentElement;
const observer = new MutationObserver(function(mutations, observer){
mutations.forEach(function(mutation){
if(mutation.addedNodes.length > 0
&& mutation.addedNodes.item(0).querySelector("div.dialog")){
setReactInputValue(document.querySelector("div.dialog input.raw-url"), tableRow.getAttribute("newLink"));
document.querySelector("div.dialog button.positive").click();
observer.disconnect();
addMessageToEditNote(tableRow.getAttribute("oldLink")
+ " → "
+ tableRow.getAttribute("newLink"));
}
});
});
observer.observe(document.querySelector("#url-input-popover-root") || document.body,
{ childList: true});
if(tableRow.getAttribute("newLink")){
tableRow.querySelector("td.link-actions > button.edit-item").click();
return;
}
tableRow.querySelector(".canonicalizer-button").disabled = true;
clearError(tableRow);
getCanonicalizedYoutubeLink(tableRow.querySelector("td > a").href)
.then(function(canonicalizedLink){
tableRow.setAttribute("oldLink", tableRow.querySelector("td > a").href);
tableRow.setAttribute("newLink", canonicalizedLink);
tableRow.querySelector("td.link-actions > button.edit-item").click();
})
.catch(function(error){
console.warn(error);
displayError(tableRow, error, "a.url");
observer.disconnect();
})
.finally(function(){
tableRow.querySelector(".canonicalizer-button").disabled = false;
});
}
function addFixerUpperButton(currentSpan){
const tableRow = currentSpan.parentElement.parentElement;
if(isCanonicalYoutubeLink(tableRow.querySelector("a.url").href)
|| tableRow.querySelector('.canonicalizer-button')){
return;
}
let button = document.createElement('button');
button.addEventListener("click", (function(){fixLinkOnNonURLPage(currentSpan);}));
button.type = 'button';
button.innerHTML = "Canonicalize URL";
button.className = 'styled-button canonicalizer-button';
button.style.float = 'right';
let td = document.createElement('td');
td.className = "canonicalizer-td";
td.appendChild(button);
currentSpan.parentElement.parentElement.appendChild(td);
}
function highlightNoncanonicalLinks(){
document.querySelectorAll(".external_links .youtube-favicon")
.forEach(function(listItem, currentIndex, listObj){
const ytLink = listItem.querySelector('a').href;
if(!isCanonicalYoutubeLink(ytLink)){
const linkButton = document.createElement('a');
linkButton.className = "styled-button canonicalizer-button";
linkButton.style.float = "right";
linkButton.textContent = "Fix URL";
const entity = extractEntityFromURL(document.location.href);
fetchFromAPI(entity.type + "/" + entity.mbid,
{"inc": "url-rels"})
.then((response) => {
let urlID = false;
for(const urlObject of response.relations){
if(urlObject.url.resource == ytLink){
urlID = urlObject.url.id;
break;
}
}
linkButton.href = document.location.origin + "/url/" + urlID + "/edit";
listItem.appendChild(linkButton);
})
.catch((error) => {
console.error(error);
displayError(listItem, error, ".canonicalizer-button");
});
}
});
}
function runUserscript(){
highlightNoncanonicalLinks();
const target = document.querySelector("#external-links-editor-container");
if(target){
const observer = new MutationObserver(function(mutations){
mutations.forEach(function(mutation){
if(mutation.addedNodes.length > 0
&& (mutation.addedNodes.item(0).id == "external-links-editor"
|| (mutation.addedNodes.item(0).classList
&& mutation.addedNodes.item(0).classList.contains("url")
&& isYoutubeLink(mutation.addedNodes.item(0).href)))){
document.querySelectorAll(".youtube-favicon")
.forEach(addFixerUpperButton);
}
if(mutation.removedNodes.length > 0
&& mutation.removedNodes.item(0).classList
&& mutation.removedNodes.item(0).classList.contains("url")){
mutation.target.nextElementSibling.remove();
const tableRow = mutation.target.parentElement;
tableRow.removeAttribute("oldLink");
tableRow.removeAttribute("newLink");
clearError(tableRow);
}
});
});
observer.observe(target, { childList: true, subtree:true});
}
}
function fixLinkURLEdit(row){
const urlInput = row.querySelector("input#id-edit-url\\.url");
const button = row.querySelector("button.canonicalizer-button");
urlInput.setAttribute("oldLink", urlInput.value);
button.disabled = true;
clearError(row);
getCanonicalizedYoutubeLink(urlInput.value)
.then((canonicalizedURL) => {
setReactInputValue(urlInput, canonicalizedURL);
addMessageToEditNote(urlInput.getAttribute("oldLink")
+ " → "
+ canonicalizedURL);
})
.catch((error) => {
console.warn(error);
displayError(row, error, ".canonicalizer-button");
})
.finally(() => {
button.disabled = false;
});
}
function runOnURLEditPage(){
const urlInput = document.querySelector("input#id-edit-url\\.url");
if(!urlInput){
return;
}
if(!isYoutubeLink(urlInput.value) || isCanonicalYoutubeLink(urlInput.value)){
return;
}
const button = document.createElement("button");
button.type = "button";
button.textContent = "Canonicalize URL";
button.className = "styled-button canonicalizer-button";
button.addEventListener("click", function(){fixLinkURLEdit(urlInput.parentElement);});
urlInput.insertAdjacentElement("afterend", button);
}
const location = document.location.href;
if(location.match("^https?://((beta|test)\\.)?musicbrainz\\.(org|eu)/dialog")){
if((new URLSearchParams(document.location.search))
.get("path").match("^/(artist|event|label|place|series)/create")){
runUserscript();
}
}else if(location.match("^https?://((beta|test)\\.)?musicbrainz\\.(org|eu)/url")){
runOnURLEditPage();
}else {
runUserscript();
}
})();