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

157 lines
6.3 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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('[VideoIPRedirect] CORE не загружен!');
return;
}
const debugLog = (msg, ...args) => core.debugLog('autologin', msg, ...args);
debugLog('🔐 Загрузка модуля...');
// Проверка, является ли страница страницей логина
function isLoginPage() {
// Проверка 1: Порт 5000
const port = window.location.port;
if (port !== '5000') {
return false;
}
// Проверка 2: URL содержит /login
const path = window.location.pathname;
if (!path.includes('/login')) {
return false;
}
// Проверка 3: Наличие полей логина и пароля
const usernameField = document.querySelector('input[name="username"]');
const passwordField = document.querySelector('input[name="password"]');
if (!usernameField || !passwordField) {
return false;
}
return true;
}
// Главная функция автологина
function performAutoLogin() {
debugLog('========== ПРОВЕРКА АВТОЛОГИНА ==========');
// Проверяем, включен ли автологин
if (!core.CONFIG.autologin.enabled) {
debugLog('⏹ Автологин отключен в настройках');
return false;
}
// Проверяем, что это страница логина
if (!isLoginPage()) {
debugLog('❌ Это не страница логина');
return false;
}
debugLog('✅ Обнаружена страница логина');
// Получаем данные для входа
const username = core.CONFIG.autologin.username;
const password = core.CONFIG.autologin.password;
if (!username || !password) {
debugLog('⚠️ Логин или пароль не заданы в настройках');
return false;
}
debugLog('🔑 Данные для входа найдены');
// Находим поля
const usernameField = document.querySelector('input[name="username"]');
const passwordField = document.querySelector('input[name="password"]');
const submitButton = document.querySelector('button[type="submit"]');
if (!usernameField || !passwordField) {
debugLog('❌ Не удалось найти поля ввода');
return false;
}
// Заполняем поля
debugLog('✍️ Заполнение полей...');
usernameField.value = username;
passwordField.value = password;
// Генерируем события для совместимости с JS-фреймворками
usernameField.dispatchEvent(new Event('input', { bubbles: true }));
usernameField.dispatchEvent(new Event('change', { bubbles: true }));
passwordField.dispatchEvent(new Event('input', { bubbles: true }));
passwordField.dispatchEvent(new Event('change', { bubbles: true }));
debugLog('✅ Поля заполнены');
// Автоматический вход, если включен
if (core.CONFIG.autologin.autoSubmit && submitButton) {
const delay = core.CONFIG.autologin.delay || 500;
debugLog(`⏱️ Вход через ${delay}мс...`);
setTimeout(() => {
debugLog('🚀 Нажатие кнопки "Вход"');
submitButton.click();
}, delay);
} else {
debugLog(' Автоматический вход отключен');
}
debugLog('========== АВТОЛОГИН ЗАВЕРШЕН ==========');
return true;
}
// Функция с повторными попытками (на случай если DOM еще не загружен)
function tryAutoLogin(attempts = 3) {
debugLog(`🔄 Попытка автологина (осталось попыток: ${attempts})`);
const success = performAutoLogin();
if (!success && attempts > 0) {
setTimeout(() => tryAutoLogin(attempts - 1), 500);
}
}
// Однократный вход по кнопке (без проверки enabled, всегда отправка формы)
function performLoginOnce() {
if (!isLoginPage()) {
return { ok: false, error: 'not_login_page' };
}
const username = core.CONFIG.autologin.username;
const password = core.CONFIG.autologin.password;
if (!username || !password) {
return { ok: false, error: 'no_credentials' };
}
const usernameField = document.querySelector('input[name="username"]');
const passwordField = document.querySelector('input[name="password"]');
const submitButton = document.querySelector('button[type="submit"]');
if (!usernameField || !passwordField) {
return { ok: false, error: 'no_fields' };
}
usernameField.value = username;
passwordField.value = password;
usernameField.dispatchEvent(new Event('input', { bubbles: true }));
usernameField.dispatchEvent(new Event('change', { bubbles: true }));
passwordField.dispatchEvent(new Event('input', { bubbles: true }));
passwordField.dispatchEvent(new Event('change', { bubbles: true }));
if (submitButton) {
const delay = core.CONFIG.autologin.delay || 500;
setTimeout(() => submitButton.click(), delay);
}
return { ok: true };
}
// Публичный API
window.VideoIPRedirect.AutoLogin = {
performAutoLogin: performAutoLogin,
tryAutoLogin: tryAutoLogin,
isLoginPage: isLoginPage,
performLoginOnce: performLoginOnce
};
debugLog('✅ Модуль загружен');
})();