-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
50 lines (41 loc) · 1.39 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// ==UserScript==
// @name MDN 语言助手 MDN Language Helper
// @namespace https://github.com/moonflame/jump-to-chinese-page
// @version 0.4.0
// @description 检测当前浏览的 MDN 非中文页面,自动跳转到对应中文版页面。适用于 Tampermonkey Chrome 扩展
// @author Rayan Zhang
// @match *://developer.mozilla.org/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
let options = document.getElementById('select_language').children;
let targetUrl = '';
// won't continue when navigating into the history or using navigation buttons
if (isCertainNavigationType()) {
return;
}
if (isAlreadyPreferredLanguage()) {
return;
}
if (!processUrl()) {
return;
}
window.location.assign(targetUrl);
return;
function isCertainNavigationType() {
return window.performance.navigation.type == window.performance.navigation.TYPE_BACK_FORWARD;
}
function isAlreadyPreferredLanguage() {
return options[0].value.toLowerCase() === 'zh-CN'.toLowerCase();
}
function processUrl() {
for (let el of options) {
if (el.value.toLowerCase().includes('zh-CN'.toLowerCase()) === true) {
targetUrl = document.location.origin + el.value;
return true;
}
}
return false;
}
})();