"Copy Tracklist Markup" with MPA

Adds multiple primary artists unlike the original function + button accessible to Contributors

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         "Copy Tracklist Markup" with MPA
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  Adds multiple primary artists unlike the original function + button accessible to Contributors
// @author       thousandeyes
// @match        https://genius.com/albums/*
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    function getTracklist() {
        const artistLinks = document.querySelectorAll('a.header_with_cover_art-primary_info-primary_artist');
        const albumTitle = document.querySelector('h1.header_with_cover_art-primary_info-title');
        const tracks = document.querySelectorAll('album-tracklist-row .chart_row-content');

        if (!artistLinks.length || !albumTitle || tracks.length === 0) {
            console.log('');
            return;
        }

        let artistNames = Array.from(artistLinks).map(link => `[${link.textContent.trim()}](${link.href})`);
        let formattedArtists = artistNames.length > 2
            ? artistNames.slice(0, -1).join(', ') + ' & ' + artistNames[artistNames.length - 1]
            : artistNames.join(' & ');

        const albumName = albumTitle.textContent.trim();
        const albumUrl = window.location.href;

        let tracklistText = `<b>${formattedArtists} - [*${albumName}*](${albumUrl}) Lyrics and Tracklist</b>\n\n`;

        tracks.forEach((trackElement, index) => {
            const trackLink = trackElement.querySelector('a');
            if (!trackLink) return;

            let rawTitle = trackLink.textContent.trim().replace(/\s*Lyrics$/, '');
            const trackUrl = trackLink.href;

            let title = rawTitle;
            let featuredText = "";

            const ftPattern = /[\(\s](ft\.?|feat\.?|Ft\.?)[\s\.](.+?)[\)]?$/i;
            const ftMatch = title.match(ftPattern);
            if (ftMatch) {
                featuredText = ftMatch[2].trim();
                title = title.replace(ftPattern, '').trim();
            }

            let primaryArtists = [];
            const byPattern = /(.+?)\s+by\s+(.+)/i;
            const match = title.match(byPattern);
            if (match) {
                title = match[1].trim();
                primaryArtists = match[2]
                    .split(/,\s*|\s*&\s*/)
                    .map(artist => `[${artist}](${getArtistUrl(artist)})`);
            }

            let trackText = `${index + 1}. `;
            if (primaryArtists.length > 0) {
                const joinedPrimary = primaryArtists.length > 2
                    ? primaryArtists.slice(0, -1).join(', ') + ' & ' + primaryArtists[primaryArtists.length - 1]
                    : primaryArtists.join(' & ');
                trackText += joinedPrimary + ' - ';
            }
            trackText += `[${title}](${trackUrl})`;

            let featuredArtistsText = "";
            const featuredArtists = trackElement.querySelectorAll('.featured_artists a');
            if (featuredArtists.length > 0) {
                featuredArtistsText = Array.from(featuredArtists).map(artist => {
                    const name = artist.textContent.trim();
                    return `[${name}](${artist.href})`;
                }).join(', ');
            } else if (featuredText) {
                featuredArtistsText = featuredText;
            }

            if (featuredArtistsText) {
                trackText += ` ft. ${featuredArtistsText}`;
            }

            tracklistText += `${trackText}\n`;
        });

        navigator.clipboard.writeText(tracklistText)
            .then(() => {
                const button = document.querySelector('.custom-copy-button');
                button.textContent = 'Copied to clipboard';
                setTimeout(() => {
                    button.textContent = 'Copy Tracklist Markup';
                }, 2000);
            })
            .catch(err => console.log('Error: ' + err));
    }

    function getArtistUrl(artistName) {
        return `https://genius.com/artists/${encodeURIComponent(artistName.trim().replace(/\s+/g, '-'))}`;
    }

    function createCopyButton() {
        const button = document.createElement('div');
        button.className = 'square_button u-bottom_margin custom-copy-button';
        button.textContent = 'Copy Tracklist Markup';
        button.style.cursor = 'pointer';
        button.addEventListener('click', getTracklist);

        const moreButton = document.querySelector('div.square_button.drop-target');
        if (moreButton) {
            moreButton.parentNode.insertBefore(button, moreButton.nextSibling);
        }
    }

    window.addEventListener('load', createCopyButton);
})();