XMwiki专辑曲目列表生成

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

目前為 2020-07-14 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         XMwiki专辑曲目列表生成
// @version      1.2
// @description  几个常用音乐平台专辑曲目列表生成 Grab song list information from some websites
// @author       XMAnon
// @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://greasyfork.org/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://greasyfork.org/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/g, ' '));
            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(/ & /g,';');
        var song = function(_no, _title,_artist) {
            this.no = _no;
            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[0], rawList[1], rawList[2].replace(/ & /g,';').replace(' feat. ',';').replace(/, /g,';'));
            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(){}

    //*********************************************************************************************************************


    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 on the player banner
                headerBox_Amzon.appendChild(newBtn);
                break;
                //case (currentUrl.indexOf('bandcamp') > -1):
            default:
                console.warn('Host not matching');
        }
    }
})();