nish / 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?:\/\/)scratch.mit.edu\/.*/
// @include     /(^https?:\/\/www\.|^https?:\/\/)www.onlinevideoconverter.com/ja/video-converter\/.*/
// @include     /(^https?:\/\/www\.|^https?:\/\/)2conv.com/jp/\/.*/
// @version     1.4.0
// @history     1.1.0 "Offlibertyを追加"
// @history     1.2.0 "ScratchDiscussの移動先サイトの変更"
// @history     1.2.1 "クレジットの変更"
// @history     1.3.0 "クレジットの変更,VideoGrabberの追加,OVCの削除"
// @history     1.4.0 "クレジットの変更,2convの追加,VideoGrabberの削除"
// @history     1.5.0 "クレジットの変更,offlibertyの削除,OVCの追加"
// @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 conveter= "https://2conv.com/jp/";
    var Scratch= "https://scratch.mit.edu/discuss/youtube";
    var OVC= "https://www.onlinevideoconverter.com/ja/video-converter";


    var mp3 = '<span> </span>' +
      '<b>|</b><a id="mp1" target="_blank" href="' + conveter + '" ><span> conveter </span></a><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>' ;
    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"));

}