Files

215 lines
6.8 KiB
JavaScript

// ==UserScript==
// @name CatWar Mod Lite
// @name:ru Варомод Лайт
// @namespace https://catwar.net/blog482084
// @version 0.1
// @description Облегчённый набор улучшений интерфейса CatWar без тяжёлых наблюдателей
// @author ScriptTeam Lite
// @license MIT; https://opensource.org/licenses/MIT
// @match https://*.catwar.net/*
// ==/UserScript==
(function (window, document) {
'use strict';
/**
* Простые настройки, которые можно менять через консоль:
* window.CWModLite.set('compactLayout', true)
* window.CWModLite.set('quickLinks', ['about','top','sniff','settings'])
*/
const STORAGE_KEY = 'cwmod-lite';
const defaults = {
compactLayout: false,
quickLinks: ['index', 'top', 'sniff', 'settings']
};
const linkMap = {
index: { href: '/', title: 'Главная' },
about: { href: '/about', title: 'Об игре' },
top: { href: '/top', title: 'СИ' },
sniff: { href: '/sniff', title: 'Лента' },
blogs: { href: '/blogs', title: 'Блоги' },
settings: { href: '/settings#cwmod', title: 'Настройки' },
mobile: { href: '/mobile', title: 'Мобильная версия' }
};
const state = Object.assign({}, defaults, loadSettings());
const styles = {};
const isCW3 = /^https:\/\/\w*\.?catwar\.net\/cw3(?!\/(kns|jagd))/.test(window.location.href);
document.addEventListener('DOMContentLoaded', function () {
applyCommon();
if (isCW3) {
enhanceMenu();
applyCompactLayout();
injectToggle();
}
});
window.CWModLite = {
get(key) {
return state[key];
},
set(key, value) {
state[key] = value;
saveSettings(state);
if (key === 'compactLayout') applyCompactLayout();
if (key === 'quickLinks') enhanceMenu(true);
return state[key];
},
reset() {
Object.keys(defaults).forEach(function (key) {
state[key] = defaults[key];
});
saveSettings(state);
enhanceMenu(true);
applyCompactLayout();
}
};
// Applies shared stylesheet and DOM tweaks used on every supported page.
function applyCommon() {
const footer = document.getElementById('footer');
if (footer) {
const text = footer.innerHTML;
if (!/\/settings#cwmod/.test(text)) {
footer.innerHTML = text.replace('<br>©', ' | <a href="/settings#cwmod">Настройки</a><br>©');
}
}
}
// Rebuilds the compact menu when the page menu appears or changes.
function enhanceMenu(forceRefresh) {
const menu = document.querySelector('#app > span.small, .small');
if (!menu) return;
let placeholder = menu.querySelector('[data-cwmod-lite="links"]');
if (placeholder && !forceRefresh) return;
const selectedLinks = Array.isArray(state.quickLinks) ? state.quickLinks : defaults.quickLinks;
const fragment = document.createDocumentFragment();
const wrapper = document.createElement('span');
wrapper.dataset.cwmodLite = 'links';
selectedLinks.forEach(function (key, index) {
const link = linkMap[key];
if (!link) return;
const anchor = document.createElement('a');
anchor.href = link.href;
anchor.textContent = link.title;
anchor.rel = 'noopener';
anchor.target = '_self';
if (index > 0) wrapper.appendChild(document.createTextNode(' | '));
wrapper.appendChild(anchor);
});
if (placeholder) {
placeholder.replaceWith(wrapper);
} else {
fragment.appendChild(document.createTextNode(' | '));
fragment.appendChild(wrapper);
menu.appendChild(fragment);
}
}
// Adds the small-screen layout classes and hides nonessential controls.
function applyCompactLayout() {
toggleStyle('compact', state.compactLayout, `
body {
background-color: #f7f7f7;
}
#app {
max-width: 1040px;
margin: 10px auto;
padding: 0 10px;
}
#main_table {
width: 100%;
max-width: 1040px;
border-collapse: collapse;
background: rgba(255, 255, 255, 0.95);
border-radius: 12px;
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.08);
}
#fightLog {
max-height: 280px;
overflow-y: auto;
}
.small {
display: inline-flex;
flex-wrap: wrap;
gap: 6px;
align-items: center;
}
`);
}
// Inserts the user-facing toggle that enables or disables compact mode.
function injectToggle() {
const menu = document.querySelector('#app > span.small, .small');
if (!menu || menu.querySelector('[data-cwmod-lite="toggle"]')) return;
const toggle = document.createElement('button');
toggle.type = 'button';
toggle.dataset.cwmodLite = 'toggle';
toggle.textContent = state.compactLayout ? 'Лайт-режим вкл.' : 'Включить лайт-режим';
toggle.style.marginLeft = '8px';
toggle.style.padding = '2px 8px';
toggle.style.fontSize = '12px';
toggle.style.borderRadius = '6px';
toggle.style.border = '1px solid rgba(0,0,0,0.1)';
toggle.style.background = '#ffffffaa';
toggle.style.cursor = 'pointer';
toggle.addEventListener('click', function () {
state.compactLayout = !state.compactLayout;
saveSettings(state);
applyCompactLayout();
toggle.textContent = state.compactLayout ? 'Лайт-режим вкл.' : 'Включить лайт-режим';
});
menu.appendChild(document.createTextNode(' | '));
menu.appendChild(toggle);
}
// Persists one style preference and injects/removes its matching CSS rule.
function toggleStyle(key, shouldEnable, cssText) {
if (!shouldEnable) {
if (styles[key] && styles[key].parentNode) {
styles[key].parentNode.removeChild(styles[key]);
}
styles[key] = null;
return;
}
if (styles[key]) return;
const style = document.createElement('style');
style.dataset.cwmodLiteStyle = key;
style.textContent = cssText;
document.head.appendChild(style);
styles[key] = style;
}
// Reads the saved lite-mode settings, using defaults for malformed values.
function loadSettings() {
try {
const raw = window.localStorage.getItem(STORAGE_KEY);
if (!raw) return {};
const data = JSON.parse(raw);
return (data && typeof data === 'object') ? data : {};
} catch (err) {
console.warn('[CWModLite] Не удалось загрузить настройки', err);
return {};
}
}
// Serializes the current lite-mode settings to browser storage.
function saveSettings(settings) {
try {
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(settings));
} catch (err) {
console.warn('[CWModLite] Не удалось сохранить настройки', err);
}
}
})(window, document);