Weibo Mobile Redirect

Auto redirect Weibo to mobile version

目前為 2025-05-29 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name              Weibo Mobile Redirect
// @name:zh-cn        新浪微博移动版跳转
// @namespace         https://github.com/Vinfall/UserScripts
// @version           1.3.1
// @author            Vinfall
// @match             https://video.weibo.com/show?fid=*
// @match             https://weibo.com/*/*
// @match             https://weibo.com/ttarticle/p/show?id=*
// @match             https://weibo.com/u/*
// @match             https://www.weibo.com/detail/*
// @exclude-match     https://card.weibo.com/article/*
// @exclude-match     https://m.weibo.cn/detail/*
// @exclude-match     https://m.weibo.cn/status/*
// @exclude-match     https://m.weibo.cn/u/*
// @exclude-match     https://passport.weibo.com/*
// @exclude-match     https://weibo.com/signup/*
// @exclude-match     https://weibo.com/tv/show/*
// @grant             none
// @run-at            document-start
// @license           CC0 1.0 Universal (Public Domain)
// @icon              https://m.weibo.cn/favicon.ico
// @description       Auto redirect Weibo to mobile version
// @description:zh-cn 新浪微博自动跳转移动版,支持微博、文章、视频
// ==/UserScript==

(() => {
    const currentUrl = window.location.href;

    // Defend in depth
    if (currentUrl.includes('card.weibo.com') || currentUrl.includes('m.weibo.cn')) {
        return;
    }

    // TODO: fix redirect to passport.weibo.com when nojs is OFF
    // function delayedRedirect(url, delay = 500) {
    //     setTimeout(() => {
    //         window.location.replace(url);
    //     }, delay);
    // }

    const cases = [
        // weibo.com/u/*
        {
            pattern: /^https:\/\/weibo\.com\/u\/(\d+)/,
            handle: (match) => {
                const userId = match[1];
                const userMobileUrl = `https://m.weibo.cn/u/${userId}`;
                window.location.replace(userMobileUrl);
            },
        },
        // weibo.com/ttarticle/p/show?id=*
        {
            pattern: /^https:\/\/weibo\.com\/ttarticle\/p\/show\?id=(\d+)/,
            handle: (match) => {
                const articleId = match[1];
                const articleMobileUrl = `https://card.weibo.com/article/h5/s#cid=${articleId}`;
                window.location.replace(articleMobileUrl);
            },
        },
        // weibo.com/1234567890/1234567890123456
        // used to be weibo.com/detail/*
        {
            pattern: /^https:\/\/weibo\.com\/\d{10}\/(\d{16})/,
            handle: (match) => {
                const detailId = match[1];
                const detailMobileUrl = `https://m.weibo.cn/detail/${detailId}`;
                window.location.replace(detailMobileUrl);
            },
        },
        // weibo.com/1234567890/abcdEFG89
        {
            pattern: /^https:\/\/weibo\.com\/\d{10}\/(\w{9})/,
            handle: (match) => {
                const statusId = match[1];
                const statusMobileUrl = `https://m.weibo.cn/status/${statusId}`;
                window.location.replace(statusMobileUrl);
                // delayedRedirect(statusMobileUrl);
            },
        },
        // video.weibo.com/show?fid=1234:1234567890123456
        // always auto redirect, added just in case
        {
            pattern: /^https:\/\/video\.weibo\.com\/show\?fid=(\d{4}:\d+)/,
            handle: (match) => {
                const fid = match[1];
                const videoUrl = `https://weibo.com/tv/show/${fid}`;
                window.location.replace(videoUrl);
            },
        },
    ];

    for (const caseItem of cases) {
        const match = currentUrl.match(caseItem.pattern);
        if (match) {
            caseItem.handle(match);
            return;
        }
    }
})();