-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathEdit-Linker.user.js
More file actions
238 lines (201 loc) · 7.35 KB
/
Edit-Linker.user.js
File metadata and controls
238 lines (201 loc) · 7.35 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
// ==UserScript==
// @name Edit Linker
// @namespace http://tampermonkey.net/
// @version 2024-03-11
// @description Add link to search musicbrainz entities on mutlple sites
// @author RustyNova
// @match *://*.musicbrainz.org/recording/*
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @namespace https://github.com/RustyNova016/MusicBrainz-UserScripts/
// @downloadURL https://raw.githubusercontent.com/RustyNova016/MusicBrainz-UserScripts/main/Edit-Linker.user.js
// @updateURL https://raw.githubusercontent.com/RustyNova016/MusicBrainz-UserScripts/main/Edit-Linker.user.js
// @homepageURL https://github.com/RustyNova016/MusicBrainz-UserScripts/
// @supportURL https://github.com/RustyNova016/MusicBrainz-UserScripts/issues
// @grant none
// ==/UserScript==
'use strict';
// The list of links to add buttons for
let links = [
{
text: "Soundcloud",
link: (page_data) => `https://soundcloud.com/search?q=${page_data.recordingName} ${page_data.artistCredit}`,
color: "#f50",
favicon: "https://a-v2.sndcdn.com/assets/images/sc-icons/favicon-2cadd14bdb.ico"
},
{
text: "Spotify",
link: (page_data) => `https://open.spotify.com/search/${page_data.recordingName} ${page_data.artistCredit}`,
color: "#18c155",
favicon: "https://open.spotifycdn.com/cdn/images/favicon32.b64ecc03.png"
},
{
text: "Youtube",
link: (page_data) => `https://www.youtube.com/results?search_query=${page_data.recordingName} ${page_data.artistCredit}`,
color: "#dd2c00",
favicon: "https://www.youtube.com/s/desktop/4fdfe272/img/favicon_32x32.png"
},
]
// The list of elements to anchor the buttons on
let anchors = [
{
target: document.querySelector("#external-links-editor-container"),
after: waitForElement("#external-links-editor")
}
]
// All the functions that ask for "page_data" will recieve this object. The fields may or may not be filled depending on the page
let data = {
// The name of the recording
recordingName: "",
// The artist credit
artistCredit: ""
}
// Insert CSS into the Head
function main() {
let head = document.getElementsByTagName('head')[0];
if (head) {
let style = document.createElement('style');
style.setAttribute('type', 'text/css');
style.textContent = `
.dashbox {
padding-bottom: 4px;
}
.button-area {
display: flex;
padding: 12px
}
.button-favicon {
height: 1.25em;
margin-left: 5px;
}
.search-button {
border-radius: 5px;
border: none;
padding: 10px;
font-size: 1em;
color: white;
cursor: pointer;
display: flex;
align-items: center;
text-decoration: inherit; /* no underline */
margin: 6px
}
.search-button:hover {text-decoration: inherit; color: inherit;}
.search-button:visited {text-decoration: inherit; color: white;}
.search-button:before {
background-image: url("https://a-v2.sndcdn.com/assets/images/sc-icons/favicon-2cadd14bdb.ico");
background-size: 14px 14px;
}
`;
head.appendChild(style);
}
}
main();
// ==============================================================================================
// Beware! Beyond this point lies code! If you aren't a programmer, you may want to turn back!
// ==============================================================================================
let globalPromises = []
let form, entity, formString;
let previousUrl = '';
new MutationObserver(function(mutations) {
if (location.href != previousUrl) {
previousUrl = location.href;
console.log("EditLinker: Starting");
Promise.all(globalPromises).catch(error => {
console.log("EditLinker: " + error);
}).finally(() => {
globalPromises = [];
onUrlChange();
});
}
}).observe(document, {subtree: true, childList: true});
// Do all the logic of a page change
async function onUrlChange() {
fetchData();
for (const anch of anchors) {
let button_area = createButtonArea();
if (anch.after != undefined) {
insertAfter(await anch.after,button_area)
} else {
anch.target.appendChild(button_area);
}
}
}
// Waits until an element appear on the page. This may never happen, so this may hang!
function waitForElement(selector) {
return new Promise((resolve, reject) => {
const mut = new MutationObserver(mutations => {
const element = document.querySelector(selector);
if (element != null) {
mut.disconnect();
resolve(element);
}
});
mut.observe(document.body, {subtree: true, childList: true});
});
}
// -----------------------------
// | UI manipulation functions |
// -----------------------------
function createButtonArea() {
let fieldset = document.createElement("fieldset");
fieldset.setAttribute("class", "button-area");
// Add legend
let legend = document.createElement("legend");
legend.innerText = "Search in:";
fieldset.appendChild(legend);
// Add buttons
for (const link of links) {
fieldset.appendChild(createButton(link));
}
return fieldset;
}
function createButton(link) {
let link_button = document.createElement("a");
link_button.innerText = link.text;
link_button.setAttribute("class", "search-button");
link_button.style.backgroundColor = link.color;
link_button.href = link.link(data)
link_button.target = "_blank"
//link_button.onclick = function() {
// window.open(link.link(data));
//};
// Favicon
let favicon_image = document.createElement("img");
favicon_image.src = link.favicon;
favicon_image.setAttribute("class", "button-favicon");
link_button.appendChild(favicon_image);
return link_button;
}
// ---------------------------
// | Data Fetching functions |
// ---------------------------
function fetchData() {
fetchRecordingName();
fetchArtistCredit()
}
// Read the recorning name from the header
function fetchRecordingName() {
data.recordingName = document.querySelector(".recordingheader").children[3].innerText;
}
// Read the artist credit
function fetchArtistCredit() {
let artistsNamesElement = document.querySelector(".subheader");
// All the artists link have an "Title" property. We use that to get only the artist children
let artistElements = [];
for (const child of Array.from(artistsNamesElement.children)) {
if (child.title !== "") {
console.log(child.innerText);
artistElements.push(child.innerText);
}
}
// We put everything into a space separated string.
// While using the proper musicbrainz credit file would center the search more,
// We are dealing with search engine that could be quite strict, and so we need to dumb it down
data.artistCredit = artistElements.join(" ");
}
// -----------------------
// | Utilities functions |
// -----------------------
function insertAfter(referenceNode, newNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}