// ==UserScript== // @name 125论坛手机版优化 // @namespace http://tampermonkey.net/ // @version 1.5 // @description 优化125论坛(bbs.125.la)在手机端的显示效果 // @author Your name // @match https://bbs.125.la/* // @grant none // @downloadURL none // ==/UserScript== (function() { 'use strict'; // 检查是否为移动端访问 if(!/mobile/i.test(navigator.userAgent)) return; // 修复视口设置 function fixViewport() { // 移除所有已存在的viewport meta标签 document.querySelectorAll('meta[name="viewport"]').forEach(el => el.remove()); // 添加新的viewport设置 const viewport = document.createElement('meta'); viewport.name = 'viewport'; viewport.content = 'width=device-width, initial-scale=1, maximum-scale=2, user-scalable=yes'; document.head.appendChild(viewport); } // 重置基础布局单位 function setRootFontSize() { const baseFontSize = Math.min(screen.width * 0.04, 16); document.documentElement.style.fontSize = baseFontSize + 'px'; } // 移除可能影响布局的样式 function cleanupStyles() { const stylesToRemove = [ 'position: fixed', 'position: absolute', 'float', 'transform', 'zoom' ]; const styleSheets = Array.from(document.styleSheets); styleSheets.forEach(sheet => { try { const rules = Array.from(sheet.cssRules || sheet.rules); rules.forEach(rule => { if(rule.style) { stylesToRemove.forEach(style => { if(rule.style.cssText.includes(style)) { rule.style.cssText = rule.style.cssText.replace(new RegExp(style + '[^;]+;', 'g'), ''); } }); } }); } catch(e) { // 跨域样式表会抛出错误,忽略即可 } }); } // 初始化 function init() { fixViewport(); setRootFontSize(); cleanupStyles(); // 监听屏幕旋转 window.addEventListener('orientationchange', setRootFontSize); } // 添加自定义样式 const customCSS = ` /* 重置基础样式 */ * { box-sizing: border-box; margin: 0; padding: 0; } /* 使用rem作为基础单位 */ html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; } /* 基础布局容器 */ body { width: 100%; max-width: 100vw; overflow-x: hidden; line-height: 1.5; } /* 使用Grid布局重构页面结构 */ .container { display: grid; grid-template-columns: 1fr; gap: 1rem; width: 100%; padding: 1rem; } /* 列表项使用Flexbox布局 */ .sub_forum li { display: flex; flex-direction: column; gap: 0.5rem; padding: 1rem; border-bottom: 1px solid rgba(0,0,0,0.1); } /* 标题和信息使用Grid布局 */ .forum_a { display: grid; grid-template-columns: 1fr auto; gap: 1rem; align-items: start; text-decoration: none; color: inherit; } /* 文字溢出处理 */ .thread_tit, h3 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; grid-column: 1 / -1; } /* 统计信息使用Flexbox布局 */ .f_count { display: flex; gap: 0.5rem; justify-content: flex-end; align-items: center; font-size: 0.875rem; color: #666; } `; // 插入样式 const style = document.createElement('style'); style.textContent = customCSS; document.head.appendChild(style); // 在DOM加载完成后初始化 if(document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { init(); } })();