电脑端页面

将移动网址转换为WWW网址,如果失败则恢复原始网址。

目前为 2025-04-08 提交的版本。查看 最新版本

// ==UserScript==
// @name         电脑端页面
// @version      1.0
// @namespace    https://greasyfork.org/users/1171320
// @description  将移动网址转换为WWW网址,如果失败则恢复原始网址。
// @author       yzcjd
// @author2     Lama AI 辅助
// @match        *://m.*/*
// @run-at       document-start
// @grant        none
// @license MIT
// ==/UserScript==

(function () {
    'use strict';

    const currentUrl = location.href;
    const hostname = location.hostname;

    // 检查是否以 m. 开头
    if (!hostname.startsWith('m.')) return;

    // 替换 m. 为 www. 或去掉 m.
    const pcHostname = 'www.' + hostname.slice(2); // e.g., m.qidian.com -> www.qidian.com
    const pcUrl = currentUrl.replace(hostname, pcHostname);

    // 创建一个隐藏的 iframe 用来测试目标页面是否可访问(避免真正跳转)
    const testFrame = document.createElement('iframe');
    testFrame.style.display = 'none';
    testFrame.src = pcUrl;

    let loaded = false;

    // 设置超时,避免页面卡死
    const timeout = setTimeout(() => {
        if (!loaded) {
            console.warn('[PC跳转] 超时,保留当前移动页面');
            testFrame.remove();
        }
    }, 3000);

    // 监听 iframe 加载成功事件
    testFrame.onload = () => {
        loaded = true;
        clearTimeout(timeout);
        testFrame.remove();
        // 页面可用,跳转
        window.location.href = pcUrl;
    };

    // 添加到页面开始测试
    document.body.appendChild(testFrame);
})();