-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathyandexmusic_add_check_on_musicbrainz.user.js
More file actions
321 lines (290 loc) · 13.7 KB
/
yandexmusic_add_check_on_musicbrainz.user.js
File metadata and controls
321 lines (290 loc) · 13.7 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
// ==UserScript==
// @name Yandex Music → MusicBrainz: Check Exists
// @version 1.0.0
// @description Добавляет рядом с "Add to MusicBrainz" и "Search on MusicBrainz" кнопку "Check on MusicBrainz", которая ищет релиз/релиз-группу через API и показывает результат панелью.
// @match https://music.yandex.ru/*
// @run-at document-idle
// @grant GM_xmlhttpRequest
// @connect musicbrainz.org
// @icon https://musicbrainz.org/favicon.ico
// @downloadURL https://github.com/Druidblack/MusicBrainz-UserScripts/raw/main/yandexmusic_add_check_on_musicbrainz.user.js
// @updateURL https://github.com/Druidblack/MusicBrainz-UserScripts/raw/main/yandexmusic_add_check_on_musicbrainz.user.js
// ==/UserScript==
(function () {
'use strict';
const BTN_ID = 'mb-check-exists-btn';
const PANEL_ID = 'mb-check-panel';
const MB_BASE = 'https://musicbrainz.org/ws/2';
// ---------- helpers ----------
const qs = (s, r=document) => r.querySelector(s);
const qsa = (s, r=document) => Array.from(r.querySelectorAll(s));
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
function styleButton(el) {
el.style.backgroundColor = '#ffcc33';
el.style.border = 'none';
el.style.outline = 'none';
el.style.borderRadius = '20px';
el.style.color = '#121212';
el.style.cursor = 'pointer';
el.style.textDecoration = 'none';
el.style.transition = 'background-color 0.2s ease';
el.style.padding = '0 18px';
el.style.height = '36px';
el.style.display = 'inline-flex';
el.style.alignItems = 'center';
el.style.fontWeight = '600';
el.style.whiteSpace = 'nowrap';
el.addEventListener('mouseenter', () => (el.style.backgroundColor = '#ffd34d'));
el.addEventListener('mouseleave', () => (el.style.backgroundColor = '#ffcc33'));
}
function escapeHtml(s) {
return String(s)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
const sanitize = s => (s || '').replace(/\s+/g, ' ').trim();
const stripExtras = t => sanitize(t.replace(/[\[(].*?[\])]/g, ''));
const uniqBy = (arr, key) => { const seen = new Set(); return arr.filter(it => (seen.has(key(it)) ? false : seen.add(key(it)))); };
// ---------- panel ----------
function createPanel() {
let panel = document.getElementById(PANEL_ID);
if (panel) return panel;
panel = document.createElement('div');
panel.id = PANEL_ID;
Object.assign(panel.style, {
position:'fixed', right:'16px', bottom:'16px', maxWidth:'560px',
background:'rgba(18,18,18,0.98)', color:'#fff',
border:'1px solid rgba(255,255,255,0.12)', borderRadius:'14px',
padding:'12px 14px', boxShadow:'0 10px 30px rgba(0,0,0,0.4)',
fontFamily:'system-ui,-apple-system,Segoe UI,Roboto,Ubuntu,Cantarell,Noto Sans,sans-serif',
zIndex: 999999
});
panel.innerHTML =
'<div style="display:flex;gap:10px;align-items:center;justify-content:space-between">' +
'<strong>MusicBrainz</strong>' +
'<button id="mb-close" style="all:unset;cursor:pointer;padding:6px">✕</button>' +
'</div><div id="mb-body" style="margin-top:8px;font-size:14px;line-height:1.4"></div>';
document.body.appendChild(panel);
panel.querySelector('#mb-close').addEventListener('click', () => panel.remove());
return panel;
}
// ---------- meta extraction (как в твоём файле) ----------
function extractAlbumMeta() {
const rawTitle =
(qs('span.PageHeaderTitle_title__caKyB')?.textContent || '').trim() ||
(qs('.page-album__title span.deco-typo')?.textContent || '').trim() ||
(qs('.page-album__title')?.textContent || '').trim();
let albumTitle = rawTitle;
const entityStr = qs('div.PageHeaderBase_entityName__9Sj_Q')?.textContent?.trim() || '';
const metaArtists = qsa('div.PageHeaderAlbumMeta_meta__zsMI8 span.PageHeaderAlbumMeta_artistLabel__2WZSM')
.map(el => el.textContent.trim())
.filter(Boolean);
let audioAuthors = [];
let artistList = metaArtists.slice();
let relPrimary = 'Album';
let relSecondary = null;
if (entityStr.toUpperCase().includes('АУДИО')) {
relPrimary = 'Other';
relSecondary = 'Audiobook';
const m = rawTitle.match(/«([^»]+)»/);
if (m) albumTitle = m[1];
const authorsPart = rawTitle.split('. «')[0];
audioAuthors = authorsPart.split(',').map(a => a.trim()).filter(Boolean);
artistList = audioAuthors.concat(metaArtists);
} else if (entityStr === 'Сингл') {
relPrimary = 'Single';
}
const yearMatch = (qs('div.PageHeaderAlbumMeta_year__2X3NO')?.textContent || '').match(/(\d{4})/);
const year = yearMatch ? yearMatch[1] : null;
return {
albumTitle: sanitize(albumTitle),
albumTitleStripped: stripExtras(albumTitle),
artistList: artistList.map(sanitize).filter(Boolean),
year, relPrimary, relSecondary,
};
}
// ---------- MusicBrainz API (CORS-safe) ----------
function mbGet(pathAndQuery) {
const url = `${MB_BASE}${pathAndQuery}${pathAndQuery.includes('?') ? '&' : '?'}fmt=json`;
if (typeof GM_xmlhttpRequest === 'function') {
return new Promise((resolve, reject) => {
GM_xmlhttpRequest({
method: 'GET',
url,
headers: { Accept: 'application/json' },
responseType: 'json',
timeout: 15000,
onload: (res) => {
try {
if (res.status < 200 || res.status >= 300) return reject(new Error(`MusicBrainz HTTP ${res.status}`));
resolve(res.response ?? JSON.parse(res.responseText || 'null'));
} catch (e) { reject(e); }
},
onerror: () => reject(new Error('Network error')),
ontimeout: () => reject(new Error('Timeout')),
});
});
}
// fallback (если внезапно нет грантов)
return fetch(url, { headers: { Accept: 'application/json' } }).then(r => {
if (!r.ok) throw new Error(`MusicBrainz HTTP ${r.status}`);
return r.json();
});
}
const rgQuery = (m, stripped=false) => {
const t = stripped ? m.albumTitleStripped : m.albumTitle;
const parts = [`releasegroup:"${t.replace(/"/g,'\\"')}"`];
m.artistList.forEach(a => parts.push(`artist:"${a.replace(/"/g,'\\"')}"`));
if (m.year) parts.push(`firstreleaseyear:${m.year}`);
if (m.relPrimary === 'Single') parts.push('primarytype:single');
if (m.relPrimary === 'Album') parts.push('primarytype:album');
return parts.join(' AND ');
};
const relQuery = (m, stripped=false) => {
const t = stripped ? m.albumTitleStripped : m.albumTitle;
const parts = [`release:"${t.replace(/"/g,'\\"')}"`];
m.artistList.forEach(a => parts.push(`artist:"${a.replace(/"/g,'\\"')}"`));
if (m.year) parts.push(`date:${m.year}`);
return parts.join(' AND ');
};
async function searchReleaseGroups(meta) {
const queries = [rgQuery(meta,false)];
if (meta.albumTitleStripped && meta.albumTitleStripped !== meta.albumTitle) queries.push(rgQuery(meta,true));
const results = [];
for (const q of queries) {
const data = await mbGet(`/release-group/?query=${encodeURIComponent(q)}&limit=10`);
(data['release-groups'] || []).forEach(rg => {
results.push({
kind:'release-group', id:rg.id, title:rg.title,
score:rg.score ?? 0,
firstReleaseDate: rg['first-release-date'] || '',
primaryType: rg['primary-type'] || '',
artistCredit: (rg['artist-credit'] || []).map(ac => ac.name).join(', '),
url:`https://musicbrainz.org/release-group/${rg.id}`,
});
});
await sleep(250);
}
const seen = new Set();
return results.filter(x => (seen.has(x.id) ? false : seen.add(x.id))).sort((a,b)=>b.score-a.score);
}
async function searchReleases(meta) {
const queries = [relQuery(meta,false)];
if (meta.albumTitleStripped && meta.albumTitleStripped !== meta.albumTitle) queries.push(relQuery(meta,true));
const results = [];
for (const q of queries) {
const data = await mbGet(`/release/?query=${encodeURIComponent(q)}&limit=10`);
(data['releases'] || []).forEach(rel => {
results.push({
kind:'release', id:rel.id, title:rel.title,
score:rel.score ?? 0,
date: rel.date || '',
status: rel.status || '',
artistCredit: (rel['artist-credit'] || []).map(ac => ac.name).join(', '),
url:`https://musicbrainz.org/release/${rel.id}`,
});
});
await sleep(250);
}
const seen = new Set();
return results.filter(x => (seen.has(x.id) ? false : seen.add(x.id))).sort((a,b)=>b.score-a.score);
}
function renderResults(meta, rgList, relList) {
const panel = createPanel();
const body = panel.querySelector('#mb-body');
const found = rgList.length + relList.length;
const title = meta.albumTitle;
const artists = meta.artistList.join(', ');
if (!found) {
body.innerHTML =
`<div><strong>Не найдено</strong> по «${escapeHtml(title)}» — ${escapeHtml(artists)}${meta.year ? ' (' + meta.year + ')' : ''}.</div>` +
`<div style="margin-top:6px"><a target="_blank" href="https://musicbrainz.org/search?query=${encodeURIComponent(title)}&type=release">Открыть поиск вручную</a></div>`;
return;
}
const mk = (it) => {
const small = it.kind === 'release-group'
? `${it.primaryType || 'RG'}${it.firstReleaseDate ? ' • ' + it.firstReleaseDate : ''}`
: `${it.status || 'Release'}${it.date ? ' • ' + it.date : ''}`;
return `
<div style="padding:6px 0;border-bottom:1px solid rgba(255,255,255,0.1)">
<div style="display:flex;justify-content:space-between;gap:8px;align-items:baseline">
<a href="${it.url}" target="_blank" style="color:#9ecbff;text-decoration:none">${escapeHtml(it.title)}</a>
<span style="opacity:0.7;font-size:12px">score: ${it.score}</span>
</div>
<div style="opacity:0.9;font-size:13px;margin-top:2px">${escapeHtml(it.artistCredit)}</div>
<div style="opacity:0.7;font-size:12px">${escapeHtml(small)}</div>
</div>`;
};
body.innerHTML =
`<div style="margin-bottom:6px"><strong>Найдено: ${found}</strong> по «${escapeHtml(title)}» — ${escapeHtml(artists)}${meta.year ? ' (' + meta.year + ')' : ''}.</div>` +
rgList.slice(0,5).map(mk).join('') +
relList.slice(0,5).map(mk).join('') +
`<div style="margin-top:6px"><a target="_blank" href="https://musicbrainz.org/search?query=${encodeURIComponent(title)}&type=release">Полный поиск на MusicBrainz</a></div>`;
}
// ---------- injection: рядом с твоим .mb-buttons ----------
function attachButton() {
// только на страницах альбомов
if (!/\/album\//.test(location.pathname)) return;
// ждём контейнер из твоего скрипта
const wrap = qs('.mb-buttons');
if (!wrap) return;
// не дублируем кнопку
if (wrap.querySelector('#' + BTN_ID)) return;
// делаем кнопку такой же, как твои
const btn = document.createElement('button');
btn.id = BTN_ID;
btn.type = 'button';
btn.textContent = 'Check on MusicBrainz';
styleButton(btn);
// Ставим В КОНЕЦ контейнера (рядом с Add/Search)
wrap.appendChild(btn);
// Клик — запрос и панель с результатами
btn.addEventListener('click', async () => {
const panel = createPanel();
const body = panel.querySelector('#mb-body');
body.textContent = 'Ищем в MusicBrainz…';
const meta = extractAlbumMeta();
if (!meta.albumTitle || !meta.artistList.length) {
body.textContent = 'Не удалось извлечь название/исполнителя со страницы.';
return;
}
try {
const [rg, rel] = await Promise.all([searchReleaseGroups(meta), searchReleases(meta)]);
renderResults(meta, rg, rel);
} catch (e) {
body.textContent = 'Ошибка запроса: ' + e.message;
}
});
// Чуть подогнать размеры под «Слушать»
const listen = qs('div.PageHeaderBase_controls__HzGgE button[aria-label="Воспроизведение"]');
if (listen) {
const cs = getComputedStyle(listen);
btn.style.height = cs.height;
btn.style.borderRadius = cs.borderRadius;
btn.style.paddingTop = cs.paddingTop;
btn.style.paddingBottom = cs.paddingBottom;
btn.style.paddingLeft = cs.paddingLeft;
btn.style.paddingRight = cs.paddingRight;
btn.style.fontSize = cs.fontSize;
btn.style.fontWeight = cs.fontWeight;
btn.style.fontFamily = cs.fontFamily;
}
}
// ---------- observers (SPA/DOM) ----------
const mo = new MutationObserver(() => attachButton());
mo.observe(document.documentElement || document.body, { childList: true, subtree: true });
['pushState','replaceState'].forEach(m => {
const orig = history[m];
history[m] = function () {
const ret = orig.apply(this, arguments);
setTimeout(attachButton, 100);
return ret;
};
});
window.addEventListener('popstate', () => setTimeout(attachButton, 100));
// старт
attachButton();
})();