// ==UserScript==
// @name night_mode
// @author Мифист
// @namespace Мифист
// @description Ночная тема
// @homepage https://gf.qytechs.cn/ru/users/687755-мифист
// @icon https://i.ibb.co/hCpwbP0/yan.png
// @version 10.0
// @encoding utf-8
// @include https://*heroeswm.ru/*
// @include https://*lordswm.com/*
// @include http://*178.248.235.15/*
// @exclude */chat*
// @exclude */war*
// @exclude */cgame*
// @exclude /\/quest_(?!journal)/
// @exclude /\.(ru|com)\/$/
// @run-at document-start
// @noframes
// ==/UserScript==
(function(window, src) {
'use strict';
if (window.top !== window.self || !location.pathname || !/(www\.(heroeswm|lordswm)\.(ru|com)|178\.248\.235\.15)/.test(src) || /\/war|\/quest_(?!journal)|\/cgame|card_game/.test(src) || typeof localStorage !== 'object') return;
var ver = '10.0';
var key = 'nightMode';
function git(s) {
return 'https://laurent-kir.github.io/hwm/night_mode/' + s;
}
var $ = (function(reg) {function fn(e, c) {if (e.nodeType) return e;if (!c && e[0] === '#' && !reg.test(e)) return document.getElementById(e.slice(1));if (c && typeof c === 'string') c = fn(c);return (c === null) ? c : (c || document).querySelector(e);}function extend(target, source) {for (var key in source) {target[key] = source[key];}return target;}return extend(fn, {toArray: function(a) {var res = [], i = 0;for (; i < a.length; res.push(a[i++]));return res;},parseNode: function(html, callback) {var div = document.createElement('div'); div.innerHTML = html; div = div.firstChild.cloneNode(!0); callback && callback(div); return div;},index: function(e) {return fn.toArray(e.parentNode.children).indexOf(e);},rnd: function(n) {return Math.random() * n >> 0;},extend: extend});})(/[\s.,:>+~\[]/);
var $$ = function(s, c) {c = c && $(c);return (c === null) ? [] : $.toArray((c || document).querySelectorAll(s));};
var css = {
main: $.parseNode('<link rel="stylesheet" href="' + git('style.css') + '">'),
headerColored: $.parseNode('<link rel="stylesheet" href="' + git('header_colored.css') + '">'),
switcher: $.parseNode('<style>#night_mode_switcher {width: 22px;height: 22px;position: fixed;right: 10px; bottom: 10px;margin: 0;padding: 0;color: transparent;outline: none;border: 1px solid #888;border-radius: 50%;background: url("https://i.ibb.co/dJCMwGN/switcher.png") center /contain;background-color: #333;cursor: pointer;z-index: 9999;opacity: .9;transition: transform .3s;}#night_mode_switcher:hover {opacity: 1;}[night-mode] #night_mode_switcher {background-color: #ccc;transform: rotate(180deg);}[data-page="frames"] #night_mode_switcher {display: none;}</style>'),
update: $.parseNode('<style>#NM_update {max-height: 0;position: -webkit-sticky;position: sticky;top: 0;background-color: rgba(225, 225, 225, 0.85);overflow: hidden;z-index: 999;transition: max-height .3s ease-out;}#NM_update[shown] {max-height: 120px;margin-bottom: 8px;border-bottom: 1px solid #aaa;}.NM_update__inner {display: table;padding: 8px;}.NM_update__inner > div {display: table-cell;padding: 0 20px;vertical-align: middle;}.NM_update__info {background-color: #fff;border: 1px solid #aaa;}.NM_update__btns > label {display: block;margin-bottom: 8px;}#NM_update_check {vertical-align: middle;}/* ---- Night mode style ---- */[night-mode] #NM_update {background-color: rgba(64, 64, 64, 0.85);border-bottom-color: #666;}[night-mode] .NM_update__info {background-color: #333;border-color: #666;}</style>'),
};
var data = localStorage.nightModeData;
var dataTest = !!data;
data = dataTest ? JSON.parse(data) : ({
version: ver,
autoswitch: 0,
autoswitchTime: '00—06',
btnHidden: 0,
headerColored: 0,
lastCheckStamp: Date.now(),
noAlert: 0
});
if (!dataTest) localStorage.nightModeData = JSON.stringify(data);
function dataReload() {
localStorage.nightModeData = JSON.stringify(data);
return;
}
var path = location.pathname.slice(1).replace(/\.(php|html)$/, '');
var html = document.documentElement;
var head = document.head;
var switcher = $.parseNode('<button id="night_mode_switcher" title="Включить/выключить ночную тему"></button>');
function nightModeInit(test) {
if (!test) return false;
html.setAttribute('night-mode', '');
head.appendChild(css.main);
data.headerColored && head.appendChild(css.headerColored);
return true;
}
function nightModeOn() {
localStorage[key] = nightModeInit(1);
}
function nightModeOff() {
localStorage[key] = '';
html.removeAttribute('night-mode');
css.main.parentNode && head.removeChild(css.main);
css.headerColored.parentNode && head.removeChild(css.headerColored);
}
function nightModeToggle() {
var check = String(!localStorage[key]).replace('false', '');
html[check ? 'setAttribute' : 'removeAttribute']('night-mode', '');
localStorage[key] = check;
if (check) {
head.appendChild(css.main);
data.headerColored && head.appendChild(css.headerColored);
} else {
css.main.parentNode && head.removeChild(css.main);
css.headerColored.parentNode && head.removeChild(css.headerColored);
}
}
function getSettingsTime() {
return data.autoswitchTime.split('—').map(Number);
}
function getServerHour() {
var hour = document.body.textContent.match(/(\d+):\d+.+online/);
return hour && +hour[1];
}
function autoSwitch() {
var hour = getServerHour();
var time = getSettingsTime();
if ((time[0] < time[1] && hour >= time[0] && hour < time[1]) || time[0] > time[1] && (hour >= time[0] || hour < time[1])) nightModeOn();
else nightModeOff();
}
head.appendChild($.parseNode('<style>:root {overflow-y: auto;} :root > body {overflow-y: visible;}</style>'));
html.setAttribute('data-page', path);
switcher.onclick = nightModeToggle;
switcher.hidden = data.btnHidden;
switcher.appendChild(css.switcher);
html.appendChild(switcher);
nightModeInit(localStorage[key]);
localStorage[key] && console.log('Night mode, v. ' + ver);
if (data.version !== ver) {
data.version = ver;
data.noAlert = 0;
dataReload();
}
if (path === 'frames' && localStorage[key]) {
return $$('frame').forEach(function(frame) {
frame.addEventListener('load', function() {
var win = this.contentWindow;
var doc = win.document;
var html = doc.documentElement;
var path = win.location.pathname.slice(1).replace(/\.(php|html)$/, '');
html.setAttribute('night-mode', '');
html.setAttribute('data-page', path);
doc.head.appendChild(css.main.cloneNode(!0));
data.headerColored && doc.head.appendChild(css.headerColored.cloneNode(!0));
});
});
}
window.addEventListener('load', function() {
document.body.appendChild(switcher);
data.autoswitch && autoSwitch();
if (path === 'home') {
(function(now) {
if (data.noAlert || ((now - data.lastCheckStamp) < 432000000)) return;
function notify() {
var plInfo = 'pl_info.php?id=5781303';
var nightModeLink = 'https://gf.qytechs.cn/ru/scripts/411415-night-mode';
var div = $.parseNode('<div id="NM_update"><div class="NM_update__inner"></div></div>');
var first = div.firstElementChild;
first.appendChild($.parseNode('<div class="NM_update__info">Вышло обновление <a href="' + nightModeLink + '">«Ночной темы»</a> для ГВД (by <a href="'+ plInfo + '">Мифист</a>), v. ' + ver + '</div>'));
first.appendChild($.parseNode('<div class="NM_update__btns"><label for="NM_update_check"><input type="checkbox" id="NM_update_check" />Отключить уведомления</label><button>Закрыть</button></div>'));
first.querySelector('button').onclick = function() {
var input = this.previousElementSibling.firstElementChild;
this.disabled = 1;
data.lastCheckStamp = Date.now();
data.noAlert = input.checked;
dataReload();
div.removeAttribute('shown');
setTimeout(function() {div.parentNode.removeChild(div);}, 400);
};
div.appendChild(css.update);
document.body.insertBefore(div, document.body.firstElementChild);
setTimeout(function() {div.setAttribute('shown', !0);}, 100);
}
var fr = $.parseNode('<iframe src="/photo_pl_photos.php?aid=300140&pl_id=5781303" hidden></iframe>');
document.body.appendChild(fr);
fr.onload = function() {
var doc = this.contentWindow.document;
var search = $('td[align="left"][height="10"] > font', doc);
ver = search && (search.textContent.match(/\d[^,]+/) || '')[0];
if (data.version !== ver) notify();
this.parentNode.removeChild(this);
};
})(Date.now());
} else if (path === 'pers_settings') {
(function(search) {
function newTr(elems) {
var tr = document.createElement('tr');
elems.forEach(function(el) {tr.appendChild(el);});
return tr;
}
function newTd(pos, html) {
var td = document.createElement('td');
td.className = 'wbwhite';
if (!pos) td.width = '35%';
else td.align = 'left';
td.innerHTML = ' ' + html;
return td;
}
/*----------------------------*/
var tr_head = document.createElement('tr');
var td_head = document.createElement('td');
td_head.className = 'wblight';
td_head.colSpan = 2;
td_head.innerHTML = ' <b>Ночная тема: настройки</b>';
tr_head.appendChild(td_head);
/*----------------------------*/
var tdl_0 = newTd(0, 'Цветная шапка');
var tdr_0 = newTd(1, '<input type="checkbox" name="headerColored" />');
var tr_0 = newTr([tdl_0, tdr_0]);
tdr_0.children[0].checked = data.headerColored;
tdr_0.children[0].onchange = function() {
data[this.name] = this.checked;
if (this.checked) head.appendChild(css.headerColored);
else css.headerColored.parentNode && head.removeChild(css.headerColored);
dataReload();
};
/*----------------------------*/
var tdl_1 = newTd(0, 'Скрывать кнопку-переключатель');
var tdr_1 = newTd(1, '<input type="checkbox" name="btnHidden" />');
var tr_1 = newTr([tdl_1, tdr_1]);
tdr_1.children[0].checked = data.btnHidden;
tdr_1.children[0].onchange = function() {
switcher.hidden = data[this.name] = this.checked;
dataReload();
};
/*----------------------------*/
var time = data.autoswitchTime.split('—');
var tdl_2 = newTd(0, 'Автопереключение (по серверу)');
var tdr_2 = newTd(1, '<input type="checkbox" name="autoswitch" /> с <input type="text" value="' + time[0] + '" maxlength="2" size="2" /> до <input type="text" value="' + time[1] + '" maxlength="2" size="2" />');
var tr_2 = newTr([tdl_2, tdr_2]);
var inputs = $.toArray(tdr_2.children);
inputs[1].disabled = inputs[2].disabled = !(inputs[0].checked = data.autoswitch);
inputs[0].onchange = function() {
var check = this.checked;
inputs.slice(1).forEach(function(el) {el.disabled = !check;});
data[this.name] = check;
dataReload();
};
inputs[1].onchange = inputs[2].onchange = function() {
var val = Number(this.value.slice(0, 2)) >> 0;
var ind = inputs.indexOf(this) - 1;
if (val < 0 || val > 23) val = 0;
this.value = time[ind] = ('0' + val).slice(-2);
data.autoswitchTime = time.join('—');
dataReload();
};
/*----------------------------*/
var rows = [tr_head, tr_0, tr_1, tr_2];
rows.forEach(function(row) {search.parentNode.insertBefore(row, search);});
})($('td.wbwhite[colspan="2"]').parentNode);
}
});
})(window, location.href);