Skip to content

Commit 22a50db

Browse files
authored
Merge pull request #1 from ychikazawa/Add-extension
Add extension
2 parents 829c447 + 721d4fd commit 22a50db

File tree

6 files changed

+79
-0
lines changed

6 files changed

+79
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
11
# chromium-extension-msdocs-easy-switch-lang
22
Easy switch lang button on msdocs for Chromium (Chrome and Edge).
3+
4+
## Description
5+
Are you an avid reader of Microsoft Docs eager to get it in English as well as in your mother tongue version?
6+
<br>But changing URLs manually is a bit tedious, no?
7+
8+
With this extension you can switch between your language and English in one click!
9+
<br>From the upper right Extensions button, display "MSDocs Easy Switch Lang" and click on it with [Microsoft Docs](https://docs.microsoft.com/en-us/) open.
10+
<br>This extension will work to switch between the UI language set in your browser and English.
11+
<br>If English is set as the UI language, your browser's list of allowed languages will appear in order.
12+
13+
14+
## Example
15+
With the English document displayed, click the button:
16+
![2022-06-01_16h54_32](https://user-images.githubusercontent.com/12545287/171356671-b4c7fc06-9b0d-4a24-a0ea-520175522351.png)
17+
18+
It will then switch to the Japanese document:
19+
![2022-06-01_16h55_51](https://user-images.githubusercontent.com/12545287/171356892-7bb0242f-91ce-492f-bea0-9462a4f3ff5e.png)

icons/128.png

8.14 KB
Loading

icons/16.png

769 Bytes
Loading

icons/48.png

2.6 KB
Loading

manifest.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"manifest_version": 3,
3+
"name": "MSDocs Easy Switch Lang",
4+
"version": "1.0",
5+
"description": "Easy switch lang button on msdocs for Chromium (Chrome and Edge).",
6+
"icons": {
7+
"128": "icons/128.png",
8+
"48": "icons/48.png",
9+
"16": "icons/16.png"
10+
},
11+
"action": {
12+
},
13+
"permissions": [
14+
"activeTab",
15+
"tabs"
16+
],
17+
"host_permissions": [
18+
"https://docs.microsoft.com/*"
19+
],
20+
"background": {
21+
"service_worker": "scripts/background.js"
22+
}
23+
}

scripts/background.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var langIndex = 0;
2+
3+
/***
4+
* Switch the language of the page
5+
*/
6+
chrome.action.onClicked.addListener(function(tab) {
7+
if (tab.url.includes("https://docs.microsoft.com/")) {
8+
var currentLang = tab.url.split("/")[3]; // ja-jp, en-us
9+
var uiLang = chrome.i18n.getUILanguage(); // ja, en-us
10+
11+
var newLang = "";
12+
if (uiLang == "en-US") {
13+
// If the current language is English, switch to AcceptLanguages in order.
14+
chrome.i18n.getAcceptLanguages(function(langs) {
15+
langIndex = langIndex+1 < langs.length ? langIndex+1 : 0;
16+
newLang = langs[langIndex];
17+
console.log(langIndex + ": " + newLang);
18+
19+
switchToNewLang(tab, currentLang, newLang);
20+
});
21+
} else {
22+
// If the current language is not English, switch to UILanguage of browser.
23+
newLang = currentLang.includes(uiLang) ? "en-us" : uiLang;
24+
switchToNewLang(tab, currentLang, newLang);
25+
}
26+
}
27+
});
28+
29+
/***
30+
* Load new page with new language
31+
*/
32+
function switchToNewLang(tab, currentLang, newLang) {
33+
console.log("Switching language from " + currentLang + " to " + newLang);
34+
var newUrl = tab.url.replace(currentLang, newLang);
35+
36+
chrome.tabs.update(tab.id, {
37+
url: newUrl
38+
});
39+
}

0 commit comments

Comments
 (0)