Duolingo input language switcher

This script allows you to type letters appropriate for current task without changing keyboard's layout

当前为 2018-01-22 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Duolingo input language switcher
  3. // @namespace https://www.duolingo.com/IVrL9
  4. // @match https://www.duolingo.com/*
  5. // @match https://www.duolingo.com/skill/*
  6. // @match https://www.duolingo.com/practice/*
  7. // @match https://www.example.com/*
  8. // @match https://example.com/*
  9. // @version 1.0
  10. // @description This script allows you to type letters appropriate for current task without changing keyboard's layout
  11. // @run-at document-start
  12. // @grant none
  13. // ==/UserScript==/// Generated by Haxe 3.4.2 (git build master @ 890f8c7)
  14. (function () { "use strict";
  15. var HxOverrides = function() { };
  16. HxOverrides.__name__ = true;
  17. HxOverrides.substr = function(s,pos,len) {
  18. if(len == null) {
  19. len = s.length;
  20. } else if(len < 0) {
  21. if(pos == 0) {
  22. len = s.length + len;
  23. } else {
  24. return "";
  25. }
  26. }
  27. return s.substr(pos,len);
  28. };
  29. var Main = function() {
  30. this.ereg = new RegExp("duolingo\\.com/skill|practice/");
  31. this.isObserved = false;
  32. var _gthis = this;
  33. this.document = window.document;
  34. this.console = { };
  35. this.observer = new MutationObserver($bind(this,this.checkMutation));
  36. Object.assign(this.console,window.console);
  37. this.originalTrace = haxe_Log.trace;
  38. haxe_Log.trace = function(v,i) {
  39. _gthis.console.log("" + i.className + ":" + i.lineNumber + ":",v);
  40. };
  41. this.initLanguages();
  42. if(this.document.readyState == "interactive" || this.document.readyState == "complete") {
  43. this.onready();
  44. } else {
  45. this.document.addEventListener("DOMContentLoaded",$bind(this,this.onready));
  46. }
  47. };
  48. Main.__name__ = true;
  49. Main.main = function() {
  50. new Main();
  51. };
  52. Main.prototype = {
  53. initLanguages: function() {
  54. this.keyCodes = ["Backquote","Digit1","Digit2","Digit3","Digit4","Digit5","Digit6","Digit7","Digit8","Digit9","Digit0","Minus","Equal","Backslash","KeyQ","KeyW","KeyE","KeyR","KeyT","KeyY","KeyU","KeyI","KeyO","KeyP","BracketLeft","BracketRight","KeyA","KeyS","KeyD","KeyF","KeyG","KeyH","KeyJ","KeyK","KeyL","Semicolon","Quote","KeyZ","KeyX","KeyC","KeyV","KeyB","KeyN","KeyM","Comma","Period","Slash"];
  55. this.languages = { };
  56. this.languages.ru = "ё1234567890-=\\йцукенгшщзхъфывапролджэячсмитьбю.Ё!\"№;%:?*()_+/ЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ,";
  57. this.languages.en = "`1234567890-=\\qwertyuiop[]asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+|QWERTYUIOP{}ASDFGHJKL:\"ZXCVBNM<>?";
  58. var len = this.languages.ru.length;
  59. var _g = 0;
  60. var _g1 = Reflect.fields(this.languages);
  61. while(_g < _g1.length) {
  62. var f = _g1[_g];
  63. ++_g;
  64. var act = this.languages[f].length;
  65. if(act != len) {
  66. this.console.error("LangString test failed: expected len " + len + "; actual len " + act + "; lang name " + f);
  67. this.console.error(this.languages[f]);
  68. return;
  69. }
  70. if(act != this.keyCodes.length * 2) {
  71. this.console.error("KeyCodes and LangString test failed: expected lang string len " + this.keyCodes.length * 2 + "; actual len " + act + "; lang name " + f);
  72. return;
  73. }
  74. }
  75. }
  76. ,onready: function(e) {
  77. this.document.removeEventListener("DOMContentLoaded",$bind(this,this.onready));
  78. this.console.log("Duolingo input switcher inited");
  79. window.setInterval($bind(this,this.checkPage),1000);
  80. }
  81. ,checkPage: function() {
  82. if(this.ereg.test(window.location.href)) {
  83. if(!this.isObserved) {
  84. this.startObserver();
  85. }
  86. } else {
  87. this.isObserved = false;
  88. this.observer.disconnect();
  89. }
  90. }
  91. ,startObserver: function(e) {
  92. var selector = "._1zuqL";
  93. var obsTarget = window.document.querySelector(selector);
  94. if(obsTarget == null) {
  95. this.console.error("There is no Node with selector \"" + selector + "\" , so nothing to observe ");
  96. return;
  97. }
  98. this.observer.observe(obsTarget,{ childList : true, subtree : true, attributes : true});
  99. this.isObserved = true;
  100. }
  101. ,checkMutation: function(records,obs) {
  102. this.nativeLanguage = "ru";
  103. this.foreignLanguage = "en";
  104. var translationInput = window.document.querySelector("textarea[data-test=challenge-translate-input]");
  105. if(translationInput != null) {
  106. var lang = translationInput.getAttribute("lang");
  107. if(lang == this.nativeLanguage) {
  108. this.initInput(translationInput,"ru","en");
  109. } else if(lang == this.foreignLanguage) {
  110. this.initInput(translationInput,"en","ru");
  111. }
  112. return;
  113. }
  114. var listenInput = window.document.querySelector("textarea[data-test=challenge-listen-input]");
  115. if(listenInput != null) {
  116. this.initInput(listenInput,"en","ru");
  117. return;
  118. }
  119. var nameInput = window.document.querySelector("input[data-test=challenge-name-input]");
  120. if(nameInput != null) {
  121. this.initInput(nameInput,"en","ru");
  122. }
  123. }
  124. ,initInput: function(input,targetLanguage,sourceLanguage) {
  125. this.targetLanguage = targetLanguage;
  126. this.sourceLanguage = sourceLanguage;
  127. input.addEventListener("keypress",$bind(this,this.onInput));
  128. input.addEventListener("keydown",$bind(this,this.refocus));
  129. }
  130. ,refocus: function(e) {
  131. if(e.keyCode == 13 || e.code == "Enter") {
  132. e.currentTarget.blur();
  133. }
  134. }
  135. ,onInput: function(e) {
  136. var targetLangStr = this.languages[this.targetLanguage];
  137. var keyCodeInd = this.keyCodes.indexOf(e.code);
  138. if(keyCodeInd != -1) {
  139. var targetChar = e.shiftKey ? targetLangStr.charAt(keyCodeInd + this.keyCodes.length) : targetLangStr.charAt(keyCodeInd);
  140. var input = e.currentTarget;
  141. window.setTimeout($bind(this,this.replaceChar),1,input,targetChar,input.selectionStart);
  142. }
  143. }
  144. ,replaceChar: function(target,newChar,position) {
  145. var val = target.value;
  146. val = val.substring(0,position) + newChar + HxOverrides.substr(val,position + 1,null);
  147. target.innerText = val;
  148. target.value = val;
  149. target.setSelectionRange(position + 1,position + 1);
  150. }
  151. };
  152. Math.__name__ = true;
  153. var Reflect = function() { };
  154. Reflect.__name__ = true;
  155. Reflect.fields = function(o) {
  156. var a = [];
  157. if(o != null) {
  158. var hasOwnProperty = Object.prototype.hasOwnProperty;
  159. for( var f in o ) {
  160. if(f != "__id__" && f != "hx__closures__" && hasOwnProperty.call(o,f)) {
  161. a.push(f);
  162. }
  163. }
  164. }
  165. return a;
  166. };
  167. var haxe_Log = function() { };
  168. haxe_Log.__name__ = true;
  169. haxe_Log.trace = function(v,infos) {
  170. js_Boot.__trace(v,infos);
  171. };
  172. var js_Boot = function() { };
  173. js_Boot.__name__ = true;
  174. js_Boot.__unhtml = function(s) {
  175. return s.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
  176. };
  177. js_Boot.__trace = function(v,i) {
  178. var msg = i != null ? i.fileName + ":" + i.lineNumber + ": " : "";
  179. msg += js_Boot.__string_rec(v,"");
  180. if(i != null && i.customParams != null) {
  181. var _g = 0;
  182. var _g1 = i.customParams;
  183. while(_g < _g1.length) {
  184. var v1 = _g1[_g];
  185. ++_g;
  186. msg += "," + js_Boot.__string_rec(v1,"");
  187. }
  188. }
  189. var d;
  190. var tmp;
  191. if(typeof(document) != "undefined") {
  192. d = document.getElementById("haxe:trace");
  193. tmp = d != null;
  194. } else {
  195. tmp = false;
  196. }
  197. if(tmp) {
  198. d.innerHTML += js_Boot.__unhtml(msg) + "<br/>";
  199. } else if(typeof console != "undefined" && console.log != null) {
  200. console.log(msg);
  201. }
  202. };
  203. js_Boot.__string_rec = function(o,s) {
  204. if(o == null) {
  205. return "null";
  206. }
  207. if(s.length >= 5) {
  208. return "<...>";
  209. }
  210. var t = typeof(o);
  211. if(t == "function" && (o.__name__ || o.__ename__)) {
  212. t = "object";
  213. }
  214. switch(t) {
  215. case "function":
  216. return "<function>";
  217. case "object":
  218. if(o instanceof Array) {
  219. if(o.__enum__) {
  220. if(o.length == 2) {
  221. return o[0];
  222. }
  223. var str = o[0] + "(";
  224. s += "\t";
  225. var _g1 = 2;
  226. var _g = o.length;
  227. while(_g1 < _g) {
  228. var i = _g1++;
  229. if(i != 2) {
  230. str += "," + js_Boot.__string_rec(o[i],s);
  231. } else {
  232. str += js_Boot.__string_rec(o[i],s);
  233. }
  234. }
  235. return str + ")";
  236. }
  237. var l = o.length;
  238. var i1;
  239. var str1 = "[";
  240. s += "\t";
  241. var _g11 = 0;
  242. var _g2 = l;
  243. while(_g11 < _g2) {
  244. var i2 = _g11++;
  245. str1 += (i2 > 0 ? "," : "") + js_Boot.__string_rec(o[i2],s);
  246. }
  247. str1 += "]";
  248. return str1;
  249. }
  250. var tostr;
  251. try {
  252. tostr = o.toString;
  253. } catch( e ) {
  254. return "???";
  255. }
  256. if(tostr != null && tostr != Object.toString && typeof(tostr) == "function") {
  257. var s2 = o.toString();
  258. if(s2 != "[object Object]") {
  259. return s2;
  260. }
  261. }
  262. var k = null;
  263. var str2 = "{\n";
  264. s += "\t";
  265. var hasp = o.hasOwnProperty != null;
  266. for( var k in o ) {
  267. if(hasp && !o.hasOwnProperty(k)) {
  268. continue;
  269. }
  270. if(k == "prototype" || k == "__class__" || k == "__super__" || k == "__interfaces__" || k == "__properties__") {
  271. continue;
  272. }
  273. if(str2.length != 2) {
  274. str2 += ", \n";
  275. }
  276. str2 += s + k + " : " + js_Boot.__string_rec(o[k],s);
  277. }
  278. s = s.substring(1);
  279. str2 += "\n" + s + "}";
  280. return str2;
  281. case "string":
  282. return o;
  283. default:
  284. return String(o);
  285. }
  286. };
  287. var $_, $fid = 0;
  288. function $bind(o,m) { if( m == null ) return null; if( m.__id__ == null ) m.__id__ = $fid++; var f; if( o.hx__closures__ == null ) o.hx__closures__ = {}; else f = o.hx__closures__[m.__id__]; if( f == null ) { f = function(){ return f.method.apply(f.scope, arguments); }; f.scope = o; f.method = m; o.hx__closures__[m.__id__] = f; } return f; }
  289. String.__name__ = true;
  290. Array.__name__ = true;
  291. Main.main();
  292. })();