caps_enhancer/modules/mnemoschema.js
2026-03-23 14:02:46 +03:00

179 lines
No EOL
6.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// ==================== МОДУЛЬ МНЕМОСХЕМЫ ====================
(function() {
const core = window.VideoIPRedirect;
if (!core) {
console.error('[CAPS_Enhancer] CORE не загружен!');
return;
}
const debugLog = (msg, ...args) => core.debugLog('mnemo', msg, ...args);
debugLog('🏠 Загрузка модуля...');
// Функция для получения хоста из текущего URL
function getCurrentHostFromUrl() {
const hostname = window.location.hostname;
debugLog('Используем hostname:', hostname);
return hostname;
}
// Функция для поиска ссылки на мнемосхему
function findMnemoLink() {
debugLog('Поиск ссылки на мнемосхему');
const allLinks = document.querySelectorAll('a');
for (let link of allLinks) {
const text = link.textContent.toLowerCase().trim();
const href = link.getAttribute('href') || '';
if (text.includes('мнемосхема') || href.includes('/scheme')) {
debugLog('✅ Найдена!');
return link;
}
}
debugLog('❌ Ссылка не найдена');
return null;
}
// Кнопка "Лещ" — переход на host:порт_видео (тот же стиль, что и Мнемосхема)
function createVideoButton(host) {
const oldVideoBtn = document.querySelector('.video-redirect-btn');
if (oldVideoBtn) oldVideoBtn.remove();
const portVideo = core.TARGET_PORT_VIDEO || core.CONFIG?.ports?.video || '5000';
const targetUrl = `http://${host}:${portVideo}`;
const button = core.createStyledButton(
'Лещ',
(e) => {
e.preventDefault();
e.stopPropagation();
window.open(targetUrl, '_blank');
},
'video-redirect-btn'
);
button.setAttribute('data-host', host);
button.setAttribute('data-port', portVideo);
return button;
}
// Функция для создания кнопки "Мнемосхема"
function createButton(originalLink, host) {
const oldBtn = document.querySelector('.scheme-redirect-btn');
if (oldBtn) oldBtn.remove();
const mode = core.CONFIG?.mnemo?.mode || 'host-port';
const currentHostWithPort = window.location.host || host;
let targetUrl;
if (mode === 'host-scheme') {
targetUrl = `http://${currentHostWithPort}/scheme`;
} else {
targetUrl = `http://${host}:${core.TARGET_PORT_SCHEME}`;
}
const button = core.createStyledButton(
'Мнемосхема',
(e) => {
e.preventDefault();
e.stopPropagation();
window.open(targetUrl, '_blank');
},
'scheme-redirect-btn'
);
// Для кнопки мнемосхемы используем <a> вместо <button> для совместимости
button.href = '#';
button.setAttribute('data-host', host);
button.setAttribute('data-port', core.TARGET_PORT_SCHEME);
button.style.textDecoration = 'none';
return button;
}
// Главная функция
function addButton() {
debugLog('========== НАЧАЛО ==========');
const pageType = core.checkPageType();
if (!pageType.isMainPage) {
debugLog('Это НЕ главная страница');
return false;
}
// Пропуск по домену: не добавлять кнопку, если зашли по домену (не по IP)
const skipOnDomain = core.CONFIG?.mnemo?.skipOnDomain === true;
if (skipOnDomain) {
const hostname = window.location.hostname;
const isIp = /^[0-9.]+$/.test(hostname);
if (!isIp) {
debugLog('skipOnDomain: заход по домену, кнопку не добавляем');
return false;
}
}
if (core.activeSchemeButton) {
debugLog('Кнопка уже добавлена');
return false;
}
const mnemoLink = findMnemoLink();
if (!mnemoLink) {
debugLog('Ссылка не найдена');
return false;
}
const host = getCurrentHostFromUrl();
debugLog('Используем host:', host);
const addVideo = core.CONFIG?.mnemo?.addVideoButton !== false;
const addScheme = core.CONFIG?.mnemo?.addSchemeButton !== false;
if (!addVideo && !addScheme) {
debugLog('Обе кнопки отключены в настройках');
return false;
}
const wrapper = document.createElement('span');
wrapper.className = 'scheme-buttons-wrapper';
wrapper.style.display = 'inline-block';
wrapper.style.verticalAlign = 'middle';
if (addVideo) wrapper.appendChild(createVideoButton(host));
if (addScheme) wrapper.appendChild(createButton(mnemoLink, host));
mnemoLink.parentNode.replaceChild(wrapper, mnemoLink);
core.activeSchemeButton = true;
debugLog('✅ Кнопка успешно добавлена!');
debugLog('========== КОНЕЦ ==========');
return true;
}
window.VideoIPRedirect.Mnemoschema = {
addSchemeButton: addButton,
removeSchemeButton: function() {
const wrapper = document.querySelector('.scheme-buttons-wrapper');
if (wrapper) {
wrapper.remove();
core.activeSchemeButton = false;
return true;
}
const btn = document.querySelector('.scheme-redirect-btn');
if (btn) {
btn.remove();
core.activeSchemeButton = false;
return true;
}
const videoBtn = document.querySelector('.video-redirect-btn');
if (videoBtn) {
videoBtn.remove();
core.activeSchemeButton = false;
return true;
}
return false;
}
};
debugLog('✅ Модуль загружен');
})();