Skip to content

Commit f778311

Browse files
committed
bandcamp_importer: enable MB Release links on Discography pages (release lists)
1 parent 1261bfb commit f778311

File tree

1 file changed

+121
-100
lines changed

1 file changed

+121
-100
lines changed

bandcamp_importer.user.js

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

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;
360+
if (release.artist_credit.length == 1) {
361+
// try to get artist's mbid from cache
362+
let artist_mbid = mblinks.resolveMBID(root_url);
363+
if (artist_mbid) {
364+
release.artist_credit[0].mbid = artist_mbid;
365+
}
345366
}
346-
}
347367

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',
368+
// try to get label mbid from cache
369+
let label_mbid = '';
370+
let label_name = '';
371+
if (label_url) {
372+
label_mbid = mblinks.resolveMBID(`label:${label_url}`);
373+
label_name = BandcampImport.getlabelname();
374+
} else {
375+
label_mbid = mblinks.resolveMBID(`label:${root_url}`);
376+
if (label_mbid) label_name = $('p#band-name-location span.title').text().trim();
377+
}
378+
if (label_mbid || label_name) {
379+
if (release.labels.length == 0) {
380+
release.labels.push({
381+
name: '',
382+
mbid: '',
383+
catno: 'none',
384+
});
385+
}
386+
release.labels[0].name = label_name;
387+
release.labels[0].mbid = label_mbid;
388+
}
389+
390+
BandcampImport.insertLink(release);
391+
LOGGER.info('Parsed release: ', release);
392+
393+
if (release.type == 'track') {
394+
mblinks.searchAndDisplayMbLink(root_url, 'artist', function (link) {
395+
$('div#name-section h3 span:last').before(link);
396+
});
397+
// add MB links to parent album
398+
mblinks.searchAndDisplayMbLink(release.parent_album_url, 'release', function (link) {
399+
$('div#name-section h3 span:first').before(link);
400+
});
401+
} else {
402+
mblinks.searchAndDisplayMbLink(root_url, 'artist', function (link) {
403+
$('div#name-section h3 span:first').before(link);
404+
});
405+
// add MB release links to album or single
406+
mblinks.searchAndDisplayMbLink(release.url, 'release', function (link) {
407+
$('div#name-section h3 span:first').after(link);
364408
});
365409
}
366-
release.labels[0].name = label_name;
367-
release.labels[0].mbid = label_mbid;
368-
}
369410

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

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-
}
414+
// append a link to the full size image
415+
const tralbumArt = document.querySelector('div#tralbumArt');
416+
const fullsizeimageurl = tralbumArt.querySelector('a').getAttribute('href').replace('_10', '_0');
417+
tralbumArt.insertAdjacentHTML(
418+
'afterend',
419+
`<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>`
420+
);
390421

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/>
422+
const bci_link = document.querySelector('div#bci_link');
423+
bci_link.style.paddingTop = '0.5em';
424+
bci_link.style.textAlign = 'right';
425+
bci_link.querySelector('a').style.fontWeight = 'bold';
426+
const upc = unsafeWindow.TralbumData.current.upc;
427+
if (typeof upc != 'undefined' && upc !== null) {
428+
document.querySelector('div #trackInfoInner').insertAdjacentHTML(
429+
'beforeend',
430+
`<div id="mbimport_upc" style="margin-bottom: 2em; font-size: smaller;">UPC: ${upc}<br/>
411431
Import: <a href="https://harmony.pulsewidth.org.uk/release?url=${encodeURIComponent(release.url)}&category=default">Harmony</a>
412432
| <a href="https://atisket.pulsewidth.org.uk/?upc=${upc}">a-tisket</a></div>`
413-
);
433+
);
434+
}
414435
}
415436
});

0 commit comments

Comments
 (0)