Ahab / Torntube

// ==UserScript==
// @name         Torntube
// @namespace    torn
// @version      1.3
// @author       Ahab [1735214]
// @description  Youtube scraping thanks to https://github.com/HermanFassett/youtube-scrape
// @include      *torn.com*
// @license      MIT
// @grant        GM_xmlhttpRequest
// @require      https://greasyfork.org/scripts/5392-waitforkeyelements/code/WaitForKeyElements.js?version=115012
// ==/UserScript==

function tubesearch(){
    if($("script[src*='/js/script/src/travel_agency_travelling.js']").length > 0){
        $('<div id="yousearch">').append('<input type="text" id="searcharea" style="font-size: 15px;">').appendTo('#skip-to-content')
    }else if($("script[src*='/js/script/src/travel_agency_travelling.js']").length === 0){
        $('<div id="yousearch">').append('<input type="text" id="searcharea" style="font-size: 15px;">').appendTo('div.content-title.m-bottom10 > div.clear')
    }
    $('<span style="cursor: default;">').append(' <input type="button" id="searchbutton" style="font-size: 15px; border-radius: 15px; width: 60px; cursor: pointer;" value="Search"/> <input type="button" id="clearbutton" style="font-size: 15px; border-radius: 15px; width: 50px; cursor: pointer;" value="Clear"/>').appendTo('#yousearch')
    $(document).on('click', '#searchbutton', function(e) {
        e.stopImmediatePropagation()
        tube()
    })
    $(document).on('click', '#clearbutton', function(e) {
        e.stopImmediatePropagation()
        if($('div[id^="list"]').length > 0){
            $('div[id^="list"]').remove()
        }else if($('div[id^="list"]').length === 0){
            $('iframe[id^="ytplayer"]').remove()
        }
    })
    document.getElementById("searcharea").addEventListener("keyup", function(e) {
        if (e.keyCode === 13) {
            tube()
        }
    })
    $("button[class*='recently-history']").removeAttr("disabled")
}

function tube(){
    GM_xmlhttpRequest({
        method: "GET",
        url: 'https://scrapeyou.herokuapp.com/api/search?q='+$("input[id*='searcharea']").val()+'&page=1',
        onload: function(response) {
            if (response.status != 200) {
                if($('div[id^="list"]')){
                    $('div[id^="list"]').remove()
                }
                $('<div id="list">').append('<span>Scraper dead try again later</span>').appendTo('#yousearch')
            }else{
            var d = JSON.parse(response.responseText)
            var t = ""
            $("input[id*='searcharea']").val("")
            if($('div[id^="list"]')){
                $('div[id^="list"]').remove()
            }
            for (var i = 0; i < Object.keys(d['results']).length; i++){
                if(d['results'][i]['video']){
                    $('<div id="list">').append('<span style="font-size: 12px; cursor: pointer; color:#333;" id="vidid" data-id='+d['results'][i]['video']['id']+'>'+d['results'][i]['video']['title']+'</span>').appendTo('#yousearch')
                }}
            $(document).on('click', '#vidid', function(e) {
                e.stopImmediatePropagation()
                if(screen.width <= 480){
                    t = '<iframe id="ytplayer" type="text/html" width="340" height="360"src="https://www.youtube.com/embed/'+$(e.target).attr('data-id')+'?autoplay=1"frameborder="0"></iframe>'
                }else{
                    t = '<iframe id="ytplayer" type="text/html" width="640" height="360"src="https://www.youtube.com/embed/'+$(e.target).attr('data-id')+'?autoplay=1"frameborder="0"></iframe>'
                }
                if($('iframe[id^="ytplayer"]')){
                    $('iframe[id^="ytplayer"]').remove()
                    $(t).appendTo('div.content-title.m-bottom10 > div.clear')
                }else{
                    $(t).appendTo('div.content-title.m-bottom10 > div.clear')
                }
            })}}
    })
}

waitForKeyElements("#skip-to-content", tubesearch);