MusicBrainz: Add Spotify and Deezer ISRC link to release pages

Adds an "import ISRCs" link to MusicBrainz release pages with a Spotify or Deezer URL

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        MusicBrainz: Add Spotify and Deezer ISRC link to release pages
// @namespace   https://musicbrainz.org/user/chaban
// @version     1.2
// @tag         ai-created
// @description Adds an "import ISRCs" link to MusicBrainz release pages with a Spotify or Deezer URL
// @author      atj, chaban
// @license     MIT
// @match       *://*.musicbrainz.org/release/*
// @icon        https://musicbrainz.org/static/images/favicons/android-chrome-512x512.png
// @grant       none
// @run-at      document-idle
// ==/UserScript==

const SpotifyLinkRegexp = /^https?:\/\/open\.spotify\.com\/album\//i;
const DeezerLinkRegexp = /^https?:\/\/www\.deezer\.com\/album\//i;

/**
 * Adds an "import ISRCs" link next to the given link element.
 * @param {HTMLElement} linkElement - The link element to add the "import ISRCs" link after.
 * @param {string} type - The type of service ("spotify" or "deezer").
 * @param {string} id - The ID of the album.
 */
function addImportLink(linkElement, type, id) {
    const isrcHuntUrl = `https://isrchunt.com/${type}/importisrc?releaseId=${id}`;
    let curElem = linkElement.nextElementSibling.nextSibling;
    let elem = document.createTextNode(' [');
    curElem = insertAfter(elem, curElem);
    elem = document.createElement('a');
    elem.href = isrcHuntUrl;
    elem.innerText = 'import ISRCs';
    curElem = insertAfter(elem, curElem);
    elem = document.createTextNode(']');
    insertAfter(elem, curElem);
}

function insertAfter(elem, after) {
    if (after.parentNode) {
        after.parentNode.insertBefore(elem, after.nextSibling);
    }
    return elem;
}

function addImportIsrcsLink() {
    const releaseRels = document.getElementById('release-relationships');

    if (!releaseRels) {
        return;
    }

    for (const bdi of releaseRels.getElementsByTagName('bdi')) {
        let matches = bdi.innerText.match(SpotifyLinkRegexp);
        if (matches) {
            const spotifyId = bdi.innerText.split('/').pop();
            const spotifyLink = bdi.parentElement;
            addImportLink(spotifyLink, 'spotify', spotifyId);
        }

        matches = bdi.innerText.match(DeezerLinkRegexp);
        if (matches) {
            const deezerId = bdi.innerText.split('/').pop();
            const deezerLink = bdi.parentElement;
            addImportLink(deezerLink, 'deezer', deezerId);
        }
    }
}

window.setTimeout(addImportIsrcsLink, 250);