2017-11-11 01:28:11 +00:00
// ==UserScript==
2015-07-18 20:59:15 +00:00
// @name Display shortcut for relationships on MusicBrainz
// @description Display icon shortcut for relationships of release-group, release, recording and work: e.g. Amazon, Discogs, Wikipedia, ... links. This allows to access some relationships without opening the entity page.
2018-02-18 17:16:45 +00:00
// @version 2018.2.18.1
2015-07-18 20:59:15 +00:00
// @author Aurelien Mino <aurelien.mino@gmail.com>
// @licence GPL (http://www.gnu.org/copyleft/gpl.html)
// @downloadURL https://raw.github.com/murdos/musicbrainz-userscripts/master/mb_relationship_shortcuts.user.js
// @updateURL https://raw.github.com/murdos/musicbrainz-userscripts/master/mb_relationship_shortcuts.user.js
// @include http*://*musicbrainz.org/artist/*
// @include http*://*musicbrainz.org/release-group/*
// @include http*://*musicbrainz.org/label/*
2017-11-11 01:28:11 +00:00
// @require https://code.jquery.com/jquery-3.2.1.min.js
2015-07-18 20:59:15 +00:00
// ==/UserScript==
// Definitions: relations-type and corresponding icons we are going to treat
var relationsIconsURLs = {
'url' : {
2017-11-11 01:28:11 +00:00
"amazon asin" : "https://musicbrainz.org/static/images/favicons/amazon-32.png" ,
"discogs" : "https://musicbrainz.org/static/images/favicons/discogs-32.png" ,
"wikidata" : "https://musicbrainz.org/static/images/favicons/wikidata-32.png" ,
"imdb" : "https://musicbrainz.org/static/images/favicons/imdb-32.png" ,
2015-07-18 20:59:15 +00:00
"creative commons licensed download" : "http://creativecommons.org/favicon.ico" ,
2017-11-11 01:28:11 +00:00
"cover art link" : "http://www.cdcovers.cc/favicon.ico" ,
"secondhandsongs" : "https://musicbrainz.org/static/images/favicons/secondhandsongs-32.png" ,
2017-11-12 01:02:26 +00:00
"lyrics" : "http://www.nomy.nu/img/lyrics-icon.gif" ,
"allmusic" : "https://musicbrainz.org/static/images/favicons/allmusic-16.png"
2015-07-18 20:59:15 +00:00
} ,
'release-group' : {
"single from" : "http://www.amaesingtools.com/images/left_arrow_icon.gif"
} ,
'release' : {
"part of set" : "http://web.archive.org/web/20060709091901/http://wiki.musicbrainz.org/-/musicbrainz/img/moin-inter.png" ,
"remaster" : "http://web.archive.org/web/20060708200714/http://wiki.musicbrainz.org/-/musicbrainz/img/moin-www.png" ,
}
} ;
2017-11-12 02:01:52 +00:00
var otherDatabasesIconURLs = {
"d-nb.info" : "https://musicbrainz.org/static/images/favicons/dnb-16.png" ,
"www.musik-sammler.de" : "https://musicbrainz.org/static/images/favicons/musiksammler-32.png" ,
"www.worldcat.org" : "https://musicbrainz.org/static/images/favicons/worldcat-32.png" ,
"rateyourmusic.com" : "https://musicbrainz.org/static/images/favicons/rateyourmusic-32.png"
} ;
2015-07-18 20:59:15 +00:00
var incOptions = {
'release-group' : [ 'release-group-rels' , 'url-rels' ] ,
'release' : [ 'release-rels' , 'url-rels' , 'discids' ] ,
'recording' : [ 'work-rels' ] ,
'work' : [ 'url-rels' ]
} ;
// prevent JQuery conflicts, see http://wiki.greasespot.net/@grant
this . $ = this . jQuery = jQuery . noConflict ( true ) ;
if ( ! unsafeWindow ) unsafeWindow = window ;
$ ( document ) . ready ( function ( ) {
// Get pageType (label or artist)
2017-11-11 01:28:11 +00:00
var parent = { } ;
var child = { } ;
2015-07-18 20:59:15 +00:00
if ( m = window . location . href . match ( "\/artist\/(.{36})[^\/]*$" ) ) {
parent . type = 'artist' ;
parent . mbid = m [ 1 ] ;
child . type = 'release-group' ;
2017-04-16 19:32:44 +00:00
} else if ( m = window . location . href . match ( "\/(release-group|label)\/(.{36})[^\/]*$" ) ) {
2015-07-18 20:59:15 +00:00
parent . type = m [ 1 ] ;
parent . mbid = m [ 2 ] ;
child . type = 'release' ;
} else if ( m = window . location . href . match ( "\/artist/(.{36})\/(releases|recordings|works)" ) ) {
parent . type = 'artist' ;
parent . mbid = m [ 1 ] ;
child . type = m [ 2 ] . replace ( /s$/ , '' ) ;
} else {
// Not supported
return ;
}
var mbidRE = /(release|release-group|work)\/([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})/ ;
// Determine target column
var columnindex = 0 ;
$ ( "table.tbl tbody tr[class!='subh']" ) . each ( function ( ) {
$ ( this ) . children ( "td" ) . each ( function ( ) {
2017-11-11 01:28:11 +00:00
if ( $ ( this ) . find ( "a" ) . attr ( "href" ) !== undefined && $ ( this ) . find ( "a" ) . attr ( "href" ) . match ( mbidRE ) ) { return false ; }
2015-07-18 20:59:15 +00:00
columnindex ++ ;
} ) ;
return false ;
} ) ;
// Set MBID to row in tables to get easiest fastest access
$ ( "table.tbl tr[class!='subh']" ) . each ( function ( ) {
var $tr = $ ( this ) ;
$tr . children ( "th:eq(" + columnindex + ")" ) . after ( "<th style='width: 150px;'>Relationships</th>" ) ;
$tr . children ( "td:eq(" + columnindex + ")" ) . after ( "<td class='relationships'></td>" ) ;
$ ( this ) . find ( "a" ) . each ( function ( ) {
var href = $ ( this ) . attr ( "href" ) ;
if ( m = href . match ( mbidRE ) ) {
$tr . attr ( "id" , m [ 2 ] ) ;
return false ;
}
} ) ;
} ) ;
2017-04-16 20:07:25 +00:00
// Calculate offset for multi-page lists
2017-11-11 01:28:11 +00:00
var page = 1 ;
2017-04-16 20:07:25 +00:00
if ( m = window . location . href . match ( "[\?&]page=([0-9]*)" ) ) {
2017-11-11 01:28:11 +00:00
page = m [ 1 ] ;
2017-04-16 20:07:25 +00:00
}
2017-11-11 01:28:11 +00:00
var offset = ( page - 1 ) * 100 ;
2015-07-18 20:59:15 +00:00
// Call the MB webservice
2017-04-16 20:07:25 +00:00
var url = '/ws/2/' + child . type + '?' + parent . type + "=" + parent . mbid + '&inc=' + incOptions [ child . type ] . join ( "+" ) + '&limit=100&offset=' + offset ;
2017-11-11 01:28:11 +00:00
//console.log("MB WS url: " + url);
2015-07-18 20:59:15 +00:00
$ . get ( url , function ( data , textStatus , jqXHR ) {
// Parse each child
$ ( data ) . find ( child . type ) . each ( function ( ) {
var mbid = $ ( this ) . attr ( "id" ) ;
// URL relationships
$ ( this ) . find ( "relation-list[target-type='url'] relation" ) . each ( function ( ) {
var reltype = $ ( this ) . attr ( "type" ) ;
var target = $ ( this ) . children ( "target" ) . text ( ) ;
2017-11-11 01:28:11 +00:00
if ( relationsIconsURLs . url . hasOwnProperty ( reltype ) ) {
$ ( "#" + mbid + " td.relationships" ) . append (
"<a href='" + target . replace ( /'/g , "'" ) + "'>" + "<img style='max-height: 16px;' src='" + relationsIconsURLs . url [ reltype ] + "' /> " + "</a>"
) ;
2017-11-12 02:01:52 +00:00
}
else for ( var rel in otherDatabasesIconURLs ) {
if ( target . indexOf ( rel ) != - 1 ) {
$ ( "#" + mbid + " td.relationships" ) . append (
"<a href='" + target . replace ( /'/g , "'" ) + "'>" + "<img style='max-height: 16px;' src='" + otherDatabasesIconURLs [ rel ] + "' /> " + "</a>"
) ;
}
2015-07-18 20:59:15 +00:00
}
} ) ;
// Other relationships
$ ( this ) . find ( "relation-list[target-type!='url']" ) . each ( function ( ) {
var targettype = $ ( this ) . attr ( "target-type" ) . replace ( "release_group" , "release-group" ) ;
var relations = { } ;
2017-12-30 10:55:33 +00:00
if ( relationsIconsURLs [ targettype ] === undefined ) {
return ;
}
2015-07-18 20:59:15 +00:00
$ ( this ) . children ( "relation" ) . each ( function ( ) {
var reltype = $ ( this ) . attr ( "type" ) ;
var target = $ ( this ) . children ( "target" ) . text ( ) ;
var url = ( targettype == 'url' ) ? target : "/" + targettype + "/" + target ;
if ( relationsIconsURLs [ targettype ] . hasOwnProperty ( reltype ) ) {
if ( ! relations . hasOwnProperty ( reltype ) ) relations [ reltype ] = [ url ] ;
else relations [ reltype ] . push ( url ) ;
}
} ) ;
$ . each ( relations , function ( reltype , urls ) {
var html = "" ;
if ( urls . length < - 1 ) {
2017-11-11 01:28:11 +00:00
html += "<img src='" + relationsIconsURLs [ targettype ] [ reltype ] + "' />(" + urls . length + ") " ;
2015-07-18 20:59:15 +00:00
} else {
$ . each ( urls , function ( index , url ) {
html += "<a href='" + url + "'><img src='" + relationsIconsURLs [ targettype ] [ reltype ] + "' /></a> " ;
} ) ;
}
$ ( "#" + mbid + " td.relationships" ) . append ( html ) ;
} ) ;
} ) ;
} ) ;
} ) ;
} ) ;