Automatic Server

Automatically change server to preferred server type on Kissanime

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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;
}