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