Greasy Fork

125论坛手机版优化

通过模拟iPhone设备来优化125论坛移动端显示

目前为 2024-12-25 提交的版本。查看 最新版本

// ==UserScript==
// @name         125论坛手机版优化
// @namespace    http://tampermonkey.net/
// @version      3.2
// @description  通过模拟iPhone设备来优化125论坛移动端显示
// @author       Your name
// @match        https://bbs.125.la/*
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    // 模拟iPhone 14 Pro设备配置
    const DEVICE_CONFIG = {
        userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Mobile/15E148 Safari/604.1',
        width: 430,
        height: 932,
        deviceScaleFactor: 3
    };

    // 防抖函数
    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();
    });

})();