mb_relationship_shortcuts.user.js: update and reduce jquery, minor improvements

This commit is contained in:
Kai 2022-04-21 21:51:06 +00:00 committed by Aurélien Mino
parent 79d894538e
commit 2d2f43211a

View file

@ -1,16 +1,17 @@
// ==UserScript== // ==UserScript==
// @name Display shortcut for relationships on MusicBrainz // @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. // @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.
// @version 2022.1.27.1 // @version 2022.4.21.1
// @author Aurelien Mino <aurelien.mino@gmail.com> // @author Aurelien Mino <aurelien.mino@gmail.com>
// @licence GPL (http://www.gnu.org/copyleft/gpl.html) // @licence GPL (http://www.gnu.org/copyleft/gpl.html)
// @downloadURL https://raw.github.com/murdos/musicbrainz-userscripts/master/mb_relationship_shortcuts.user.js // @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 // @updateURL https://raw.github.com/murdos/musicbrainz-userscripts/master/mb_relationship_shortcuts.user.js
// @namespace https://github.com/murdos/musicbrainz-userscripts
// @include http*://*musicbrainz.org/artist/* // @include http*://*musicbrainz.org/artist/*
// @include http*://*musicbrainz.org/release-group/* // @include http*://*musicbrainz.org/release-group/*
// @include http*://*musicbrainz.org/label/* // @include http*://*musicbrainz.org/label/*
// @exclude */artist/*/recordings* // @exclude */artist/*/recordings*
// @require https://code.jquery.com/jquery-3.2.1.min.js // @require https://code.jquery.com/jquery-3.6.0.min.js
// ==/UserScript== // ==/UserScript==
// Definitions: relations-type and corresponding icons we are going to treat // Definitions: relations-type and corresponding icons we are going to treat
@ -56,6 +57,11 @@ const streamingIconClasses = {
'tidal.com': 'tidal', 'tidal.com': 'tidal',
}; };
/**
* @param {string} mbid
* @param {string} targetUrl
* @param {string} iconClass
*/
function injectShortcutIcon(mbid, targetUrl, iconClass) { function injectShortcutIcon(mbid, targetUrl, iconClass) {
if (!iconClass) return; if (!iconClass) return;
$(`#${mbid} td.relationships`).append( $(`#${mbid} td.relationships`).append(
@ -63,9 +69,13 @@ function injectShortcutIcon(mbid, targetUrl, iconClass) {
); );
} }
/**
* @param {string} url
* @param {Object} iconClassMap
*/
function findIconClassOfUrl(url, iconClassMap) { function findIconClassOfUrl(url, iconClassMap) {
for (let partialUrl in iconClassMap) { for (let partialUrl in iconClassMap) {
if (url.indexOf(partialUrl) != -1) { if (url.includes(partialUrl)) {
return iconClassMap[partialUrl]; return iconClassMap[partialUrl];
} }
} }
@ -103,7 +113,7 @@ td.relationships span.favicon.ended {
background-image: url(data:image/gif;base64,R0lGODlhEQARALMAAAAAAP////z8/Onp6dzc3KmpqaGhoZGRkYyMjHx8fP///wAAAAAAAAAAAAAAAAAAACH5BAEAAAoALAAAAAARABEAAARNUBCUqr0JEVnI+GA4EJ0WnGiKTskQGEcsy0YwVK6q2/g7/7Vba6cTumA/Gm9ITBl9yViw10Q9kdEps7o8RqU8EzcwIXlEIrOEgsFoBBEAOw==); background-image: url(data:image/gif;base64,R0lGODlhEQARALMAAAAAAP////z8/Onp6dzc3KmpqaGhoZGRkYyMjHx8fP///wAAAAAAAAAAAAAAAAAAACH5BAEAAAoALAAAAAARABEAAARNUBCUqr0JEVnI+GA4EJ0WnGiKTskQGEcsy0YwVK6q2/g7/7Vba6cTumA/Gm9ITBl9yViw10Q9kdEps7o8RqU8EzcwIXlEIrOEgsFoBBEAOw==);
}`; }`;
// prevent JQuery conflicts, see http://wiki.greasespot.net/@grant // prevent JQuery conflicts, see https://wiki.greasespot.net/@grant
this.$ = this.jQuery = jQuery.noConflict(true); this.$ = this.jQuery = jQuery.noConflict(true);
if (!unsafeWindow) unsafeWindow = window; if (!unsafeWindow) unsafeWindow = window;
@ -130,9 +140,9 @@ $(document).ready(function () {
return; return;
} }
$(`<style id='relationship-shortcuts-userscript-css'>${userscriptCSS}</style>`).appendTo('head'); document.head.insertAdjacentHTML('beforeend', `<style id='relationship-shortcuts-userscript-css'>${userscriptCSS}</style>`);
let 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})/; const 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 // Determine target column
let columnindex = 0; let columnindex = 0;
@ -140,7 +150,8 @@ $(document).ready(function () {
$(this) $(this)
.children('td') .children('td')
.each(function () { .each(function () {
if ($(this).find('a').attr('href') !== undefined && $(this).find('a').attr('href').match(mbidRE)) { const href = $(this).find('a').attr('href');
if (href !== undefined && href.match(mbidRE)) {
return false; return false;
} }
columnindex++; columnindex++;
@ -187,8 +198,7 @@ $(document).ready(function () {
let offset = (page - 1) * 100; let offset = (page - 1) * 100;
// Call the MB webservice // Call the MB webservice
let url = `/ws/2/${child.type}?${parent.type}=${parent.mbid}&inc=${incOptions[child.type].join('+')}&limit=100&offset=${offset}`; const url = `/ws/2/${child.type}?${parent.type}=${parent.mbid}&inc=${incOptions[child.type].join('+')}&limit=100&offset=${offset}`;
//console.log("MB WS url: " + url);
$.get(url, function (data, textStatus, jqXHR) { $.get(url, function (data, textStatus, jqXHR) {
// Parse each child // Parse each child
@ -242,9 +252,9 @@ $(document).ready(function () {
$(this) $(this)
.children('relation') .children('relation')
.each(function () { .each(function () {
let reltype = $(this).attr('type'); const reltype = $(this).attr('type');
let target = $(this).children('target').text(); const target = $(this).children('target').text();
let url = targettype == 'url' ? target : `/${targettype}/${target}`; const url = targettype == 'url' ? target : `/${targettype}/${target}`;
if (Object.prototype.hasOwnProperty.call(relationsIconsURLs[targettype], reltype)) { if (Object.prototype.hasOwnProperty.call(relationsIconsURLs[targettype], reltype)) {
if (!Object.prototype.hasOwnProperty.call(relations, reltype)) relations[reltype] = [url]; if (!Object.prototype.hasOwnProperty.call(relations, reltype)) relations[reltype] = [url];