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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);