Ahab / Flytube

// ==UserScript==
// @name         Flytube
// @namespace    torn
// @version      1.0
// @author       Ahab [1735214]
// @description  Youtube scraping thanks to https://github.com/HermanFassett/youtube-scrape
// @include      *torn.com*
// @license      MIT
// @grant        GM_xmlhttpRequest
// ==/UserScript==

if($("script[src*='/js/script/src/travel_agency_travelling.js']").length > 0){
    $('<div id="search">').append('<input type="text" id="searcharea" style="font-size: 15px;">').appendTo('#skip-to-content')
    $('<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('#search')
    $(document).on('click', '#searchbutton', function(e) {
        e.stopImmediatePropagation()
        tube()
    })
    $(document).on('click', '#clearbutton', function(e) {
        e.stopImmediatePropagation()
        if($('div[id^="list"]').length >= 1){
            $('div[id^="list"]').remove()
        }else{
            $('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('#skip-to-content')
                }else{
                    $(t).appendTo('#skip-to-content')
                }
            })}}
    })
}