[TypingTube] random_jump

try to take over the world!

// ==UserScript==
// @name         [TypingTube] random_jump
// @namespace    http://tampermonkey.net/
// @version      0.9
// @description  try to take over the world!
// @author       You
// @match        https://typing-tube.net/*
// @icon         https://www.google.com/s2/favicons?domain=typing-tube.net
// @grant        none
// ==/UserScript==
let tag = document.createElement('script');
 tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
//randomMaxLengthに大きすぎる値が保存されてしまった場合の対策
let ID = location.pathname.match(/\d+/)
if(ID && +ID[0] > localStorage.getItem('randomMaxLength')){
	localStorage.setItem('randomMaxLength',ID[0])
}
if(+localStorage.getItem('randomMaxLength') > 100000){
	localStorage.setItem('randomMaxLength',57945)
}
const randomMaxLength = localStorage.getItem('randomMaxLength') ? +localStorage.getItem('randomMaxLength') : 50571;


if(!location.href.match('net/movie')){
	document.querySelector("[href*='lv=5']").parentNode.insertAdjacentHTML('afterend', `<div style="cursor:pointer;" id="random_selector" class="m-4 col btn btn-outline-success">ランダム(抽選範囲${randomMaxLength})
    </div><span id=player_box style="position:absolute;visibility:hidden;"></span>`)
	document.getElementById("random_selector").addEventListener("click",randomSwitch)
}
let random
let player_clone



random_generator = function (){
	if(document.getElementById("player_box") == null){
		document.querySelector("head").insertAdjacentHTML('afterend', `<span id=player_box style="position:absolute;visibility:hidden;"></span>`)
	}
	random = Math.floor(Math.random() * (randomMaxLength - 5 + 1) + 5)
	$.ajax({
		type: 'POST',
		url: '/movie/lyrics/' + random,
		headers: {
            'X-CSRF-Token' : document.getElementsByName('csrf-token').item(0).content
        },
		success:function(data){
			if(data.match(/(v=).*\n/) != null){
				createReq = new createRequest(data , random , "Random")
				onYouTubeIframeAPIReady_random(data.match(/(v=).*\n/)[0].slice(2))
			}else{
				random_generator()
			}
		},error: function(data) {
			random_generator()
		}
	});
}



function reLottery(){
	Lost ++
	document.getElementById("random_selector").textContent = "ランダム(ハズレ"+Lost+"回目)"
	document.getElementById("player_box").remove()
	document.getElementById("random_selector").insertAdjacentHTML('afterend', `<span id=player_box style="position:absolute;visibility:hidden;"></span>`)
	document.getElementById("random_selector").style.cursor = "pointer"
	console.log(Lost)
	console.log(Lost % 10)
	if(Lost && Lost % 10 != 0){
		randomSwitch()
	}
}


function randomSwitch(){
	document.getElementById("random_selector").removeEventListener("click",randomSwitch)
	document.getElementById("random_selector").style.cursor = "none"
	random = Math.floor(Math.random() * (randomMaxLength - 5 + 1) + 5)
        $.ajax({
            type: 'POST',
			url: '/movie/lyrics/' + random,
			success:function(data){
				console.log(data)

				if(data.match(/(v=).*\n/) != null){
					onYouTubeIframeAPIReady_randomSwitch(data.match(/(v=).*\n/)[0].slice(2))
				}else{
					reLottery()
				}
			},error: function(data) {
				reLottery()
			}
		});
}

let player_random
function onYouTubeIframeAPIReady_randomSwitch(preview_videoid) {
console.log(preview_videoid)
	player_random = ""
     player_random = new YT.Player('player_box', {
        height: 100 ,
        width: 100 ,
        playerVars: {
            autoplay: 0,
            controls: 0,
            disablekb: 1,
            modestbranding:1,
            origin: location.protocol + '//' + location.hostname + "/",
            start: 0
        },
        videoId: preview_videoid,
        events: {
            'onReady': onPlayerReady_SwitchCheck,
            'onError': onPlayerReady_SwitchCheck,


        }
    });
}
let Lost = 0
function onPlayerReady_SwitchCheck(){
	try{
		if(player_random.getDuration() != 0){
			window.open('https://typing-tube.net/movie/show/'+random);
			document.getElementById("player_box").remove()
			document.getElementById("random_selector").insertAdjacentHTML('afterend', `<span id=player_box style="position:absolute;visibility:hidden;"></span>`)
			document.getElementById("random_selector").style.cursor = "pointer"
			document.getElementById("random_selector").addEventListener("click",randomSwitch)
		}else{
			reLottery()
		}
	}catch(e){
		//例外が発生した場合の処理
		reLottery()
	}


}