// ==UserScript== // @name 繁簡自由切換 // @name:zh-CN 简繁自由切换 // @name:ja 简繁 // @name:en Switch Traditional Chinese and Simplified Chinese // @namespace hoothin // @version 1.1.2 // @description 任意轉換網頁中的簡體中文與繁體中文(默認簡體→繁體) // @description:zh-CN 任意转换网页中的简体中文与繁体中文(默认繁体→简体) // @description:ja 简繁中国語に変換 // @description:en Just Switch Traditional Chinese and Simplified Chinese // @author hoothin // @include * // @grant GM_setValue // @grant GM_getValue // @grant GM_registerMenuCommand // @contributionURL https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=rixixi@sina.com&item_name=Greasy+Fork+donation // @contributionAmount 1 // @downloadURL none // ==/UserScript== (function() { 'use strict'; var auto=true;//是否自動切換至OS使用的默認語言 var shortcutKey=119;//快捷鍵keyCode var lang = navigator.appName=="Netscape"?navigator.language:navigator.userLanguage; lang = lang.toLowerCase(); var isSimple = (lang === "zh-cn" || lang === "zh-hans" || lang === "zh-sg" || lang === "zh-my"); var action = 0;//1:noChange, 2:showSimplified, 3:showTraditional function stranText(txt){ if(!txt)return ""; if(action == 2){return simplized(txt);} else if(action == 3){return traditionalized(txt);} } function stranBody(pNode){ var childs; if(pNode){ childs=pNode.childNodes; }else{ childs=document.documentElement.childNodes; } if(childs) for(var i=0;i 10000){ index=scStr.indexOf(char); if(index != -1)str+=tcStr.charAt(index); else str+=char; } else str+=char; } return str; } function simplized(orgStr){ var str='', index, char; for(var i=0;i 10000){ index=tcStr.indexOf(char); if(index != -1)str+=scStr.charAt(index); else str+=char; } else str+=char; } return str; } function setLanguage(){ GM_setValue("action_" + location.hostname.toString().replace(/\./g,"_"), action); switch(action){ case 1: alert("已於該網域禁用簡繁切換"); location.reload(); break; case 2: alert("已切换至简体中文"); break; case 3: alert("已切換至繁體中文"); break; } if(action > 1){ stranBody(); } } function switchLanguage(){ if(isSimple){ action--; action=action<1?3:action; }else{ action++; action=action>3?1:action; } setLanguage(); } var saveAction=GM_getValue("action_" + location.hostname.toString().replace(/\./g,"_")); action=saveAction?saveAction:(isSimple?2:3); if(auto && action > 1){ setTimeout(function(){ stranBody(); var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver; var observer = new MutationObserver(function(records){ records.map(function(record) { if(record.addedNodes){ [].forEach.call(record.addedNodes,function(item){ stranBody(item); }); } }); }); var option = { 'childList': true, 'subtree': true }; observer.observe(document.body, option); },50); } var curLang=isSimple; document.addEventListener("keydown", function(e) { if(e.keyCode == shortcutKey && e.ctrlKey) { if("TEXTAREA"==document.activeElement.tagName){ document.activeElement.innerHTML=curLang?traditionalized(document.activeElement.innerHTML):simplized(document.activeElement.innerHTML); curLang=!curLang; }else if("INPUT"==document.activeElement.tagName){ document.activeElement.value=curLang?traditionalized(document.activeElement.value):simplized(document.activeElement.value); curLang=!curLang; }else{ action=action==2?3:2; setLanguage(); } } }); GM_registerMenuCommand("繁簡切換【Ctrl+F8】", switchLanguage); })();