Skip to content

Instantly share code, notes, and snippets.

@kepstin
Last active October 12, 2015 08:18
Show Gist options
  • Save kepstin/3998113 to your computer and use it in GitHub Desktop.
Save kepstin/3998113 to your computer and use it in GitHub Desktop.
Fix filename so it's installable
/ ==UserScript==
// @name MusicBrainz: Search Music Forest and add ISRCs
// @description This adds a link to Music Forest next to catalogue numbers on release pages and a link on the Music Forest page to add the ISRCs
// @version 20140408
// @author -
// @namespace df069240-fe79-11dc-95ff-0800200c9a66
//
// @include http://musicbrainz.org/release/*
// @include https://musicbrainz.org/release/*
// @include http://musicbrainz.org/release-group/*
// @include https://musicbrainz.org/release-group/*
// @include http://musicbrainz.org/recording/*
// @include https://musicbrainz.org/recording/*
// @include http://musicbrainz.org/work/*
// @include https://musicbrainz.org/work/*
// @include http://musicbrainz.org/artist/*
// @include https://musicbrainz.org/artist/*
// @include http://www.minc.gr.jp/db/*
// @include https://www.minc.gr.jp/db/*
// @grant none
// ==/UserScript==
//**************************************************************************//
if (document.location.href.match(/minc.gr.jp/) && document.location.hash.match(/^#!mbid/)) {
var qs = document.location.hash.slice(2);
var medium = 0;
var e = document.getElementsByTagName('table');
for (var x = 0; x < e.length; x++) {
if (e[x].innerHTML.match(/収録No/)) {
medium = medium + 1;
var tr = e[x].getElementsByTagName('tr');
for (var i = 1; i < tr.length; i++) {
var td = tr[i].getElementsByTagName('td');
var tnum = td[0].innerHTML;
//.replace(/.*>([0-9]+).*/, "$1");
var tisrc = td[6].innerHTML;
//.replace(/.*>([0-9]+).*/, "$1");
tnum = tnum.replace(/.*>([0-9]+).*/, '$1');
if (tisrc.match(/[A-Z]{2}[A-Z0-9]{3}[0-9]{7}/)) {
tisrc = tisrc.replace(/.*>([A-Z0-9]+).*/, '$1');
} else {
tisrc = '';
}
qs = qs + '&isrc' + medium + '-' + tnum + '=' + tisrc;
}
}
}
document.getElementsByTagName('table') [0].outerHTML = document.getElementsByTagName('table') [0].outerHTML + '<a href="http://mb.lmfao.org.uk/isrc/?' + qs + '" target="_self">Add to MusicBrainz</a>';
} else if (document.location.href.match(/musicbrainz.org/)) {
console.log('Running MF add ISRCs on musicbrainz page');
var e = $('span.catalog-number');
for (var i = 0; i < e.length; i++) {
var catno = e[i].textContent;
console.log('found catno:', catno);
var m = catno.match(/^([A-Z]{3,4})[ -]([0-9]+)/);
if (m && m[1] && m[2]) {
e[i].outerHTML += '&nbsp;[<a href=" https://www.minc.gr.jp/db/AlbInfo.aspx?CAT1=' + m[1] + '&CAT2=' + m[2] + '#!mbid=' + document.URL + '">MF</a>]';
}
}
if (document.location.href.match(/musicbrainz.org\/work/)) {
e = $('h2.work-information + dl.properties > dt');
for (var i = 0; i < e.length; i++) {
if (e[i].textContent == 'JASRAC ID:') {
var dd = e[i].nextElementSibling;
var jasrac_id = dd.textContent.trim();
console.log('found jasrac id:', jasrac_id);
var m = jasrac_id.match(/^(\d[\d\w]\d)-(\d{4})-(\d{1})$/);
if (m && m[1] && m[2] && m[3]) {
dd.innerHTML =
'<code>' + jasrac_id + '</code>' +
'&nbsp;[<a href="http://www2.jasrac.or.jp/eJwid/main.jsp?trxID=F20101&WORKS_CD=' + m[1] + m[2] + m[3] + '&subSessionID=001&subSession=start">JW</a>]' +
'&nbsp;[<a href="https://www.minc.gr.jp/db/SakCdInfo.aspx?SAKUHINCD=' + m[1] + m[2] + m[3] + '">MF</a>]';
}
}
}
}
console.log('Applying work links to table...');
var t = $('#content form > table.tbl') [0];
var th = $('#content form > table.tbl > thead > tr > th');
var column = - 1;
for (var i = 0; i < th.length; i++) {
if (th[i].textContent == 'Attributes') {
column = i;
break;
}
}
if (column >= 0) {
console.log('Attributes column is ' + column);
var li = $('#content form > table.tbl > tbody > tr > td:nth-child(' + (column + 1) + ') > ul > li');
for (var i = 0; i < li.length; i++) {
var text = li[i].textContent.trim();
console.log(text);
var m = text.match(/^(\d[\d\w]\d)-(\d{4})-(\d{1}) \(JASRAC ID\)/);
if (m && m[1] && m[2] && m[3]) {
li[i].innerHTML =
'<code>' + m[1] + '-' + m[2] + '-' + m[3] + '</code>' +
'&nbsp;[<a href="http://www2.jasrac.or.jp/eJwid/main.jsp?trxID=F20101&WORKS_CD=' + m[1] + m[2] + m[3] + '&subSessionID=001&subSession=start">JW</a>]' +
'&nbsp;[<a href="https://www.minc.gr.jp/db/SakCdInfo.aspx?SAKUHINCD=' + m[1] + m[2] + m[3] + '">MF</a>]';
}
}
}
}
// http://snipplr.com/view/1853/
function getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue) {
var arrElements = (strTagName == '*' && oElm.all) ? oElm.all : oElm.getElementsByTagName(strTagName);
var arrReturnElements = new Array();
var oAttributeValue = (typeof strAttributeValue != 'undefined') ? new RegExp('(^|s)' + strAttributeValue + '(s|$)', 'i') : null;
var oCurrent;
var oAttribute;
for (var i = 0; i < arrElements.length; i++) {
oCurrent = arrElements[i];
oAttribute = oCurrent.getAttribute && oCurrent.getAttribute(strAttributeName);
if (typeof oAttribute == 'string' && oAttribute.length > 0) {
if (typeof strAttributeValue == 'undefined' || (oAttributeValue && oAttributeValue.test(oAttribute))) {
arrReturnElements.push(oCurrent);
}
}
}
return arrReturnElements;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment