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

239 lines
No EOL
10 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('close', msg, ...args);
const isElementVisible = core.isElementVisible;
debugLog('❌ Загрузка модуля...');
// Функция для поиска кнопки закрытия ПО ТОЧНОМУ СЕЛЕКТОРУ
function findVideoCloseButton() {
debugLog('========== ПОИСК КНОПКИ ЗАКРЫТИЯ ВИДЕО ==========');
// 1. Ищем по точному классу sc-heWoWI
debugLog('Поиск по селектору: button.sc-heWoWI');
let closeBtn = document.querySelector('button.sc-heWoWI');
if (!closeBtn) {
// 2. Ищем по части класса
debugLog('Поиск по селектору: button[class*="sc-heWoWI"]');
closeBtn = document.querySelector('button[class*="sc-heWoWI"]');
}
if (!closeBtn) {
// 3. Ищем по href="#"
debugLog('Поиск по селектору: button[href="#"]');
closeBtn = document.querySelector('button[href="#"]');
}
if (!closeBtn) {
// 4. Ищем по img с close_icon.png
debugLog('Поиск по селектору: button img[src*="close_icon.png"]');
const closeImg = document.querySelector('button img[src*="close_icon.png"]');
if (closeImg) {
closeBtn = closeImg.closest('button');
}
}
if (!closeBtn) {
// 5. Ищем во всех окнах с видео
debugLog('Поиск в окнах с видео');
const videos = document.querySelectorAll('img[src*="/getVideo/"]');
for (let video of videos) {
let parent = video.closest('div[style*="fixed"], div[style*="absolute"], div[class*="sc-"]');
if (parent) {
const btn = parent.querySelector('button.sc-heWoWI, button[href="#"], button img[src*="close_icon.png"]')?.closest('button');
if (btn) {
closeBtn = btn;
debugLog('✅ Найдена кнопка в окне с видео');
break;
}
}
}
}
if (closeBtn) {
debugLog('✅ КНОПКА ЗАКРЫТИЯ НАЙДЕНА!');
debugLog(' Текст:', closeBtn.textContent?.trim() || 'нет текста');
debugLog(' Класс:', closeBtn.className);
debugLog(' Href:', closeBtn.getAttribute('href'));
debugLog(' HTML:', closeBtn.outerHTML.substring(0, 100));
debugLog(' Видима:', isElementVisible(closeBtn));
// Проверяем наличие img внутри
const img = closeBtn.querySelector('img');
if (img) {
debugLog(' Изображение:', {
src: img.src,
alt: img.alt
});
}
} else {
debugLog('❌ КНОПКА ЗАКРЫТИЯ НЕ НАЙДЕНА');
// Выводим все кнопки для отладки
debugLog('Все кнопки на странице:');
document.querySelectorAll('button').forEach((btn, i) => {
debugLog(` Кнопка ${i}:`, {
class: btn.className,
href: btn.getAttribute('href'),
hasImg: !!btn.querySelector('img'),
text: btn.textContent?.trim() || 'нет текста'
});
});
}
debugLog('========== ПОИСК ЗАВЕРШЕН ==========');
return closeBtn;
}
// Функция для поиска кнопки "Закрыть карточку"
function findCloseCardButton() {
debugLog('========== ПОИСК КНОПКИ "ЗАКРЫТЬ КАРТОЧКУ" ==========');
const allButtons = document.querySelectorAll('button');
for (let button of allButtons) {
const text = button.textContent?.toLowerCase().trim() || '';
if (text.includes('закрыть карточку')) {
debugLog('✅ КНОПКА "ЗАКРЫТЬ КАРТОЧКУ" НАЙДЕНА!');
debugLog(' Текст:', button.textContent?.trim());
debugLog(' Класс:', button.className);
debugLog(' Href:', button.getAttribute('href'));
return button;
}
if (text.includes('отменить')) {
debugLog('✅ КНОПКА "отменить" НАЙДЕНА!');
debugLog(' Текст:', button.textContent?.trim());
debugLog(' Класс:', button.className);
debugLog(' Href:', button.getAttribute('href'));
return button;
}
}
debugLog('❌ Кнопка "Закрыть карточку" не найдена');
return null;
}
function setupEscapeHandler() {
debugLog('========== НАСТРОЙКА ОБРАБОТЧИКА ESCAPE ==========');
document.addEventListener('keydown', function(e) {
if (e.key === 'Escape') {
debugLog('🔴🔴🔴 ESCAPE НАЖАТ! 🔴🔴🔴');
e.preventDefault();
e.stopPropagation();
// 1. Сначала ищем кнопку "Закрыть карточку"
debugLog('--- ЭТАП 1: Поиск "Закрыть карточку" ---');
const closeCardBtn = findCloseCardButton();
if (closeCardBtn && isElementVisible(closeCardBtn)) {
debugLog('🟢 НАЖИМАЕМ "ЗАКРЫТЬ КАРТОЧКУ" 🟢');
closeCardBtn.click();
debugLog('✅ Клик выполнен');
setTimeout(() => {
if (core.VideoWindow) {
core.VideoWindow.removeButtonsFromWindow();
}
}, 100);
return;
}
// 2. Если нет, ищем кнопку закрытия видео
debugLog('--- ЭТАП 2: Поиск кнопки закрытия видео ---');
const videoCloseBtn = findVideoCloseButton();
if (videoCloseBtn && isElementVisible(videoCloseBtn)) {
debugLog('🟢 НАЖИМАЕМ КНОПКУ ЗАКРЫТИЯ ВИДЕО 🟢');
videoCloseBtn.click();
debugLog('✅ Клик выполнен');
setTimeout(() => {
if (core.VideoWindow) {
core.VideoWindow.removeButtonsFromWindow();
}
}, 100);
return;
}
debugLog('❌ НЕ УДАЛОСЬ НАЙТИ КНОПКУ ДЛЯ ЗАКРЫТИЯ');
}
});
debugLog('✅ Обработчик Escape установлен');
}
// Функция для принудительного добавления обработчика на кнопку
function setupDirectCloseHandler() {
debugLog('Настройка прямого обработчика на кнопку закрытия');
const closeBtn = findVideoCloseButton();
if (closeBtn) {
// Сохраняем оригинальный обработчик
const originalClick = closeBtn.onclick;
// Добавляем свой обработчик
closeBtn.addEventListener('click', function() {
debugLog('Кнопка закрытия нажата!');
setTimeout(() => {
if (core.VideoWindow) {
core.VideoWindow.removeButtonsFromWindow();
}
}, 100);
});
debugLog('✅ Прямой обработчик добавлен');
}
}
// Функция для отладки - подсветка всех кнопок закрытия на странице
function colorAllCloseButtons() {
debugLog('🎨 Подсветка всех кнопок закрытия');
const selectors = [
'button.sc-heWoWI',
'button[class*="sc-heWoWI"]',
'button[href="#"]',
'button img[src*="close_icon.png"]'
];
let count = 0;
selectors.forEach(selector => {
const buttons = document.querySelectorAll(selector);
buttons.forEach(btn => {
const button = btn.tagName === 'BUTTON' ? btn : btn.closest('button');
if (button && !button.dataset.colored) {
button.style.border = '3px solid red';
button.style.boxShadow = '0 0 10px red';
button.dataset.colored = 'true';
count++;
debugLog(` Кнопка #${count}:`, button.className || 'без класса');
}
});
});
debugLog(`✅ Подсвечено кнопок: ${count}`);
return count;
}
window.VideoIPRedirect.CloseHandler = {
setupEscapeHandler: setupEscapeHandler,
setupDirectCloseHandler: setupDirectCloseHandler,
findVideoCloseButton: findVideoCloseButton,
findCloseCardButton: findCloseCardButton,
colorAllCloseButtons: colorAllCloseButtons,
isElementVisible: isElementVisible
};
debugLog('✅ Модуль загружен');
})();