pootz / YouTube To MP3

// ==UserScript==
// @name        YouTube To MP3
// @namespace   pootz
// @description Convert youtube video to MP3
// @include     /(^https?:\/\/www\.|^https?:\/\/)youtube\.com\/watch\?v=.*/
// @include     /(^https?:\/\/www\.|^https?:\/\/)ytmp3\.cc\/.*/
// @include     /(^https?:\/\/www\.|^https?:\/\/)listentoyoutube\.com\/.*/
// @include     /(^https?:\/\/www\.|^https?:\/\/)mp3fiber\.com\/.*/
// @include     /convert2mp3\.net\/(en)?.*/
// @version     1.5
// @history     v1.5 - youtube new version
// @history     v1.4 - youtube updated
// @history     v1.3 - youtube updated their code, also replaced video2mp3 for ytmp3.cc
// @history     v1.2 - @updateURL updated
// @history     v1.1 - added new urls
// @license     MIT
// @require     https://code.jquery.com/jquery-2.1.1.min.js
// @require     https://gist.github.com/raw/2625891/waitForKeyElements.js
// @updateURL   https://openuserjs.org/meta/pootz/YouTube_To_MP3.meta.js
// @downloadURL https://openuserjs.org/install/pootz/YouTube_To_MP3.user.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 convert2mp3 = "http://convert2mp3.net/en/";
    var youtubeMP3 = "https://ytmp3.cc/";
    var listentoyoutube = "http://www.listentoyoutube.com/";
    var mp3fiber = "http://mp3fiber.com";

    var mp3 = '<span> </span>' +
        '<b>|</b><a id="mp1" target="_blank" href="' + convert2mp3 + '" ><span> Convert2mp3 </span></a><span> </span>' +
        '<b>|</b><a id="mp2" target="_blank" href="' + mp3fiber + '" ><span> MP3Fiber </span></a>' +
        '<b>|</b><a id="mp3" target="_blank" href="' + listentoyoutube + '" ><span> ListenToYoutube </span></a>' +
        '<b>|</b><a id="mp4" target="_blank" href="' + youtubeMP3 + '" ><span> YoutubeMP3 </span></a><b>|</b>';

    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"));

}