// ==UserScript== // @name 天邑路由器定时重启显示 // @namespace http://tampermonkey.net/ // @version 1.0 // @description 显示并管理天邑路由器的定时重启功能,型号TY-6202A // @author github@fyhbauyg // @match http://192.168.1.1/* // @match https://192.168.1.1/* // @grant none // @run-at document-start // ==/UserScript== (function() { 'use strict'; console.log('路由器定时重启管理器已加载'); // 1. 改进的单选按钮取消选择功能 function addRadioDeselect() { let clickRecords = new Map(); document.addEventListener('mousedown', function(e) { if (e.target.matches('input[name="datea"][type="radio"]')) { // 记录鼠标按下时的状态 const radio = e.target; clickRecords.set(radio, { wasChecked: radio.checked, timestamp: Date.now() }); } }); document.addEventListener('click', function(e) { if (e.target.matches('input[name="datea"][type="radio"]')) { const radio = e.target; const record = clickRecords.get(radio); if (record && record.wasChecked) { // 如果点击前已经是选中状态 e.preventDefault(); e.stopImmediatePropagation(); // 延迟一小段时间,确保其他事件处理完成 setTimeout(() => { radio.checked = false; // 手动触发变更事件 const event = new Event('change', { bubbles: true }); radio.dispatchEvent(event); // 如果需要,也可以触发页面可能监听的其他事件 radio.dispatchEvent(new Event('input', { bubbles: true })); }, 10); clickRecords.delete(radio); } } }); // 处理鼠标移出情况 document.addEventListener('mouseup', function(e) { if (!e.target.matches('input[name="datea"][type="radio"]')) { // 如果鼠标在别处松开,清理记录 clickRecords.clear(); } }); } // 2. 显示定时重启区域 function showRebootSettings() { const rebootDiv = document.getElementById('Rebootcyclediv'); if (rebootDiv && rebootDiv.style.display === 'none') { rebootDiv.style.display = 'block'; // 如果父元素也隐藏了,一并显示 let parent = rebootDiv.parentElement; while (parent && parent !== document.body) { if (parent.style.display === 'none') { parent.style.display = 'block'; } parent = parent.parentElement; } } } // 3. 应用当前配置 function applyCurrentConfig() { try { if (window.datas?.Rebootcycle?.[0]) { const config = datas.Rebootcycle[0]; console.log('读取到的配置:', config); // 启用开关 const enableCheckbox = document.getElementById('enablereboot'); if (enableCheckbox) { enableCheckbox.checked = config.Enable === "1"; console.log('设置启用状态:', enableCheckbox.checked); } // 时间设置 if (config.CycleTime) { const [hour, minute] = config.CycleTime.split(':'); const hourSelect = document.getElementById('starth'); const minuteSelect = document.getElementById('startm'); if (hourSelect) hourSelect.value = hour; if (minuteSelect) minuteSelect.value = minute; console.log('设置时间:', hour, minute); } // 日期设置 if (config.Days) { const days = parseInt(config.Days); console.log('配置的Days值:', config.Days, '转换为数字:', days); if (days > 0 && days <= 5) { const dayMap = { '1': 'Mon', '2': 'Tue', '3': 'Wed', '4': 'Thu', '5': 'Fri' }; const dayId = dayMap[config.Days]; console.log('对应的dayId:', dayId); if (dayId) { const dayRadio = document.getElementById(dayId); if (dayRadio) { dayRadio.checked = true; console.log('已选择日期:', dayId); } else { console.log('未找到元素:', dayId); } } } else if (days === 0) { console.log('未选择任何日期'); // 清除所有选择 document.querySelectorAll('input[name="datea"]').forEach(radio => { radio.checked = false; }); } } } else { console.log('未找到datas.Rebootcycle配置'); } } catch (e) { console.error('配置读取失败:', e); } } // 4. 保存提示 function addSaveNotifications() { // 定时重启保存 const rebootSaveBtn = document.getElementById('submitreboot'); if (rebootSaveBtn && !rebootSaveBtn.hasAttribute('data-monitored')) { rebootSaveBtn.setAttribute('data-monitored', 'true'); // 保存原始点击处理 const originalClick = rebootSaveBtn.onclick; rebootSaveBtn.addEventListener('click', function(e) { console.log('保存定时重启设置'); setTimeout(() => showNotify('定时重启设置已保存'), 800); // 执行原始处理 if (originalClick && typeof originalClick === 'function') { originalClick.call(this, e); } }, true); // 使用捕获阶段确保先执行 } // 日志保存 const logSaveBtn = document.getElementById('save_log'); if (logSaveBtn && !logSaveBtn.hasAttribute('data-monitored')) { logSaveBtn.setAttribute('data-monitored', 'true'); logSaveBtn.addEventListener('click', () => { setTimeout(() => showNotify('日志设置已保存'), 800); }, true); } } // 5. 显示通知 function showNotify(message) { // 移除旧的通知 const oldNotify = document.querySelector('.tm-notify'); if (oldNotify) oldNotify.remove(); const notify = document.createElement('div'); notify.className = 'tm-notify'; notify.innerHTML = `
${message}
`; document.body.appendChild(notify); setTimeout(() => { if (notify.parentNode) { notify.querySelector('div').style.animation = 'tmSlideOut 0.3s ease-out forwards'; setTimeout(() => notify.parentNode.removeChild(notify), 300); } }, 3000); } // 6. 主初始化 function init() { // 添加CSS动画 if (!document.getElementById('tm-notify-styles')) { const style = document.createElement('style'); style.id = 'tm-notify-styles'; style.textContent = ` @keyframes tmSlideIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } } @keyframes tmSlideOut { from { transform: translateX(0); opacity: 1; } to { transform: translateX(100%); opacity: 0; } } `; document.head.appendChild(style); } console.log('开始初始化脚本'); // 延迟执行确保页面加载 const initInterval = setInterval(() => { const rebootDiv = document.getElementById('Rebootcyclediv'); const dateRadios = document.querySelectorAll('input[name="datea"]'); if (rebootDiv && dateRadios.length > 0) { console.log('页面元素已加载,执行初始化'); clearInterval(initInterval); showRebootSettings(); addRadioDeselect(); applyCurrentConfig(); addSaveNotifications(); // 监听页面变化,重新应用配置 const observer = new MutationObserver(() => { showRebootSettings(); addSaveNotifications(); }); observer.observe(document.body, { childList: true, subtree: true }); } else { console.log('等待页面元素加载...'); } }, 500); // 设置超时 setTimeout(() => { if (document.getElementById('Rebootcyclediv')) { console.log('超时,强制初始化'); showRebootSettings(); addRadioDeselect(); applyCurrentConfig(); addSaveNotifications(); } }, 10000); } // 启动 if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { init(); } })();