YouTube to mp3 Downloader

try to take over the world!

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         YouTube to mp3 Downloader
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  try to take over the world!
// @author       You
// @match        https://www.youtube.com/*
// @grant        GM.xmlHttpRequest
// @grant        GM_download
// ==/UserScript==
(()=>{
    'use strict';
    const download = (url, file_name) => {
        GM_download({
            url: url,
            name: file_name,
            saveAs: false,
        });
    };
    const addBtn = (alies, func) => {
        var btn = document.createElement("button");
        btn.innerText = alies;
        btn.addEventListener("click",func);
        document.body.append(btn);
        return btn;
    };
    function setCSS(elm,parm){
        for(let k in parm) elm.style[k] = parm[k];
        return elm;
    }
    const elm = setCSS(addBtn("download as mp3",main),{
        position: "fixed",
        top: 0,
        left: 0,
        zIndex: 999999,
    });
    function post(url,payload,onload){
        function shapeParam(json){
            return Object.keys(json).map(v=>v+'='+json[v]).join('&');
        }
        GM.xmlHttpRequest({
            method: "POST",
            url: url,
            data: shapeParam(payload),
            headers: {
                "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
            },
            onload: function(response) {
                onload(response.responseText);
            }
        });
    };
    function main(){
        const id = location.href.match(/[\?&]v=([^&]+)/)[1];
        const title = document.getElementsByTagName("title")[0].innerText.replace(/ - YouTube$/,'');
        unsafeWindow.id = id;
        setCSS(elm,{
            display: "none"
        });
        post("https://www.y2mate.com/mates/mp3/ajax",{
            url: location.href,
            q_auto: 1,
            ajax: 1,
        },function(){
            main2(id, title);
        });
    };
    function main2(id, title){
        post("https://www.y2mate.com/mates/mp3Convert",{
            type: "youtube",
            _id: "5f40aa59d684eb68478b4571",
            v_id: id,
            mp3_type: "320", // kbps
            token: '',
        },function(res) {
            unsafeWindow.s = res;
            setCSS(elm,{
                display: "block"
            });
            const url = res.match(/<a href=\\"[^"]+/)[0].match(/".+$/)[0].slice(1,-1);
            download(url, title);
        });
    }
})();