Skip to content

Instantly share code, notes, and snippets.

@darkthread
Created July 7, 2023 13:01

Revisions

  1. darkthread created this gist Jul 7, 2023.
    37 changes: 37 additions & 0 deletions ms-learn-lang-switch.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    // ==UserScript==
    // @name 本草綱目中英快速切換
    // @namespace https://blog.darkthread.net
    // @version 0.1
    // @description MS Learn 文件中英文切換
    // @author You
    // @match https://learn.microsoft.com/*/*
    // @icon https://www.google.com/s2/favicons?sz=64&domain=microsoft.com
    // @grant none
    // ==/UserScript==

    (function() {
    'use strict';
    const m = location.href.match(/com\/(en-us|zh-tw)\//);
    if (m) {
    const isEn = m[1] == 'en-us';
    const style = document.createElement('style');
    style.innerHTML = `
    #lang-switch {
    position:fixed;top:6px;right:6px;z-index:999;opacity:0.2;cursor:pointer;
    padding:2px 6px;background-color:cadetblue;color:white;font-size:11pt;
    }
    #lang-switch:hover {
    opacity: 1;
    }
    `;
    document.head.appendChild(style);
    const div = document.createElement('div');
    div.innerText = isEn ? '中' : 'EN';
    div.id = 'lang-switch';
    div.onclick = function() {
    const toUrl = location.toString().replace(/com\/(en-us|zh-tw)\//, `com/${(isEn ? 'zh-tw' : 'en-us')}/`);
    location.href = toUrl;
    };
    document.body.appendChild(div);
    }
    })();