Greasy Fork

Greasy Fork is available in English.

125论坛手机版优化

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

当前为 2024-12-25 提交的版本,查看 最新版本

// ==UserScript==
// @name         125论坛手机版优化
// @namespace    http://tampermonkey.net/
// @version      3.1
// @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, // iPhone 14 Pro的设备像素比
        viewport: {
            width: 430,
            height: 932,
            deviceScaleFactor: 3,
            mobile: true,
            hasTouch: true
        }
    };

    // 模拟设备特性
    function emulateDevice() {
        // 修改User-Agent
        Object.defineProperty(navigator, 'userAgent', {
            get: function() { return DEVICE_CONFIG.userAgent; }
        });

        // 修改设备属性
        const screenProps = {
            width: { value: DEVICE_CONFIG.width },
            height: { value: DEVICE_CONFIG.height },
            availWidth: { value: DEVICE_CONFIG.width },
            availHeight: { value: DEVICE_CONFIG.height },
            devicePixelRatio: { value: DEVICE_CONFIG.deviceScaleFactor }
        };

        Object.defineProperties(window.screen, screenProps);
        Object.defineProperties(window, {
            devicePixelRatio: { value: DEVICE_CONFIG.deviceScaleFactor },
            innerWidth: { value: DEVICE_CONFIG.width },
            innerHeight: { value: DEVICE_CONFIG.height },
            outerWidth: { value: DEVICE_CONFIG.width },
            outerHeight: { value: DEVICE_CONFIG.height }
        });
    }

    // 设置viewport
    function setupViewport() {
        const viewport = document.createElement('meta');
        viewport.name = 'viewport';
        viewport.content = `width=${DEVICE_CONFIG.viewport.width}, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no, viewport-fit=cover`;
        
        // 移除已存在的viewport
        document.querySelectorAll('meta[name="viewport"]').forEach(el => el.remove());
        document.head.appendChild(viewport);
    }

    // 初始化
    function init() {
        emulateDevice();
        setupViewport();
    }

    // 在页面加载最开始就执行
    if(document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }

    // 监听viewport变化
    const observer = new MutationObserver((mutations) => {
        mutations.forEach((mutation) => {
            if(mutation.type === 'childList') {
                setupViewport();
            }
        });
    });

    observer.observe(document.head, {
        childList: true,
        subtree: true
    });

})();