Greasy Fork

Greasy Fork is available in English.

微软文档语言切换

微软文档快捷语言切换按钮

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         微软文档语言切换
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  微软文档快捷语言切换按钮
// @author       iron2han
// @match        *://learn.microsoft.com/*
// @icon         https://www.google.com/s2/favicons?domain=tampermonkey.net
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    window.addEventListener('load', function () {
        let lang = getCurrentLang();

        if (lang == null) {
            return;
        }

        let xpathResult = document.evaluate('//*[@id="ms--secondary-nav"]//div[@class="buttons"]', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)

        if (xpathResult.snapshotLength == 0) {
            return;
        }

        let ele = xpathResult.snapshotItem(0);

        lang = lang.toLowerCase();

        if (lang != 'zh-cn') {
            ele.appendChild(createZHCNElement())
        }

        if (lang != 'en-us') {
            ele.appendChild(createENUSElement())
        }

    }, false);
})();

function createENUSElement() {
    let div = document.createElement('div');

    div.style = "margin-left: 5px";

    let url = replaceUrl('en-us');

    div.innerHTML = `<a data-test-id="navbar-primary-cta" class="button button-sm button-primary button-filled margin-right-none" href="${url}" data-bi-name="secondary-nav-cta-primary-download-dotnet">
切换到英文
</a>`;

    return div;
}


function createZHCNElement() {
    let div = document.createElement('div');

    div.style = "margin-left: 5px";

    let url = replaceUrl('zh-cn');

    div.innerHTML = `<a data-test-id="navbar-primary-cta" class="button button-sm button-primary button-filled margin-right-none" href="${url}" data-bi-name="secondary-nav-cta-primary-download-dotnet">
切换到中文
</a>`;

    return div;
}

function replaceUrl(lang) {
    let currentLang = getCurrentLang();

    if (currentLang == null) {
        return location.href;
    }

    return location.href.replace(currentLang, lang);
}

function getCurrentLang() {
    let match = this.location.href.match('com\/([a-zA-Z]{2,2}\-[a-zA-Z]{2,2})\/');

    if (match == null) {
        return null;
    }

    let lang = match[1];
    return lang;
}