MSDN切换中英文按钮

在MSDN文档的左上角加一个快速切换中英文的按钮

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         MSDN切换中英文按钮
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  在MSDN文档的左上角加一个快速切换中英文的按钮
// @author       You
// @match        https://docs.microsoft.com/*
// @icon         https://www.google.com/s2/favicons?domain=microsoft.com
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    var href = window.location.href;
    var div = document.createElement('div');
    var button = document.createElement('button');


    if(href.includes('/en-us/')){
        button.innerText = '中文';
        button.onclick = () => {
            var url = window.location.href;
            var zh = url.replace("/en-us/", "/zh-cn/");
            window.location.href = zh;
        };
    }
    else{
        button.onclick = () => {
            var url = window.location.href;
            var zh = url.replace(/.com\/[\w\-]+\//, ".com/en-us/");
            window.location.href = zh;
        };
        button.innerText = '英文';
    }


    div.style.position = 'fixed';
    div.style.top = '0';
    div.style.left = '0';

    div.appendChild(button);
    document.body.appendChild(div);
    // Your code here...
})();