Automatic Server

Automatically change server to preferred server type on Kissanime

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         Automatic Server
// @namespace    https://greasyfork.org/users/154522
// @version      1.1
// @description  Automatically change server to preferred server type on Kissanime
// @author       G-Rex
// @match        http://kissanime.ru/Anime/*/Episode*
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        unsafeWindow
// @run-at       document-body
// ==/UserScript==

let hijackFailed = false;

(function() {
    'use strict';

	let defaultServer = getDefaultServer();
	let currentServer = getCurrentServer();
	if(currentServer && defaultServer !== currentServer) {
		changeServer(defaultServer);
	}
	hijackServerSelection();
})();

function getDefaultServer() {
	let defaultServer = localStorage.getItem('server');

	if(defaultServer) {
		localStorage.removeItem('server');
		GM_setValue('server', defaultServer);
		return defaultServer;
	}

	defaultServer = GM_getValue('server', null);
	if(defaultServer) {
		return defaultServer;
	}

	defaultServer = 'default';
	GM_setValue('server', defaultServer);

	return defaultServer;
}

function hijackServerSelection() {
	let selection = document.getElementById('selectServer');
	if(!selection) {
		if(!hijackFailed) {
			//change the time if you keep being redirected when changing servers
			setTimeout(() => {hijackServerSelection();}, 2000);
		}

		hijackFailed = true;
		return;
	}
	selection.setAttribute('onChange', 'doChange(this.value);');
	let options = selection.getElementsByTagName('option');
	writeScript();
}

function getCurrentServer() {
	return getServer(window.location.href);
}

function writeScript() {
	let head = document.getElementsByTagName('head');
	head = head[0];
	let inner = head.innerHTML;
	let script = document.createElement('script');
    script.setAttribute("type", "application/javascript");
	script.textContent = 'function doChange(value) {' +
		'	saveServer(value);' +
		'	changePage(value);' +
		'}' +

		'function saveServer(value) {' +
		'	let server = getServer(value);' +
		'	localStorage.setItem(\'server\', server);' +
		'}' +

		'function changePage(value) {' +
		'	window.location.href = value;' +
		'}' +

		'function getServer(url) {' +
		'	let serverIndex = url.search(/(?<=s=).*$/);' +
		'	let server = url.substring(serverIndex);' +
		'	return server;' +
		'}';
	document.body.appendChild(script);
}

function doChange(value) {
	saveServer(value);
	changePage(value);
}

function saveServer(value) {
	let server = getServer(value);
	localStorage.setItem('server', server);
}

function changePage(value) {
	window.location.href = value;
}

function changeServer(server) {
	let currentPage = window.location.href;
	let serverIndex = currentPage.search(/(?<=s=).*$/);
	let newPage = currentPage.substring(0, serverIndex);
	newPage += server;
	unsafeWindow.location = newPage;
}

function getServer(url) {
	let serverIndex = url.search(/(?<=s=).*$/);
	let server = url.substring(serverIndex);

	if(server) {
		return server;
	}

	return null;
}