XMwiki专辑曲目列表生成

几个常用音乐平台专辑曲目列表生成 Grab song list information from some websites

Versión del día 17/7/2020. Echa un vistazo a la versión más reciente.

// ==UserScript==
// @name         XMwiki专辑曲目列表生成
// @version      1.2.2
// @description  几个常用音乐平台专辑曲目列表生成 Grab song list information from some websites
// @author       XMAnon
// @icon         https://s1.ax1x.com/2020/07/15/UabtVe.jpg
// @match        *://www.amazon.com/*
// @match        *://www.amazon.de/*
// @match        *://www.amazon.fr/*
// @match        *://www.amazon.it/*
// @match        *://www.amazon.es/*
// @match        *://open.spotify.com/*

// getSpotify() function is mostly inspired from Jeffrey.Deng(https://greatest.deepsurf.us/users/129338)复制spotify歌曲名脚本
// Grabbing Info from other sites is almost similar.
// 音乐平台抓取歌曲列表,并生成符合XM格式的歌曲列表(页面2)
// Supported:
//           Spotify Album,
//           Amazon Free Streaming Site (tested on US/DE, other Countries should also work)
// To be done:
//           Multiple CD case, Bandcamp, MusicBrainz
//
// @namespace https://greatest.deepsurf.us/users/666548
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
    function copyToClipboard(text) {
        if (window.clipboardData && window.clipboardData.setData) {
            // IE specific code path to prevent textarea being shown while dialog is visible.
            return clipboardData.setData("Text", text);

        } else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
            var textarea = document.createElement("textarea");
            textarea.textContent = text;
            textarea.style.position = "fixed";// Prevent scrolling to bottom of page in MS Edge.
            document.body.appendChild(textarea);
            textarea.select();
            try {
                return document.execCommand("copy");// Security exception may be thrown by some browsers.
            } catch (ex) {
                console.warn("Copy to clipboard failed.", ex);
                return false;
            } finally {
                document.body.removeChild(textarea);
            }
        }
    }
    //***************************************** Spotify *********************************************************************
    var getSpotify = function() {
        var nodes = document.querySelector('#main .tracklist-container .tracklist').querySelectorAll("div > li > div.tracklist-col.name > div > div");
        if (!nodes) {
            console.warn("Songs nodes not found!!");
            return;
        }
        var playList = [];
        var song = function(_title, _singer) {
            this.title = _title;
            this['singer • album'] = _singer.replace(', ',';');
        };
        var len = nodes.length;
        var charList = '';
        for (var i = 0; i < len; i += 2) {
            var one = new song(nodes[i].innerText, nodes[i+1].innerText.replace(/\n/gm, ' ').replace(/, /gm,';'));
            playList.push(one);
            charList = charList + one.title + '【歌手】' + one['singer • album'] + '\n';
        }
        copyToClipboard(charList);
    }
    unsafeWindow.getSpotify = getSpotify;

    //***************************************** Amazon *********************************************************************
    var getAmazon = function(){
        var nodes = document.querySelector("#dmusic_tracklist_content > tbody").getElementsByClassName('a-text-left a-align-center darkenOnHover');//tested on 'amazon.de' unlimited stream page
        var headerNode = document.getElementById('dmusic_tracklist_header_box');
        var len = nodes.length;
        if (!nodes) {
            console.warn("nodes not found");
            return;}
        else if (len === 0){
            nodes = [headerNode.nextSibling.nextSibling]; //Special Case when it's EP
            len = 1;
        }
        var playList = [];
        var albumArtist = document.getElementById("ProductInfoArtistLink").innerText.replace(/ & /gm,';').replace(' feat. ',';');
        var song = function(_title,_artist) {
            this.title = _title;
            this.artist = _artist;
        };
        var charList = '';
        for (var i = 0; i < len; i += 1) {
            var rawList = (nodes[i].innerText.split('\n'));
            rawList = rawList.filter(function(e){return e.replace(/\t/gm,"")});
            var one = new song(rawList[1], rawList[2].replace(/ & /gm,';').replace(' feat. ',';').replace(/, /gm,';'));
            switch(0){
                case(one.artist.indexOf('\t')):
                    one.artist = albumArtist;
                    break;
                case(one.artist.indexOf('de ')):
                case(one.artist.indexOf('di ')):
                case(one.artist.indexOf('by ')):
                    one.artist = one.artist.substring(3);
                    break;
                case(one.artist.indexOf('von ')):
                    one.artist = one.artist.substring(4);
                    break;
            }
            //console.log(rawList);
            //console.log(one);
            playList.push(one);
            charList = charList + one.title + '【歌手】'+ one.artist + '\n';
        }
        copyToClipboard(charList);
    }
    unsafeWindow.getAmazon = getAmazon;
    //var getBandCamp = function(){}
    //var getMusicBrain = function(){}
//     var getDiscogs = function(){//Discogs格式太乱了,情况太多了,写好了三种的,不好用先注释掉了。。。。
//         var nodes = document.querySelector("#tracklist > div > table > tbody").querySelectorAll("tr");//[0].innerText;
//         var len = nodes.length;
//         if (!nodes) {
//             console.warn("nodes not found");
//             return;}
//         var playList = [];
//         var albumArtist = document.getElementById("profile_title").innerText.split(' ‎– ')[0].replace(/ & | feat. |, | And /gm,';').replace(/\*/gm,'');
//         var song = function(_title,_artist) {
//             this.title = _title;
//             this.artist = _artist;
//         };
//         var charList = '';
//         for (var i = 0; i < len; i += 1) {
//             var rawList = (nodes[i].innerText.replace('\n–\n','').split('\t'));
//             rawList = rawList.filter(function(e){return e.replace(/\n/gm,'')});
//             var lenElement = rawList.length;
//             var one;
//             switch(lenElement){
//                 case(4):
//                     one = new song(rawList[2], rawList[1].replace(/ & | feat. |, | And /gm,';').replace(/\*/gm,''));
//                     break;
//                 case(3):
//                 case(2):
//                     one = new song(rawList[lenElement - 2], albumArtist);
//                     break;
//             }
//             ;
//             console.log(rawList);
//             console.log(one);
//             playList.push(one);
//             charList = charList + one.title + '【歌手】'+ one.artist + '\n';
//         }
//         copyToClipboard(charList);
//     }
//     unsafeWindow.getDiscogs = getDiscogs;
    //*********************************************************************************************************************


    window.onload = function checkSrc() {//Check data source, add a button
        var newBtn = document.createElement("input");
        newBtn.type = "button";
        newBtn.value = "虾抓";
        newBtn.title = "Click to copy the song lists";
        var currentUrl = window.location.href;
        switch(true){
            case (currentUrl.indexOf('open.spotify.com') > -1):
                newBtn.onclick = getSpotify;
                newBtn.style.backgroundColor = 'black';
                var likeBtn_Spotify = document.getElementsByClassName("spoticon-heart-32")[0].parentNode.parentNode; //Button near the heart button
                likeBtn_Spotify.appendChild(newBtn);
                break;
            case (currentUrl.indexOf('amazon') > -1):
                newBtn.onclick = getAmazon;
                newBtn.style.backgroundColor = 'orange';
                var headerBox_Amzon = document.getElementById("dmusicProductTitle_feature_div"); //Button under the album title
                headerBox_Amzon.appendChild(newBtn);
                break;
                //case (currentUrl.indexOf('bandcamp') > -1):
//             case (currentUrl.indexOf('discogs.com') > -1):
//                 newBtn.onclick = getDiscogs;
//                 newBtn.style.backgroundColor = 'white';
//                 var title_Discogs = document.getElementById("profile_title"); //Button near the title
//                 title_Discogs.appendChild(newBtn);
            default:
                console.warn('Host not matching');
        }
    }
})();