Skip to content

Commit bece75c

Browse files
authored
Merge pull request #609 from arsinclair/feat/implement-discography-page-linking
bandcamp_importer: enable MB Release links on Discography pages (release lists)
2 parents bd5b10b + 31c2503 commit bece75c

File tree

1 file changed

+124
-101
lines changed

1 file changed

+124
-101
lines changed

bandcamp_importer.user.js

Lines changed: 124 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// ==UserScript==
22
// @name Import Bandcamp releases to MusicBrainz
33
// @description Add a button on Bandcamp's album pages to open MusicBrainz release editor with pre-filled data for the selected release
4-
// @version 2024.10.17.1
4+
// @version 2025.04.22.1
55
// @namespace http://userscripts.org/users/22504
66
// @downloadURL https://raw.github.com/murdos/musicbrainz-userscripts/master/bandcamp_importer.user.js
77
// @updateURL https://raw.github.com/murdos/musicbrainz-userscripts/master/bandcamp_importer.user.js
8-
// @include /^https?://[^/]+/(?:album|track)/[^/]+\/?$/
9-
// @include /^https?://web\.archive\.org/web/\d+/https?://[^/]+/(?:album|track)/[^/]+\/?$/
8+
// @include /^https:\/\/[^/]+(?:\/(?:album|track)\/[^/]+\/?|\/music\/?|\/?)$/
9+
// @include /^https?:\/\/web\.archive\.org\/web\/\d+\/https?:\/\/[^/]+(?:\/(?:album|track)\/[^/]+\/?|\/music\/?|\/?)$/
1010
// @require https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js
1111
// @require lib/mbimport.js
1212
// @require lib/logger.js
@@ -299,117 +299,140 @@ $(document).ready(function () {
299299
*/
300300
if (!unsafeWindow.TralbumData) return;
301301
/***/
302+
let mblinks = new MBLinks('BCI_MBLINKS_CACHE');
302303

303-
MBImportStyle();
304+
const isDiscographyPage = unsafeWindow.TralbumData.url && !!unsafeWindow.TralbumData.url.match(/\/music\/?$/);
304305

305-
let mblinks = new MBLinks('BCI_MBLINKS_CACHE');
306+
if (isDiscographyPage) {
307+
const hostname = unsafeWindow.TralbumData.url.replace('/music', '');
306308

307-
let release = BandcampImport.retrieveReleaseInfo();
308-
309-
// add MB artist link
310-
let root_url = release.url.match(/^(https?:\/\/[^/]+)/)[1].split('?')[0];
311-
let label_url = '';
312-
313-
mblinks.searchAndDisplayMbLink(
314-
root_url,
315-
'label',
316-
function (link) {
317-
$('p#band-name-location span.title').append(link);
318-
},
319-
`label:${root_url}`,
320-
);
321-
const labelback = document.querySelector('a.back-to-label-link');
322-
if (labelback) {
323-
const labelbacklink = labelback.getAttribute('href');
324-
if (labelbacklink) {
325-
label_url = labelbacklink
326-
.match(/^(https?:\/\/[^/]+)/)[1]
327-
.split('?')[0]
328-
.fix_bandcamp_url();
329-
mblinks.searchAndDisplayMbLink(
330-
label_url,
331-
'label',
332-
function (link) {
333-
$('a.back-to-label-link span.back-link-text').append(link);
334-
},
335-
`label:${label_url}`,
336-
);
309+
$('ol#music-grid > li > a').each(function () {
310+
const $link = $(this);
311+
const bandcampReleaseUrl = $link.attr('href');
312+
313+
if (bandcampReleaseUrl && bandcampReleaseUrl.match(/^(\/album|\/track)/)) {
314+
const full_url = hostname + bandcampReleaseUrl;
315+
316+
mblinks.searchAndDisplayMbLink(
317+
full_url,
318+
'release',
319+
function (link) {
320+
$('p.title', $link).prepend(link);
321+
},
322+
`release:${full_url}`,
323+
);
324+
}
325+
});
326+
} else {
327+
MBImportStyle();
328+
329+
let release = BandcampImport.retrieveReleaseInfo();
330+
331+
// add MB artist link
332+
let root_url = release.url.match(/^(https?:\/\/[^/]+)/)[1].split('?')[0];
333+
let label_url = '';
334+
335+
mblinks.searchAndDisplayMbLink(
336+
root_url,
337+
'label',
338+
function (link) {
339+
$('p#band-name-location span.title').append(link);
340+
},
341+
`label:${root_url}`,
342+
);
343+
const labelback = document.querySelector('a.back-to-label-link');
344+
if (labelback) {
345+
const labelbacklink = labelback.getAttribute('href');
346+
if (labelbacklink) {
347+
label_url = labelbacklink
348+
.match(/^(https?:\/\/[^/]+)/)[1]
349+
.split('?')[0]
350+
.fix_bandcamp_url();
351+
mblinks.searchAndDisplayMbLink(
352+
label_url,
353+
'label',
354+
function (link) {
355+
$('a.back-to-label-link span.back-link-text').append(link);
356+
},
357+
`label:${label_url}`,
358+
);
359+
}
337360
}
338-
}
339361

340-
if (release.artist_credit.length == 1) {
341-
// try to get artist's mbid from cache
342-
let artist_mbid = mblinks.resolveMBID(root_url);
343-
if (artist_mbid) {
344-
release.artist_credit[0].mbid = artist_mbid;
362+
if (release.artist_credit.length == 1) {
363+
// try to get artist's mbid from cache
364+
let artist_mbid = mblinks.resolveMBID(root_url);
365+
if (artist_mbid) {
366+
release.artist_credit[0].mbid = artist_mbid;
367+
}
345368
}
346-
}
347369

348-
// try to get label mbid from cache
349-
let label_mbid = '';
350-
let label_name = '';
351-
if (label_url) {
352-
label_mbid = mblinks.resolveMBID(`label:${label_url}`);
353-
label_name = BandcampImport.getlabelname();
354-
} else {
355-
label_mbid = mblinks.resolveMBID(`label:${root_url}`);
356-
if (label_mbid) label_name = $('p#band-name-location span.title').text().trim();
357-
}
358-
if (label_mbid || label_name) {
359-
if (release.labels.length == 0) {
360-
release.labels.push({
361-
name: '',
362-
mbid: '',
363-
catno: 'none',
370+
// try to get label mbid from cache
371+
let label_mbid = '';
372+
let label_name = '';
373+
if (label_url) {
374+
label_mbid = mblinks.resolveMBID(`label:${label_url}`);
375+
label_name = BandcampImport.getlabelname();
376+
} else {
377+
label_mbid = mblinks.resolveMBID(`label:${root_url}`);
378+
if (label_mbid) label_name = $('p#band-name-location span.title').text().trim();
379+
}
380+
if (label_mbid || label_name) {
381+
if (release.labels.length == 0) {
382+
release.labels.push({
383+
name: '',
384+
mbid: '',
385+
catno: 'none',
386+
});
387+
}
388+
release.labels[0].name = label_name;
389+
release.labels[0].mbid = label_mbid;
390+
}
391+
392+
BandcampImport.insertLink(release);
393+
LOGGER.info('Parsed release: ', release);
394+
395+
if (release.type == 'track') {
396+
mblinks.searchAndDisplayMbLink(root_url, 'artist', function (link) {
397+
$('div#name-section h3 span:last').before(link);
398+
});
399+
// add MB links to parent album
400+
mblinks.searchAndDisplayMbLink(release.parent_album_url, 'release', function (link) {
401+
$('div#name-section h3 span:first').before(link);
402+
});
403+
} else {
404+
mblinks.searchAndDisplayMbLink(root_url, 'artist', function (link) {
405+
$('div#name-section h3 span:first').before(link);
406+
});
407+
// add MB release links to album or single
408+
mblinks.searchAndDisplayMbLink(release.url, 'release', function (link) {
409+
$('div#name-section h3 span:first').after(link);
364410
});
365411
}
366-
release.labels[0].name = label_name;
367-
release.labels[0].mbid = label_mbid;
368-
}
369412

370-
BandcampImport.insertLink(release);
371-
LOGGER.info('Parsed release: ', release);
413+
// append a comma after each tag to ease cut'n'paste to MB
414+
$('div.tralbum-tags a:not(:last-child).tag').after(', ');
372415

373-
if (release.type == 'track') {
374-
mblinks.searchAndDisplayMbLink(root_url, 'artist', function (link) {
375-
$('div#name-section h3 span:last').before(link);
376-
});
377-
// add MB links to parent album
378-
mblinks.searchAndDisplayMbLink(release.parent_album_url, 'release', function (link) {
379-
$('div#name-section h3 span:first').before(link);
380-
});
381-
} else {
382-
mblinks.searchAndDisplayMbLink(root_url, 'artist', function (link) {
383-
$('div#name-section h3 span:first').before(link);
384-
});
385-
// add MB release links to album or single
386-
mblinks.searchAndDisplayMbLink(release.url, 'release', function (link) {
387-
$('div#name-section h3 span:first').after(link);
388-
});
389-
}
416+
// append a link to the full size image
417+
const tralbumArt = document.querySelector('div#tralbumArt');
418+
const fullsizeimageurl = tralbumArt.querySelector('a').getAttribute('href').replace('_10', '_0');
419+
tralbumArt.insertAdjacentHTML(
420+
'afterend',
421+
`<div id='bci_link'><a class='custom-color' href='${fullsizeimageurl}' title='Open original image in a new tab (Bandcamp importer)' target='_blank'>Original image</a></div>`,
422+
);
390423

391-
// append a comma after each tag to ease cut'n'paste to MB
392-
$('div.tralbum-tags a:not(:last-child).tag').after(', ');
393-
394-
// append a link to the full size image
395-
const tralbumArt = document.querySelector('div#tralbumArt');
396-
const fullsizeimageurl = tralbumArt.querySelector('a').getAttribute('href').replace('_10', '_0');
397-
tralbumArt.insertAdjacentHTML(
398-
'afterend',
399-
`<div id='bci_link'><a class='custom-color' href='${fullsizeimageurl}' title='Open original image in a new tab (Bandcamp importer)' target='_blank'>Original image</a></div>`,
400-
);
401-
402-
const bci_link = document.querySelector('div#bci_link');
403-
bci_link.style.paddingTop = '0.5em';
404-
bci_link.style.textAlign = 'right';
405-
bci_link.querySelector('a').style.fontWeight = 'bold';
406-
const upc = unsafeWindow.TralbumData.current.upc;
407-
if (typeof upc != 'undefined' && upc !== null) {
408-
document.querySelector('div #trackInfoInner').insertAdjacentHTML(
409-
'beforeend',
410-
`<div id="mbimport_upc" style="margin-bottom: 2em; font-size: smaller;">UPC: ${upc}<br/>
424+
const bci_link = document.querySelector('div#bci_link');
425+
bci_link.style.paddingTop = '0.5em';
426+
bci_link.style.textAlign = 'right';
427+
bci_link.querySelector('a').style.fontWeight = 'bold';
428+
const upc = unsafeWindow.TralbumData.current.upc;
429+
if (typeof upc != 'undefined' && upc !== null) {
430+
document.querySelector('div #trackInfoInner').insertAdjacentHTML(
431+
'beforeend',
432+
`<div id="mbimport_upc" style="margin-bottom: 2em; font-size: smaller;">UPC: ${upc}<br/>
411433
Import: <a href="https://harmony.pulsewidth.org.uk/release?url=${encodeURIComponent(release.url)}&category=default">Harmony</a>
412434
| <a href="https://atisket.pulsewidth.org.uk/?upc=${upc}">a-tisket</a></div>`,
413-
);
435+
);
436+
}
414437
}
415438
});

0 commit comments

Comments
 (0)