-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGenius-Musicbrainz-Buttons.user.js
More file actions
322 lines (300 loc) · 14.6 KB
/
Genius-Musicbrainz-Buttons.user.js
File metadata and controls
322 lines (300 loc) · 14.6 KB
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
// ==UserScript==
// @name Genius MusicBrainz Buttons
// @namespace https://github.com/Lioncat6/MusicBrainz-UserScripts/
// @match https://genius.com/artists/*
// @match https://genius.com/albums/*
// @match https://genius.com/*-lyrics
// @match https://genius.com/*-annotated
// @match https://genius.com/search?q=*
// @match https://genius.com/artists-index/*
// @homepageURL https://github.com/Lioncat6/MusicBrainz-UserScripts/
// @icon https://www.google.com/s2/favicons?sz=64&domain=genius.com
// @grant none
// @version 04-24-2026
// @author Lioncat6
// @description Adds MusicBrainz buttons to Genius pages
// @downloadURL https://github.com/Lioncat6/MusicBrainz-UserScripts/raw/main/Genius-Musicbrainz-Buttons.user.js
// @updateURL https://github.com/Lioncat6/MusicBrainz-UserScripts/raw/main/Genius-Musicbrainz-Buttons.user.js
// @license CC-NC
// @homepageURL https://github.com/Lioncat6/MusicBrainz-UserScripts/
// @supportURL https://github.com/Lioncat6/MusicBrainz-UserScripts/issues
// ==/UserScript==
(async function () {
'use strict';
const scriptName = "%c||Genius MusicBrainz Buttons||";
const allowed_types = ['artist', 'work', 'release_group'];
let lastSetMarker = null;
function findRetry(query) {
let target = document.querySelector(query);
let tries = 0;
const maxTries = 4;
const intervalTime = 300; // ms
return new Promise((resolve, reject) => {
const interval = setInterval(() => {
target = document.querySelector(query);
if (target) {
clearInterval(interval);
resolve(target);
} else if (tries >= maxTries) {
clearInterval(interval);
resolve(null);
}
tries++;
}, intervalTime);
});
}
function injectCss() {
const style = document.createElement('style');
style.textContent = `
.square_button--musicbrainz {
background-color: #e37333;
border-color: #e37333;
width: 40px;
}
.square_button--musicbrainz:hover {
background-color: #f08548;
border-color: #f08548;
}
.mb_icon {
transform: scale(80%) translateY(-11px) translateX(-5px)
}
.mbWorkLink {
text-decoration: underline;color: #9e9e9e;
transition: all 0.3s ease;
font-size: 0.75rem;
padding-left: 1.313rem;
}
.mbWorkLink:hover {
color: black;
text-decoration: none;
}
.mbRGLink {
text-decoration: underline;
color: #9a9a9a;
transition: all 0.3s ease;
}
.mbRGLink:hover {
color: white;
text-decoration: none;
}
.mbIconLink {
transform: translateY(-4px);
overflow: visible;
}
.mbArtistLink {
margin-left: 0.25rem;
}
`;
document.head.appendChild(style);
}
async function injectButtons(mbUrl, urlName) {
if (window.location.href.match(/genius\.com\/artists\/([^/?]+)/)) {
let linkContainer = await findRetry('.u-text_center.u-vertical_margins');
if (!linkContainer) {
console.error(`${scriptName} %cButton injection failed`, "color: cyan;", "color: red;");
return;
}
const mbButton = document.createElement('a');
mbButton.href = mbUrl;
mbButton.target = "_blank";
mbButton.className = 'square_button square_button--musicbrainz u-quarter_vertical_margins';
mbButton.title = `View '${urlName}' on MusicBrainz`;
const mbSvg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
mbSvg.setAttribute('height', '30');
mbSvg.setAttribute('width', '30');
mbSvg.className.baseVal = 'inline_icon mb_icon';
mbSvg.innerHTML = `<g><path d="m13 1-12 7v14l12 7z" fill="#ffffff"/><path d="m14 1 12 7v14l-12 7z" fill="#ffffff"/></g>`;
mbButton.appendChild(mbSvg);
linkContainer.appendChild(mbButton);
console.log(`${scriptName} %cButton injected for artist '${urlName}'`, "color: cyan;", "color: green;");
}
if (window.location.href.match(/genius\.com\/([^/?]+)-lyrics/)) {
let linkContainer = await findRetry('.ContributorSidebar__Body-sc-b06c6d43-2');
if (!linkContainer) {
console.warn(`${scriptName} %cCould not find link container by class; Falling back to id`, "color: cyan;", "color: orange;");
let linkContainerChild = document.getElementById('collapsible-song-info-content');
if (linkContainerChild) {
linkContainer = linkContainerChild.parentElement.parentElement;
} else {
console.error(`${scriptName} %cButton injection failed`, "color: cyan;", "color: red;");
return;
}
}
const mbButton = document.createElement('a');
mbButton.href = mbUrl;
mbButton.target = "_blank";
mbButton.className = 'mbWorkLink';
mbButton.innerText = 'View on MusicBrainz';
mbButton.title = `View '${urlName}' on MusicBrainz`;
mbButton.appendChild(getMarker());
linkContainer.appendChild(mbButton);
console.log(`${scriptName} %cButton injected for work '${urlName}'`, "color: cyan;", "color: green;");
}
if (window.location.href.match(/genius\.com\/albums\/([^/?]+)/)) {
let linkContainer = await findRetry('.header_with_cover_art-primary_info');
if (!linkContainer) {
console.error(`${scriptName} %cButton injection failed`, "color: cyan;", "color: red;");
return;
}
const mbButton = document.createElement('a');
mbButton.href = mbUrl;
mbButton.target = "_blank";
mbButton.className = 'mbRGLink';
mbButton.innerText = 'View on MusicBrainz';
mbButton.title = `View '${urlName}' on MusicBrainz`;
mbButton.appendChild(getMarker());
linkContainer.appendChild(mbButton);
console.log(`${scriptName} %cButton injected for release group '${urlName}'`, "color: cyan;", "color: green;");
}
}
async function fetchUrl(url= undefined) {
let mbUrl = null;
let urlName = null;
const pageUrl = url || window.location.href;
let response = await fetch(`https://musicbrainz.org/ws/2/url?fmt=json&resource=${encodeURIComponent(pageUrl)}&inc=artist-rels+work-rels+release-group-rels`);
if (response.ok) {
console.log(`${scriptName} %cResult found for ${pageUrl}`, "color: cyan;", "color: green;");
let data = await response.json();
if (data.relations && data.relations.length > 0) {
for (let rel of data.relations) {
if (allowed_types.includes(rel['target-type']) && rel.ended != true) {
console.log(`${scriptName} %cFound MB id: ${rel[rel['target-type']]?.id} for ${rel['target-type']} ${rel[rel['target-type']]?.name || rel[rel['target-type']]?.title}`, "color: cyan;", `color: magenta; font-style: italic;`);
mbUrl = `https://musicbrainz.org/${rel['target-type'].replace(/_/g, '-')}/${rel[rel['target-type']]?.id}`;
urlName = rel[rel['target-type']]?.name || rel[rel['target-type']]?.title;
break;
}
}
}
} else if (response.status === 404) {
console.log(`${scriptName} %cNo result found for ${pageUrl}`, "color: cyan;", "color: orange;");
} else {
console.error(`${scriptName} %cError fetching data for ${pageUrl}: ${response.status} ${response.statusText}`, "color: cyan;", "color: red;");
}
return { mbUrl, urlName, resource: pageUrl };
}
async function fetchUrls(urls) {
if (urls.length === 0 || !urls) return [];
if (urls.length == 1) {
const urlData = await fetchUrl(urls[0]);
return urlData ? [urlData] : [];
}
const results = [];
const batchSize = 100;
for (let i = 0; i < urls.length; i += batchSize) {
const batch = urls.slice(i, i + batchSize);
let response = await fetch(`https://musicbrainz.org/ws/2/url?fmt=json${batch.map(url => `&resource=${encodeURIComponent(url)}`).join('')}&inc=artist-rels+work-rels+release-group-rels`);
if (response.ok) {
let data = await response.json();
for (let url of data.urls) {
let mbUrl = null;
let urlName = null;
if (url.relations && url.relations.length > 0) {
for (let rel of url.relations) {
if (allowed_types.includes(rel['target-type']) && rel.ended != true) {
mbUrl = `https://musicbrainz.org/${rel['target-type'].replace(/_/g, '-')}/${rel[rel['target-type']]?.id}`;
urlName = rel[rel['target-type']]?.name || rel[rel['target-type']]?.title;
break;
}
}
}
if (mbUrl) {
results.push({ mbUrl, urlName, resource: url.resource });
console.log(`${scriptName} %cFound MB id: ${mbUrl} for URL ${url.resource}`, "color: cyan;", "color: magenta; font-style: italic;");
}
}
} else {
console.error(`${scriptName} %cError fetching data for URLs: ${response.status} ${response.statusText}`, "color: cyan;", "color: red;");
}
}
return results;
}
async function checkArtistLinks() {
const blackListClasses = ['mini_card'];
let linkElements = Array.from(document.querySelectorAll('a[href*="genius.com/artists/"]')).filter((element) => element.className.split(" ").every((className) => !blackListClasses.includes(className)));
console.log(`${scriptName} %cChecking ${linkElements.length} artist links`, "color: cyan;", "color: blue;");
let urlsToCheck = linkElements ? Array.from(linkElements).map(el => el.href) : [];
let urlData = await fetchUrls(Array.from(new Set(urlsToCheck)));
linkElements.forEach(el => {
let urlInfo = urlData.find(data => data.resource === el.href);
if (urlInfo && urlInfo.mbUrl) {
const mbLink = document.createElement('a');
mbLink.href = urlInfo.mbUrl;
mbLink.target = "_blank";
mbLink.innerHTML = `<svg height="1em" width="1em" viewBox="0 0 26 16" class="mbIconLink" style="color: ${document.defaultView.getComputedStyle(el).color || 'inherit'}; display: inline; vertical-align: -0.125em;"><g><path d="m13 1-12 7v14l12 7z" fill="currentColor"/><path d="m14 1 12 7v14l-12 7z" fill="currentColor"/></g></svg>`;
mbLink.title = `View '${urlInfo.urlName}' on MusicBrainz`;
mbLink.className = 'mbArtistLink';
mbLink.appendChild(getMarker());
el.parentElement.insertBefore(mbLink, el.nextSibling);
console.log(`${scriptName} %cAdded MB link for artist URL ${el.href}`, "color: cyan;", "color: green;");
}
});
}
function getMarker() {
const marker = document.createElement('meta');
marker.className = 'genius-musicbrainz-buttons-injected';
return marker;
}
function isInjected() {
return document.querySelector('.genius-musicbrainz-buttons-injected') !== null;
}
function setRoutableMarker() {
const routableParent = document.querySelector('routable-page');
if (routableParent) {
const marker = getMarker();
marker.id='genius-musicbrainz-buttons-routable-marker';
routableParent.firstElementChild.firstElementChild.appendChild(marker);
}
}
async function runInjector() {
try {
if (!isInjected()) {
console.log(`${scriptName} %cRunning injector`, "color: cyan;", "color: yellow;");
const { mbUrl, urlName, resource } = await fetchUrl();
try {
injectCss();
} catch (error) {
console.error(`${scriptName} %cError injecting CSS`, "color: cyan;", "color: red;", error);
}
if (mbUrl) {
try {
await injectButtons(mbUrl, urlName);
} catch (error) {
console.error(`${scriptName} %cError injecting buttons`, "color: cyan;", "color: red;", error);
}
}
try {
await checkArtistLinks();
} catch (error) {
console.error(`${scriptName} %cError injecting artist links`, "color: cyan;", "color: red;", error);
}
try {
setRoutableMarker();
} catch (error) {
console.error(`${scriptName} %cError setting routable marker`, "color: cyan;", "color: red;", error);
}
} else {
console.log("b")
}
} catch (error) {
console.error(`${scriptName} %cError running script`, "color: cyan;", "color: red;", error);
}
}
function watchRoutableMarker() {
console.log(`${scriptName} %cWatching page change...`, "color: cyan;", "color: yellow;");
let watcher = setInterval(async () => {
if (!document.getElementById('genius-musicbrainz-buttons-routable-marker')) {
clearInterval(watcher);
await runInjector();
}
}, 100)
}
await runInjector();
const originalPushState = history.pushState;
history.pushState = function () { //Watching for location changes, since genius is partially react
originalPushState.apply(this, arguments);
watchRoutableMarker();
};
window.addEventListener("popstate", (event) => { //Watching for the back/forward buttons being used
watchRoutableMarker();
});
})();