Redirect m. to www. (with Weibo Fix)

Redirects mobile (m.) sites to their desktop (www.) versions, with a special rule for Weibo detail pages.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Redirect m. to www. (with Weibo Fix)
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  Redirects mobile (m.) sites to their desktop (www.) versions, with a special rule for Weibo detail pages.
// @author       Continy
// @match        http://*/*
// @match        https://*/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const host = window.location.host;
    const path = window.location.pathname;

    // --- --- --- Rule 1: Special Case for Weibo Detail Pages --- --- ---
    // First, check for the most specific case.
    if (host === 'm.weibo.cn') {
        console.log('[Redirect Script] Weibo detail page detected. Applying special rule.');

        const pageHtml = document.documentElement.innerHTML;
        const idRegex = /"id": (\d+),/;
        const bidRegex = /"bid": "([^"]+)"/;

        const idMatch = pageHtml.match(idRegex);
        const bidMatch = pageHtml.match(bidRegex);

        if (idMatch && idMatch[1] && bidMatch && bidMatch[1]) {
            const pc_id = idMatch[1];
            const pc_bid = bidMatch[1];
            const pc_url = `https://weibo.com/${pc_id}/${pc_bid}`;

            console.log(`[Redirect Script] Found User ID: ${pc_id}, Post BID: ${pc_bid}. Redirecting to: ${pc_url}`);
            window.location.replace(pc_url); // Use replace to avoid back-button loops
        } else {
            console.error('[Redirect Script] Failed to extract Weibo IDs. The page structure may have changed.');
        }

    // --- --- --- Rule 2: Generic Case for all other "m." sites --- --- ---
    // If the Weibo rule didn't match, check for any other "m." host.
    } else if (host.startsWith("m.")) {
        console.log(`[Redirect Script] Generic mobile site detected (${host}). Applying standard redirect.`);

        const newUrl = `${window.location.protocol}//www.${host.substring(2)}${path}${window.location.search}${window.location.hash}`;

        // Note: This is the simple redirect from your original script.
        // It does not check if the destination page exists.
        window.location.replace(newUrl);
    }
})();