Automatic Server

Automatically change server to preferred server type on Kissanime

As of 2018-04-15. See the latest version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

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

(function() {
    'use strict';

	let defaultServer = getDefaultServer();
	let currentServer = getCurrentServer();
	if(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');
	selection.setAttribute('onChange', 'doChange(this.value);');
	let options = selection.getElementsByTagName('option');
	writeScript();
}

function getCurrentServer() {
	//let slection = document.getElementById('selectServer');
	//return getServer(selection.value);
	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 = '(' + fn + ')();';
	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);
	return server;
}