kimonline / 喜马拉雅音频下载助手

// ==UserScript==
// @name         喜马拉雅音频下载助手
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  获取喜马拉雅FM音频的真实地址
// @author       CPLex
// @match        http://www.ximalaya.com/*
// @require      https://code.jquery.com/jquery-latest.js
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    function getElementByAttr(tag,attribute,value) {
        var temp_elements=document.getElementsByTagName(tag);
        var elements=[];
        for(var i = 0;i < temp_elements.length;i++){
            if(temp_elements[i].getAttribute(attribute) == value)
                elements.push( temp_elements[i] );
        }
        return elements;
    }
    //判断是专辑还是单个音频
    function isAlbum(){
        //是否是专辑中声音这个标题
        if(document.getElementsByClassName("body_top mgT20")[0]){
            return true;
        }
        else{
            return false;
        }
    }

    function getSoundIDs(){
        var sound_ids = [];
        if (isAlbum()){
            sound_ids = document.getElementsByClassName("personal_body")[0].getAttribute("sound_ids").split(",");
        }
        else{
            sound_ids[0] = document.getElementsByClassName("detail_soundBox2")[0].getAttribute("sound_id");
        }

        return sound_ids;
    }

    function getLink(id,play_path){
        var element;
        var link=document.createElement("a");
        link.href = play_path;
        link.download ="test.m4a";
        link.innerText = "下载地址";
        if (isAlbum()){
            element = getElementByAttr("li","sound_id",id);
        }
        else{
            element = document.getElementsByClassName("createtime");
        }
        element[0].appendChild(link);
        console.log(link.href);
    }

    function main(){
        //获取页面上所以音频的id
        var sound_ids = getSoundIDs();
        var url = [];
        var k = 0 ;
        for (var i = 0; i < sound_ids.length; i++){
            url[i] = "http://www.ximalaya.com/tracks/"+ sound_ids[i]+".json";
            $.getJSON(
                url[i],
                function(data) {
                    console.log(sound_ids[k]);
                    //alert(data.play_path);
                    //console.log(sound_ids[i]);
                    getLink(sound_ids[k],data.play_path);
                    k++;
                }
            );
        }
    }
    main();





})();