Cpt_mathix / MyAnimeList(MAL) - Random Anime/Manga/People & Characters

// ==UserScript==
// @name         MyAnimeList(MAL) - Random Anime/Manga/People & Characters
// @version      1.3.6
// @description  Search for a random anime, manga, person or character
// @author       Cpt_mathix
// @match        *://myanimelist.net/*
// @exclude      *://myanimelist.net/animelist*
// @exclude      *://myanimelist.net/mangalist*
// @licence      GPL-2.0+; http://www.gnu.org/licenses/gpl-2.0.txt
// @grant        none
// @noframes
// @namespace https://greasyfork.org/users/16080
// ==/UserScript==

if (document.location.href.indexOf('hideLayout') == -1) {
	if (!document.getElementById('randAnime'))
		anime();
	if (!document.getElementById('randManga'))
		manga();
	if (!document.getElementById('randPerson'))
		person();
	if (!document.getElementById('randChar'))
		character();
}

if (getInfoFromUrl(document.location.href, "random") === "true") {
	var header_right = document.getElementsByClassName("header-right")[0];
	header_right.insertAdjacentHTML("afterbegin", '<a href="javascript:void(0);" id="new_random" class="js-anime-edit-info-button">New Random</a> - ');
	header_right.addEventListener("click", function() {
		var type = getInfoFromUrl(document.location.href, "randomType");
		switch(type) {
			case "anime":
				getRandomAnime();
				break;
			case "manga":
				getRandomManga();
				break;
			case "people":
				getRandomPeople();
				break;
			case "character":
				getRandomCharacter();
				break;
			default:
				alert("Something went wrong, sorry for the inconvenience");
				break;
		}
	});
}

function anime() {
    var animeDropmenu = document.querySelector('#nav > li:nth-child(1) > ul');
    var newli1 = document.createElement('li');
	var html1 = "<a id=\"randAnime\" href=\"javascript:void(0)\">Random Anime</a>";
    newli1.innerHTML = html1;
    animeDropmenu.insertBefore(newli1, document.querySelector('#nav > li:nth-child(1) > ul > li:nth-child(3)').nextSibling);
	newli1.addEventListener('click', function() {
		getRandomAnime();
	});
}

function manga() {
    var mangaDropmenu = document.querySelector('#nav > li:nth-child(2) > ul');
    var newli2 = document.createElement('li');
    var html2 = "<a id=\"randManga\" href=\"javascript:void(0)\">Random Manga</a>";
    newli2.innerHTML = html2;
    mangaDropmenu.insertBefore(newli2, document.querySelector('#nav > li:nth-child(2) > ul > li:nth-child(2)').nextSibling);
	newli2.addEventListener('click', function() {
		getRandomManga();
	});
}

function person() {
    var industryDropmenu = document.querySelector('#nav > li:nth-child(4) > ul');
    var newli3 = document.createElement('li');
    var html3 = "<a id=\"randPerson\" href=\"javascript:void(0)\">Random Person</a>";
	newli3.innerHTML = html3;
	industryDropmenu.insertBefore(newli3, document.querySelector('#nav > li:nth-child(4) > ul > li:nth-child(3)').nextSibling);
	newli3.addEventListener('click', function() {
		getRandomPeople();
	});
}

function character() {
	var industryDropmenu = document.querySelector('#nav > li:nth-child(4) > ul');
    var newli4 = document.createElement('li');
    var html4 = "<a id=\"randChar\" href=\"javascript:void(0)\">Random Character</a>";
    newli4.innerHTML = html4;
    industryDropmenu.appendChild(newli4);
	newli4.addEventListener('click', function() {
		getRandomCharacter();
	});
}

function getRandomAnime() {
	var r = Math.floor(Math.random() * 38000);
	$.get('/includes/ajax.inc.php?t=64&id=' + r, function(result) {
		if (result.length > 29) {
			document.location.href = '/anime/' + r  + '?random=true&randomType=anime';
		} else {
			getRandomAnime();
		}
	}).fail( function() {
		getRandomAnime();
	});
}

function getRandomManga() {
	var r = Math.floor(Math.random() * 120000);
	$.get('/includes/ajax.inc.php?t=65&id=' + r, function(result) {
		if (result.length > 0) {
			document.location.href = '/manga/' + r  + '?random=true&randomType=manga';
		} else {
			getRandomManga();
		}
	}).fail( function() {
		getRandomManga();
	});
}

function getRandomPeople() {
	var r = Math.floor(Math.random() * 50000);
	$.get('/people/' + r, function(result) {
		document.location.href = '/people/' + r + '?random=true&randomType=people';
	}).fail( function() {
		getRandomPeople();
	});
}

function getRandomCharacter() {
	var r = Math.floor(Math.random() * 160000);
	$.get('/character/' + r, function(result) {
		console.log($(result).find('#content > div.badresult'));
		if(!$(result).find('#content > div.badresult').length) {
			document.location.href = '/character/' + r + '?random=true&randomType=character';
		} else {
			getRandomCharacter();
		}
	}).fail( function() {
		getRandomCharacter();
	});
}

function getInfoFromUrl(url, info) {
	if (url.indexOf('?') === -1)
		return null;

	var urlVariables = url.split('?')[1].split('&'),
		varName;

	for (var i = 0; i < urlVariables.length; i++) {
		varName = urlVariables[i].split('=');

		if (varName[0] === info) {
			return varName[1] === undefined ? null : varName[1];
		}
	}
}