Spotify - Append Artist Name and Title to Copied Link

Spotify - Append Artist Name and Title to Copied Link.

当前为 2022-03-20 提交的版本,查看 最新版本

// ==UserScript==
// @name         Spotify - Append Artist Name and Title to Copied Link
// @description  Spotify - Append Artist Name and Title to Copied Link.
// @match        https://open.spotify.com/*
// @author       to
// @namespace    https://github.com/to
// @connect      spotify.com
// @connect      song.link
// @version      0.5
// @grant        GM_xmlhttpRequest
// @grant        GM_setClipboard
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @license      MIT
// @icon         https://www.google.com/s2/favicons?sz=64&domain=spotify.com
// ==/UserScript==

// SpotifyのAPIを利用すると 日本のアーティストが確実に日本語表記になる
const CLIENT_ID = '';
const CLIENT_SECRET = '';
const MARKETS = ['JP', 'US'];

let market = MARKETS[0];

// マーケットの国を切り替える
// (海外のアーティストがカタカナ表記になってしまうことがあるため)
MARKETS.forEach(m => {
    GM_registerMenuCommand ('Market: ' + m, () => {
        market = m;
    });
});

document.addEventListener('copy', e => {
	// URL以外がコピーされたか、または、通常のテキストコピー操作か?
    let url = e.target.value;
	if(!(/^http/.test(url) || !(e.target instanceof HTMLTextAreaElement)))
		return;

    url = url.replace(/\?.+/, '');

    // SpotifyのAPIを利用するか?
    if(CLIENT_ID){
        let ts = url.match(/\/(track|album)\/(.+)/);
        if(ts){
            requestToSpotify(`https://api.spotify.com/v1/${ts[1]}s/${ts[2]}?market=${market}`, r => {
                GM_setClipboard([
                    r.artists.map(a => a.name).join(', '),
                    '-',
                    r.name,
                    url,
                    'https://' + (r.type == 'track' ? 'song' : r.type) + '.link/s/' + r.id].join(' '));
            });
        }
    } else {
        // JPロケールが適用されていない(USと同じ値が返される)
        GM_xmlhttpRequest({
            url: `https://api.song.link/v1-alpha.1/links?userCountry=${market}&url=${encodeURIComponent(url)}`,
            onload: function(r){
                r = JSON.parse(r.responseText);

                GM_setClipboard([
                    r.entitiesByUniqueId[r.entityUniqueId].artistName,
                    '-',
                    r.entitiesByUniqueId[r.entityUniqueId].title,
                    url,
                    r.pageUrl].join(' '));
            }});
    }
});

function requestToSpotify(url, callback){
    GM_xmlhttpRequest({
        url: 'https://accounts.spotify.com/api/token',
        method: 'POST',
        data: 'grant_type=client_credentials',
        headers: {
            'Authorization': 'Basic ' + btoa(CLIENT_ID + ':' + CLIENT_SECRET),
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        onload: function(r){
            r = JSON.parse(r.responseText);

            GM_xmlhttpRequest({
                url: url,
                headers: {
                    'Authorization': r.token_type + ' ' + r.access_token,
                    'Content-Type': 'application/json',
                },
                onload: function(r){
                    callback(JSON.parse(r.responseText));
	            }});
        }});
}

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址