Skip to content

Instantly share code, notes, and snippets.

@darkthread
Created July 7, 2023 13:01
Show Gist options
  • Save darkthread/149c2d7a12fec938ab1bb09e480d19fb to your computer and use it in GitHub Desktop.
Save darkthread/149c2d7a12fec938ab1bb09e480d19fb to your computer and use it in GitHub Desktop.
MS Learn 文件快速中英切換
// ==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);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment