Slither.io auto respawn (evergreen)

Auto respawn + Skin rotator + Unlocked zoom + No skin + No BG + Reduce lag + Server info + Tab close warn + more!

目前为 2016-06-25 提交的版本。查看 最新版本

// ==UserScript==
// @name        Slither.io auto respawn (evergreen)
// @namespace   slitherioautorespawn
// @description Auto respawn + Skin rotator + Unlocked zoom + No skin + No BG + Reduce lag + Server info + Tab close warn + more!
// @version     1.2
// @author      condoriano
// @include     http://slither.io/*
// @include     https://slither.io/*
// @grant       none
// ==/UserScript==

(function() {
	'use strict';
	if(window.top != window.self) return;
	window.onbeforeunload = function() { return 'Quit game?'; };

	var optnDiv, optnBox, popupsDiv, trDiv, extrasDiv,
		gameBG = null,
		settings = {},
		playBtn = document.getElementById('playh').children[0],
		optnKeys = [ 'z', 'x', 'c', 'v', 'b', 'n', 'm' ],
		optnKeysInt = [ 90, 88, 67, 86, 66, 78, 77 ],
		overlayOpened = false;

	function init() {
		if(/Firefox/i.test(navigator.userAgent)) document.addEventListener("DOMMouseScroll", handleZoom, false);
		else document.body.onmousewheel = handleZoom;

		editHTML();
		hookHTML();
		hookKeys();

		var tempLoop = setInterval(function() {
			if(window.bgp2) {
				loadSettings();
				clearInterval(tempLoop);
			}
		}, 100);
	}

	function editHTML() {
		document.body.style.backgroundColor = '#' + getRandomInt(1, 6) + getRandomInt(1, 6) + getRandomInt(1, 6);
		document.getElementById('nick_holder').style.backgroundColor = 'rgba(0,0,0,0.5)';
		[ '#tips', '#nick', '#clq', '.nsi', '.lq2' ].forEach(function(e) {
			var b = document.querySelectorAll(e);
			for(var i = 0; i < b.length; i++) b[i].style.color = '#EEE';
		});
		[ '.sadg1', '.sadu1', '.sadd1' ].forEach(function(e) {
			var b = document.querySelectorAll(e);
			for(var i = 0; i < b.length; i++) b[i].style.background = hasClass(b[i], 'sadg1') ? 'rgba(250,250,250,0.1)' : hasClass(b[i], 'sadu1') ? 'rgba(250,250,250,0.15)' : 'rgba(0,0,0,0.5)';
		});

		optnDiv = document.createElement('div');
		optnDiv.id = 'optn';
		optnDiv.style = 'display: inline-block';
		optnDiv.innerHTML = '<a id="optnBtn" href="#" class="btn btnt"><img src="http://i.imgur.com/KeFKZvM.png" border="0" height="87" width="90"></a>';
		document.getElementById('grqh').appendChild(optnDiv);

		optnBox = document.createElement('div');
		optnBox.id = 'optnBox';
		optnBox.style = 'position: absolute; top: 100px; right: 0px; width: 280px; background-color: rgba(0,0,0,0.5); color: #BBB; font-family: Tahoma; font-size: 12px; border-radius: 12px; padding: 6px 12px;';
		optnBox.innerHTML += '<div id="optnBox-settings" style="padding: 6px 6px 12px; border-bottom: 1px dashed #555;"></div>';
		optnBox.innerHTML += '<div id="optnBox-keys" style="padding: 6px 6px 12px; border-bottom: 1px dashed #555;"></div>';
		optnBox.innerHTML += '<div style="padding-top: 4px; font-size: 9px; text-align: right">Mod by Condoriano. <a style="text-decoration: none; color: #AF3;" href="https://greasyfork.org/en/scripts/20897-slither-io-auto-respawn-evergreen" target="_blank">Homepage</a></div>';
		optnDiv.appendChild(optnBox);

		optnBox.children[0].innerHTML += '<span style="display: block; font-weight: bold; color: #3AF; margin-bottom: 4px; position: relative; left: -5px;">Game Settings</span>';
		optnBox.children[0].innerHTML += '<label><input id="optnAutoRespawn" type="checkbox">Auto respawn</label>';
		optnBox.children[0].innerHTML += '<label><input id="optnRotateSkin" type="checkbox">Rotate skin</label>';
		optnBox.children[0].innerHTML += '<label><input id="optnNoSkin" type="checkbox">No skins</label>';
		optnBox.children[0].innerHTML += '<label><input id="optnNoBG" type="checkbox">No background</label>';

		optnBox.children[1].innerHTML += '<span style="display: block; font-weight: bold; color: #3AF; margin-bottom: 4px; position: relative; left: -5px;">Controls / Shortcut keys</span>';
		optnBox.children[1].innerHTML += '<span><span>Mouse or Arrow keys</span> : Move snake</span>';
		optnBox.children[1].innerHTML += '<span><span>W or Space</span> : Speed boost</span>';
		optnBox.children[1].innerHTML += '<span><span>ESC</span> : Quit to main menu</span>';
		optnBox.children[1].innerHTML += '<span><span>&gt;</span> : Next skin</span>';
		optnBox.children[1].innerHTML += '<span><span>&lt;</span> : Prev skin</span>';

		popupsDiv = document.createElement('div');
		popupsDiv.id = 'popupsDiv';
		popupsDiv.style = 'position: absolute; display: flex; flex-direction: column-reverse; bottom: 10px; left: 210px; width: 260px; max-height: 200px; overflow: hidden; font-family: Tahoma; z-index: 5;';
		document.body.appendChild(popupsDiv);

		trDiv = document.createElement('div');
		trDiv.id = 'topRight';
		trDiv.style = 'position: absolute; top: 100px; left: 5px; z-index: 100;';
		document.body.appendChild(trDiv);

		extrasDiv = document.createElement('div');
		extrasDiv.id = 'extrasDiv';
		extrasDiv.innerHTML = '<div id="extras-ip"></div><div id="extras-location"></div><div id="extras-fps"></div>';
		extrasDiv.style = 'display: none; position: absolute; top: 5px; left: 5px; color: #AAA; font-family: Verdana; font-size: 12px; z-index: 5;';
		document.body.appendChild(extrasDiv);

		var optn = document.getElementById('optnBox-settings').children;
		for(var i = 1; i < optn.length; i++) {
			optn[i].style = 'display: inline-block; min-width: 50%;';
			var j = document.createElement('strong');
			j.innerHTML = '(' + optnKeys[i-1].toUpperCase() + ')';
			j.style = 'display: inline-block; min-width: 18px;';
			optn[i].insertBefore(j, optn[i].firstChild);
			optn[i].children[1].style = 'margin: 0px 4px; position: relative; top: 2px';
		}
		optn = document.getElementById('optnBox-keys').children;
		for(var i = 1; i < optn.length; i++) {
			optn[i].style = 'display: inline-block; min-width: 50%; margin: 1px 0px;';
			optn[i].children[0].style = 'display: inline-block; background-color: #000; color: #FFF; padding: 0px 6px; border-radius: 3px; min-width: 12px; text-align: center;';
		}
	}

	function hookHTML() {
		var checkboxes = optnBox.querySelectorAll('input[type="checkbox"]');
		for(var i = 0; i < checkboxes.length; i++) {
			(function(j) {
				checkboxes[j].addEventListener('change', function() {
					var theID = this.id;
					settings[theID] = this.checked;
					localStorage.setItem('settings', JSON.stringify(settings));
					createPopup(this.nextSibling.textContent + ' <span style="color: ' + (this.checked ? '#9D2;">Enabled</span>' : '#F33;">Disabled</span>'));

					if(theID == 'optnNoSkin') window.render_mode = this.checked ? 1 : 2;
					else if(theID == 'optnNoBG') window.bgp2 = this.checked ? null : gameBG;
				});
			})(i);
		}
		document.getElementById('nick').addEventListener('change', function() { // todo: this doesnt get called if press play or enter
			settings[this.id] = this.value;
			localStorage.setItem('settings', JSON.stringify(settings));
		});

		document.getElementById('optnBtn').addEventListener('click', function(e) {
			optnBox.style.display = optnBox.style.display == 'none' ? 'block' : 'none';
			e.preventDefault();
		});
		playBtn.addEventListener('click', function() { overlayOpened = false; document.getElementById('nick').blur(); });
	}

	function handleZoom(e) {
		if(!window.playing) return;
		window.gsc *= Math.pow(0.9, e.wheelDelta / -120 || e.detail / 2 || 0);
	}

	function loadSettings() {
		gameBG = window.bgp2;
		localStorage.setItem("edttsg", "1");
		if(localStorage.getItem('settings')) settings = JSON.parse(localStorage.getItem('settings'));
		var checkboxes = optnBox.querySelectorAll('input[type="checkbox"]');
		for(var i = 0; i < checkboxes.length; i++) { if(settings[checkboxes[i].id]) checkboxes[i].click(); }
		if(settings.hasOwnProperty('nick')) document.getElementById('nick').value = settings.nick;
		setInterval(logicLoop, 1000);
	}

	function logicLoop() {
		if(document.getElementById('optnAutoRespawn').checked) { if(!overlayOpened) playBtn.click(); }
		if(document.getElementById('optnRotateSkin').checked) changeSkin(1, false);

		if(window.playing) {
			extrasDiv.style.display = 'block';
			trDiv.style.display = 'none';
			document.getElementById('extras-ip').innerHTML = 'Server IP: <span style="color: #EEE;">' + window.bso.ip + ':' + window.bso.po + '</span';
			document.getElementById('extras-fps').innerHTML = 'FPS: <span style="color: #EEE;">' + window.fps + '</span';

			if(document.getElementById('extras-location').innerHTML == '') {
				var httpReq = new XMLHttpRequest();
				httpReq.open('GET', '//geoip.nekudo.com/api/' + window.bso.ip, true);
				httpReq.onreadystatechange = function(data) {
					if(httpReq.readyState == 4 && httpReq.status == 200) {
						var data = JSON.parse(httpReq.responseText);
						document.getElementById('extras-location').innerHTML = 'Server Location: <span style="color: #EEE;">' + data.city + ', ' + data.country.name + '</span';
					}
				};
				httpReq.send();
			}
		}
		else {
			extrasDiv.style.display = 'none';
			trDiv.style.display = 'block';
			document.getElementById('extras-location').innerHTML = '';
		}
	}

	function createPopup(msg, displayTime=1200, bgColor='rgba(0,0,0,0.7)') {
		var popup = document.createElement('div');
		popup.style = 'display: table; background-color: ' + bgColor + '; color: #DDD; margin: 2px 0px; max-width: 260px; padding: 0px 16px 2px 16px; border-radius: 30px; font-size: 12px;';
		popup.innerHTML = msg;
		popupsDiv.insertBefore(popup, popupsDiv.firstChild);
		setTimeout(function() { popup.remove(); }, displayTime);
	}

	function loadTrDiv() {
		window.google_ad_client = "ca-pub-8318511014856551"; window.google_ad_slot = "6797760423"; window.google_ad_width = 300; window.google_ad_height = 250;
		var container = document.getElementById('topRight'), script = document.createElement('script'), w = document.write;
		document.write = function (content) { container.innerHTML = content; document.write = w; };
		script.type = 'text/javascript';
		script.src = 'http://pagead2.googlesyndication.com/pagead/show_ads.js';
		document.body.appendChild(script);
		setTimeout(function() { script.remove(); }, 50000);
	}
	setTimeout(loadTrDiv, 10000);
	setInterval(loadTrDiv, 60000);

	function hookKeys() {
		document.addEventListener('keydown', function(e) {
			var key = e.keyCode || e.which;
			if(e.ctrlKey || e.altKey || e.shiftKey) return;
			if(document.activeElement.tagName == 'INPUT' && document.activeElement.type == 'text') return;
			if(key == 27) { //key ESC
				overlayOpened = overlayOpened ? false : true;
				window.want_close_socket = true;
				window.dead_mtm = 0;
				window.play_btn.setEnabled(true);
				window.resetGame();
			}
			else if(key == 190) changeSkin(1, true); //key >
			else if(key == 188) changeSkin(-1, true); //key <
			else {
				for(var i = 0; i < optnKeysInt.length; i++) {
					if(key == optnKeysInt[i]) document.getElementById('optnBox-settings').children[i+1].click();
				}
			}
		});
	}

	function changeSkin(e, popup) {
		if(!window.playing) return;
		var curSkin = window.snake.rcv, maxSkin = window.max_skin_cv;
		curSkin = curSkin + e;
		curSkin = curSkin < 0 ? maxSkin : curSkin > maxSkin ? 0 : curSkin;
		window.setSkin(window.snake, curSkin);
		if(popup) {
			createPopup('Skin #' + (curSkin + 1) + ' applied');
			localStorage.setItem('snakercv', curSkin);
		}
	}

	function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min)) + min; }
	function hasClass(element, cls) { return (' ' + element.className + ' ').indexOf(' ' + cls + ' ') > -1; }

	init();

})();