Last active
April 6, 2020 06:58
-
-
Save dinowang/0b2cb0730680a359d09524bdbddd29f2 to your computer and use it in GitHub Desktop.
Microsoft docuemnts language (en-us/zh-tw/zh-cn) switcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Microsoft docuemnts language (en-us/zh-tw/zh-cn) switcher | |
// @namespace http://dinowang.net/ | |
// @version 0.1 | |
// @description | |
// @author Dino Wang | |
// @match https://*.microsoft.com/*/* | |
// @match https://*.azure.cn/*/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
(function () { | |
var langs = [ "zh-tw", "zh-cn", "en-us" ], | |
pathParts = location.pathname.split(/\//g), | |
currentLang = pathParts[1]; | |
var style = document.createElement('style'); | |
style.type = "text/css"; | |
style.innerHTML = ".msex-container { position: fixed; bottom: 0px; right: 0px; background-color: #333; text-align: center; padding: 0 8px; }" + | |
".msex-container .block { display: inline-block; }" + | |
".msex-container .block:not(:last-child):after { content: \"|\"; margin: 0 4px; color: #aaa; }" + | |
".msex-container .block a { font-size: 9pt; color: #aaa; }" + | |
".msex-container .block a:hover { font-size: 9pt; color: #ccc; }" + | |
".msex-container .block a:not(:last-child):after { content: \"|\"; margin: 0 4px; color: #777; }"; | |
document.getElementsByTagName('head')[0].appendChild(style); | |
if (currentLang && currentLang.match(/^(zh-tw|zh-cn|en-us)/)) { | |
var container = document.createElement("div"); | |
container.className = "msex-container"; | |
document.body.insertBefore(container, document.body.children[0]); | |
var block = document.createElement("div"); | |
block.className = "block"; | |
container.appendChild(block); | |
var defaultToggleButton = document.querySelector("#language-toggle"); | |
if (defaultToggleButton != null) { | |
var toggle = document.createElement("a"); | |
toggle.href = "#" | |
toggle.innerText = "language toggle"; | |
toggle.onclick = _ => defaultToggleButton.click(); | |
block.appendChild(toggle); | |
} | |
langs.forEach(lang => { | |
pathParts[1] = lang; | |
var a = document.createElement("a"); | |
a.href = currentLang == lang ? "#" : location.origin + pathParts.join("/"); | |
a.innerText = lang; | |
block.appendChild(a); | |
}); | |
var defaultThemeSelector = document.querySelector(".theme-selector"); | |
if (defaultThemeSelector != null) { | |
block = document.createElement("div"); | |
block.className = "block"; | |
container.appendChild(block); | |
defaultThemeSelector.querySelectorAll(".theme-control").forEach(selector => { | |
var a = document.createElement("a"); | |
a.href = "#"; | |
//a.innerText = selector.innerText.trim(); | |
a.innerText = selector.getAttribute("data-theme-to"); | |
a.onclick = _ => selector.click(); | |
block.appendChild(a); | |
}); | |
} | |
} | |
})(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment