// ==UserScript== // @name 125论坛手机版优化 // @namespace http://tampermonkey.net/ // @version 3.3 // @description 通过模拟移动设备来优化125论坛移动端显示 // @author Your name // @match https://bbs.125.la/* // @run-at document-start // @downloadURL none // ==/UserScript== (function() { 'use strict'; // 模拟720P设备配置 const DEVICE_CONFIG = { userAgent: 'Mozilla/5.0 (Linux; Android 12; SM-A525F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.98 Mobile Safari/537.36', width: 720, height: 1280, deviceScaleFactor: 2 // 对于720p设备,2的设备像素比更合适 }; // 防抖函数 function debounce(func, wait) { let timeout; return function() { const context = this; const args = arguments; clearTimeout(timeout); timeout = setTimeout(() => func.apply(context, args), wait); }; } // 模拟设备特性 function emulateDevice() { // 只在第一次执行时修改User-Agent if (!window._userAgentModified) { Object.defineProperty(navigator, 'userAgent', { get: function() { return DEVICE_CONFIG.userAgent; } }); window._userAgentModified = true; } // 修改设备属性 try { Object.defineProperties(window.screen, { width: { value: DEVICE_CONFIG.width }, height: { value: DEVICE_CONFIG.height }, availWidth: { value: DEVICE_CONFIG.width }, availHeight: { value: DEVICE_CONFIG.height } }); } catch(e) { console.log('Screen properties already defined'); } } // 设置viewport function setupViewport() { const existingViewport = document.querySelector('meta[name="viewport"]'); const viewportContent = `width=${DEVICE_CONFIG.width}, initial-scale=1, user-scalable=yes`; if (existingViewport) { if (existingViewport.content !== viewportContent) { existingViewport.content = viewportContent; } } else { const viewport = document.createElement('meta'); viewport.name = 'viewport'; viewport.content = viewportContent; document.head.appendChild(viewport); } } // 初始化函数 const init = debounce(function() { emulateDevice(); setupViewport(); }, 100); // 在页面加载最开始就执行 if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { init(); } // 使用防抖处理viewport变化 const debouncedSetupViewport = debounce(setupViewport, 100); // 监听viewport变化,使用防抖 const observer = new MutationObserver((mutations) => { let needsUpdate = false; mutations.forEach((mutation) => { if (mutation.type === 'childList') { needsUpdate = true; } }); if (needsUpdate) { debouncedSetupViewport(); } }); // 延迟启动观察器 setTimeout(() => { observer.observe(document.head, { childList: true, subtree: true }); }, 1000); // 清理函数 window.addEventListener('unload', () => { observer.disconnect(); }); })();