nish_taka13 / YouTube To MP3 For Scratcher

// ==UserScript==
// @name        YouTube To MP3 For Scratcher
// @namespace   nish_taka13
// @description Convert youtube video to MP3
// @include     /(^https?:\/\/www\.|^https?:\/\/)youtube\.com\/watch\?v=.*/
// @include     /(^https?:\/\/www\.|^https?:\/\/)onlinevideoconverter.com/ja/mp3-converter\/.*/
// @include     /(^https?:\/\/www\.|^https?:\/\/)scratch.mit.edu\/.*/
// @include     /(^https?:\/\/www\.|^https?:\/\/)http://offliberty.io\/.*/
// @include     /convert2mp3\.net\/(en)?.*/
// @version     1.1
// @history     1.1 "Offlibertyを追加"
// @license     MIT
// @require     https://code.jquery.com/jquery-2.1.1.min.js
// @require     https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant       GM_addStyle
// @grant       GM_setValue
// @grant       GM_getValue
// ==/UserScript==


var url = location.href;

if (url.indexOf("ytmp3") >= 0) {

    document.getElementById("input").value = GM_getValue("youtubeLink");

} else if (url.indexOf("convert2mp3") >= 0) {

    waitForKeyElements( 'button', click );
    waitForKeyElements( '[class*=btn-success]', getDownloadLink );
    document.getElementById("urlinput").value = GM_getValue("youtubeLink");
    document.getElementsByClassName("orange")[0].click();

} else if (url.indexOf("listentoyoutube") >= 0) {

    document.getElementsByClassName("url-form__url")[0].value = GM_getValue("youtubeLink");
    document.getElementById("go-button").click();


} else if (url.indexOf("mp3fiber") >= 0) {

    waitForKeyElements( 'div.searchDiv > p > a[onclick]', getDownloadLink );
    document.getElementsByClassName("inputbg")[0].value = GM_getValue("youtubeLink");

} else {

    GM_setValue("youtubeLink", url);
    //var title = $("#watch8-action-buttons");
    var title = $("#flex");

    //waitForKeyElements( "#watch8-action-buttons", addLinks );
    waitForKeyElements( ".view-count.style-scope.yt-view-count-renderer", addLinks );

    var getLink = url;
    var id = "";

    if (url.indexOf("&") == -1) {

        encodeUrl();
        id = url.substr(url.indexOf("=")+1);
    }
    else {

        getLink = url.substring(0, url.indexOf("&"));
        id = url.substring(url.indexOf("=")+1, url.indexOf("&"));
        encodeUrl();
    }

    var OVC= "https://www.onlinevideoconverter.com/ja/mp3-converter";
    var Scratch= "https://scratch.mit.edu/";
    var Offliberty= "http://offliberty.io/";


    var mp3 = '<span> </span>' +
        '<b>|</b><a id="mp1" target="_blank" href="' + OVC + '" ><span> OVC </span></a><span> </span>' +
        '<b>|</b><a id="mp1" target="_blank" href="' + Scratch + '" ><span> Scratch</span></a><span> </span>' +
        '<b>|</b><a id="mp1" target="_blank" href="' + Offliberty + '" ><span> Offliberty </span></a><span> </span>' ;
    title.append(mp3);
}

function encodeUrl() {

    getLink = getLink.replace(/&/g,"%26");
    getLink = getLink.replace(/\?/g,"%3F");
    getLink = getLink.replace(/=/g,"%3D");
    getLink = getLink.replace(/\//g,"%2F");
    getLink = getLink.replace(/:/g,"%3A");
    getLink = getLink.replace(/_/g,"%5F");
    getLink = getLink.replace("feature=player_embedded&","");

}

function click (jNode) {

    jNode.click();

}

function addLinks (jNode) {

    jNode.append(mp3);

}

function getDownloadLink (jNode) {

    location.replace(jNode.prop("href"));

}