Greasy Fork

Greasy Fork is available in English.

维基百科任何语言自动重定向中文

Redirect to zh.wikipedia.org with correct Chinese title from any language

当前为 2025-04-03 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         维基百科任何语言自动重定向中文
// @namespace    http://tampermonkey.net/
// @version      0.9
// @description  Redirect to zh.wikipedia.org with correct Chinese title from any language
// @match        *://*.wikipedia.org/wiki/*
// @grant        GM_xmlhttpRequest
// @connect      *.wikipedia.org
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    var url = window.location.href;
    if (!url.includes("zh.wikipedia.org")) {
        var langMatch = url.match(/\/\/([a-z]{2,3})\.wikipedia\.org/);
        if (!langMatch) {
            console.error("无法识别语言代码,URL:", url);
            return;
        }
        var currentLang = langMatch[1];
        var path = url.split('/wiki/')[1];
        if (!path) {
            console.error("无法提取标题,URL:", url);
            return;
        }
        var title = decodeURIComponent(path);

        GM_xmlhttpRequest({
            method: "GET",
            url: `https://${currentLang}.wikipedia.org/w/api.php?action=query&format=json&prop=langlinks&titles=${encodeURIComponent(title)}&lllang=zh`,
            onload: function(response) {
                try {
                    var data = JSON.parse(response.responseText);
                    var pages = data.query.pages;
                    var pageId = Object.keys(pages)[0];
                    if (pageId === "-1" || !pages[pageId].langlinks) {
                        console.log("未找到对应中文页面,标题:", title);
                        window.location.href = "https://zh.wikipedia.org/wiki/" + encodeURIComponent(title);
                        return;
                    }
                    var zhTitle = pages[pageId].langlinks[0]["*"];
                    console.log(`从 ${currentLang} 标题 "${title}" 找到中文标题: "${zhTitle}"`);
                    window.location.href = "https://zh.wikipedia.org/wiki/" + encodeURIComponent(zhTitle);
                } catch (e) {
                    console.error("API 解析失败:", e);
                    window.location.href = "https://zh.wikipedia.org/wiki/" + encodeURIComponent(title);
                }
            },
            onerror: function(error) {
                console.error("API 请求失败:", error);
                window.location.href = "https://zh.wikipedia.org/wiki/" + encodeURIComponent(title);
            }
        });
    }
})();