Skip to content

Instantly share code, notes, and snippets.

@doggy8088
Created October 3, 2021 06:48
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save doggy8088/29e4fa49a49a9127edb2a64941ae616c to your computer and use it in GitHub Desktop.
中、英文網頁切換的瀏覽器書籤小工具 (Tampermonkey Userscript)
// ==UserScript==
// @name 中、英文網頁切換的瀏覽器書籤小工具
// @namespace https://blog.miniasp.com/
// @version 0.1
// @description 按下 Alt+s 就會自動將目前網頁切換至中文或英文版
// @license MIT
// @homepage https://blog.miniasp.com/
// @homepageURL https://blog.miniasp.com/
// @website https://www.facebook.com/will.fans
// @source https://github.com/miniasp/en-zh-bookmarklet
// @namespace https://github.com/miniasp/en-zh-bookmarklet
// @author Will Huang
// @match *://*/*
// ==/UserScript==
(function() {
'use strict';
document.addEventListener('keydown', (ev) => {
if (ev.altKey && ev.key === 's' && !/^(?:input|select|textarea|button)$/i.test(ev.target.nodeName)) {
(function () {
var idx,
hn = location.hostname.toLowerCase(),
ln = location.href,
pn = location.pathname;
if (ln === 'https://getbootstrap.com/' || ln === 'http://getbootstrap.com/') {
void(location.href = 'http://bootstrap.hexschool.com');
}
if (location.hostname == 'zh.wikipedia.org') {
document.querySelector("#p-lang > div > ul > li.interlanguage-link.interwiki-en > a").click();
}
if (location.hostname == 'en.wikipedia.org') {
document.querySelector("#p-lang > div > ul > li.interlanguage-link.interwiki-zh > a").click();
}
if (ln.indexOf('//getbootstrap.com/docs/3.3/') >= 0) {
void(location.href = ln.replace(/getbootstrap\.com\/docs\/3\.3\//i, 'v3.bootcss.com/'));
}
if (ln.indexOf('//v3.bootcss.com/') >= 0) {
void(location.href = ln.replace(/v3\.bootcss\.com\//i, 'getbootstrap.com/docs/3.3/'));
}
if (ln.indexOf('//bootstrap.hexschool.com/') >= 0) {
void(location.href = ln.replace(/bootstrap\.hexschool\.com/i, 'getbootstrap.com'));
}
if (ln.indexOf('//getbootstrap.com/docs/4.0/') >= 0) {
void(location.href = ln.replace(/http(s?)\:\/\/getbootstrap\.com/i, 'http://bootstrap.hexschool.com'));
}
if (ln.indexOf('//doc.rust-lang.org/stable/book/') >= 0) {
void(location.href = ln.replace(/\/\/doc\.rust\-lang\.org\/stable\/book\//i, '//rust-lang.tw/book-tw/'));
}
if (ln.indexOf('//rust-lang.tw/book-tw/') >= 0) {
void(location.href = ln.replace(/\/\/rust\-lang\.tw\/book\-tw\//i, '//doc.rust-lang.org/stable/book/'));
}
if (ln.indexOf('//www.autohotkey.com/docs/') >= 0) {
void(location.href = ln.replace(/\/\/www\.autohotkey\.com\/docs\//i, '//wyagd001.github.io/zh-cn/docs/'));
}
if (ln.indexOf('//wyagd001.github.io/zh-cn/docs/') >= 0) {
void(location.href = ln.replace(/\/\/wyagd001\.github\.io\/zh-cn\/docs\//i, '//www.autohotkey.com/docs/'));
}
if (ln.indexOf('//angular.io/') >= 0) {
void(location.href = ln.replace(/angular\.io/i, 'angular.tw'));
}
if (ln.indexOf('//angular.tw/') >= 0) {
void(location.href = ln.replace(/angular\.tw/i, 'angular.io'));
}
if (ln.indexOf('//material.angular.io/') >= 0) {
void(location.href = ln.replace(/material.angular\.io/i, 'material.angular.tw'));
}
if (ln.indexOf('//material.angular.tw/') >= 0) {
void(location.href = ln.replace(/material.angular\.tw/i, 'material.angular.io'));
}
if (ln.indexOf('//www.jquery123.com/') >= 0) {
void(location.href = ln.replace(/www\.jquery123\.com/i, 'api.jquery.com'));
}
if (ln.indexOf('//api.jquery.com/') >= 0) {
void(location.href = ln.replace(/http(s?)\:\/\/api\.jquery\.com/i, 'http://www.jquery123.com'));
}
if (ln.indexOf('//reactivex.io/rxjs/') >= 0) {
void(location.href = ln.replace(/reactivex\.io\/rxjs/i, 'cn.rx.js.org'));
}
if (ln.indexOf('//cn.rx.js.org/') >= 0) {
if (ln == 'https://cn.rx.js.org/') {
location.href = 'http://reactivex.io/rxjs/manual/index.html';
return;
}
void(location.href = ln.replace(/https:\/\/cn\.rx\.js\.org/i, 'http://reactivex.io/rxjs'));
}
if (ln.indexOf('//vuejs.org/') >= 0) {
void(location.href = ln.replace(/vuejs\.org/i, 'cn.vuejs.org'));
}
if (ln.indexOf('//cn.vuejs.org/') >= 0) {
void(location.href = ln.replace(/cn\.vuejs\.org/i, 'vuejs.org'));
}
if (hn.indexOf('google.com') >= 0) {
// https://developers.google.com/web/fundamentals/?hl=en-us
idx = location.search.indexOf('hl=');
if (idx == -1) {
if (location.search.indexOf('?') == 0) {
void(location.search += '&hl=en-us');
} else {
void(location.search += '?hl=en-us');
}
} else {
if (location.search.substr(idx + 3, 2) == 'en') {
void(location.search = location.search.replace(/hl=(\w\w)(-\w+)?/i, 'hl=zh-Hant'));
} else {
void(location.search = location.search.replace(/hl=(\w\w)(-\w+)?/i, 'hl=en-us'));
}
}
}
if (hn.indexOf('mozilla.org') >= 0) {
if (pn.search(/^\/(en)(-\w\w)?(\/)?(.*)/i) >= 0) {
void(location.pathname = pn.replace(/^\/en(-\w\w)?\//i, '/zh-tw/'));
} else {
void(location.pathname = pn.replace(/^\/\w\w(-\w\w)?\//i, '/en-us/'));
}
}
if (hn.indexOf('kubernetes.io') >= 0) {
if (pn.search(/^(?=\/zh\/).{4}/) >= 0) {
void(location.pathname = pn.replace(/^(?=\/zh\/).{4}/i, '/'));
} else {
void(location.pathname = pn.replace(/^((?!\/zh\/).{0,4})/i, '/zh$1'));
}
}
if (hn.indexOf('microsoft.com') >= 0 || hn.indexOf('office.com') >= 0 || hn.indexOf('visualstudio.com') >= 0) {
// http://support.microsoft.com/kb/2951262
idx = pn.search(/^\/kb\/\d+/i);
if (hn.indexOf('support.microsoft.com') >= 0 && idx >= 0) {
// http://support.microsoft.com/kb/2951262/
if (pn.search(/^\/kb\/\d+\/?$/i) >= 0) {
void(location.pathname = pn.replace(/^(\/kb\/\d+)(\/)?(\w\w-\w\w)?$/i, '$1/en-us'));
} else {
// http://support.microsoft.com/kb/2951262/en-us
if (pn.search(/^\/kb\/\d+\/(\w\w)(-\w\w)?/i) >= 0) {
if (pn.search(/^(\/kb\/\d+\/)en(-\w\w)?$/i) >= 0) {
// http://support.microsoft.com/kb/2951262/zh-tw
void(location.pathname = pn.replace(/^(\/kb\/\d+\/)en(-\w\w)?$/i, '$1zh-tw'));
} else {
// http://support.microsoft.com/kb/2951262/en-us
void(location.pathname = pn.replace(/^(\/kb\/\d+\/)\w\w(-\w\w)?$/i, '$1en-us'));
}
} else {
void(location.pathname = pn.replace(/^(\/kb\/\d+)(\/\w\w-\w\w)?$/i, '$1/en-us'));
}
}
} else {
// http://msdn.microsoft.com/ja-jp/library/system.drawing.color.aspx
if (pn.search(/^\/(\w\w)(-\w\w)?\/?/i) >= 0) {
if (pn.search(/^\/(en)(-\w\w)?(\/)?(.*)/i) >= 0) {
void(location.pathname = pn.replace(/^\/(en)(-\w\w)?(\/)?(.*)/i, '/zh-tw$3$4'));
} else {
void(location.pathname = pn.replace(/^\/(\w\w)(-\w\w)?(\/)?(.*)/i, '/en-us$3$4'));
}
}
}
}
}());
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment