Duolingo input language switcher

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

目前为 2019-12-22 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Duolingo input language switcher
  3. // @namespace https://www.duolingo.com/IVrL9
  4. // @author T1mL3arn
  5. // @match https://www.duolingo.com/*
  6. // @version 2.0.2
  7. // @description This script allows you to type letters appropriate for current task without changing keyboard's layout
  8. // @run-at document-start
  9. // @grant none
  10. // @icon https://www.androidpolice.com/wp-content/uploads/2014/03/nexusae0_Duolingo-Thumb.png
  11. // @license GPLv3
  12. // @homepageURL https://github.com/T1mL3arn/Duolingo-input-language-switcher
  13. // @supportURL https://greasyfork.org/en/scripts/37693-duolingo-input-language-switcher/feedback
  14. // ==/UserScript==/// Generated by Haxe 4.0.3
  15. (function ($global) { "use strict";
  16. var Main = function() {
  17. this.document = window.document;
  18. this.console = window.console;
  19. this.initLanguages();
  20. if(this.document.readyState == "interactive" || this.document.readyState == "complete") {
  21. this.onready();
  22. } else {
  23. this.document.addEventListener("DOMContentLoaded",$bind(this,this.onready));
  24. }
  25. };
  26. Main.main = function() {
  27. new Main();
  28. };
  29. Main.prototype = {
  30. initLanguages: function() {
  31. 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"];
  32. this.languages = { };
  33. this.languages.ru = "ё1234567890-=\\йцукенгшщзхъфывапролджэячсмитьбю.Ё!\"№;%:?*()_+/ЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ,";
  34. this.languages.en = "`1234567890-=\\qwertyuiop[]asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+|QWERTYUIOP{}ASDFGHJKL:\"ZXCVBNM<>?";
  35. var len = this.languages.ru.length;
  36. var _g = 0;
  37. var _g1 = Reflect.fields(this.languages);
  38. while(_g < _g1.length) {
  39. var f = _g1[_g];
  40. ++_g;
  41. var act = this.languages[f].length;
  42. if(act != len) {
  43. this.console.error("LangString test failed: expected len " + len + "; actual len " + act + "; lang name " + f);
  44. this.console.error(this.languages[f]);
  45. return;
  46. }
  47. if(act != this.keyCodes.length * 2) {
  48. this.console.error("KeyCodes and LangString test failed: expected lang string len " + this.keyCodes.length * 2 + "; actual len " + act + "; lang name " + f);
  49. return;
  50. }
  51. }
  52. }
  53. ,onready: function(e) {
  54. this.document.removeEventListener("DOMContentLoaded",$bind(this,this.onready));
  55. this.console.log("Duolingo input switcher is ready");
  56. window.document.body.addEventListener("keydown",$bind(this,this.onKeyDown));
  57. }
  58. ,onKeyDown: function(e) {
  59. if(e.ctrlKey) {
  60. return;
  61. }
  62. var pressedKeyCodeIndex = this.keyCodes.indexOf(e.code);
  63. if(pressedKeyCodeIndex == -1) {
  64. return;
  65. }
  66. var inputElt = e.target;
  67. switch(inputElt.dataset.test) {
  68. case "challenge-listen-input":case "challenge-listentap-input":case "challenge-name-input":
  69. this.setLanguagePair("en","ru");
  70. break;
  71. case "challenge-text-input":
  72. this.setLanguagePair("en","ru");
  73. break;
  74. case "challenge-translate-input":
  75. this.nativeLanguage = "ru";
  76. this.foreignLanguage = "en";
  77. var lang = inputElt.getAttribute("lang");
  78. if(lang == this.nativeLanguage) {
  79. this.setLanguagePair("ru","en");
  80. } else if(lang == this.foreignLanguage) {
  81. this.setLanguagePair("en","ru");
  82. }
  83. break;
  84. default:
  85. return;
  86. }
  87. e.preventDefault();
  88. this.replaceLetter(this.getLanguageLetter(this.targetLanguage,pressedKeyCodeIndex,e.shiftKey),inputElt);
  89. this.callReactOnChange(inputElt);
  90. }
  91. ,setLanguagePair: function(target,source) {
  92. this.targetLanguage = target;
  93. this.sourceLanguage = source;
  94. }
  95. ,getLanguageLetter: function(language,letterIndex,isUppercase) {
  96. var letters = this.languages[language];
  97. if(isUppercase) {
  98. return letters.charAt(letterIndex + this.keyCodes.length);
  99. } else {
  100. return letters.charAt(letterIndex);
  101. }
  102. }
  103. ,replaceLetter: function(letter,inputElt) {
  104. var start = inputElt.selectionStart;
  105. var end = inputElt.selectionEnd;
  106. var phrase = inputElt.value;
  107. phrase = phrase.substring(0,start) + letter + phrase.substring(end);
  108. inputElt.value = phrase;
  109. inputElt.setSelectionRange(start + 1,start + 1);
  110. }
  111. ,callReactOnChange: function(elt) {
  112. var _g = 0;
  113. var _g1 = Reflect.fields(elt);
  114. while(_g < _g1.length) {
  115. var fieldName = _g1[_g];
  116. ++_g;
  117. if(fieldName.indexOf("__reactEventHandlers") != -1) {
  118. Reflect.field(elt,fieldName).onChange({ target : elt});
  119. }
  120. }
  121. }
  122. };
  123. var Reflect = function() { };
  124. Reflect.field = function(o,field) {
  125. try {
  126. return o[field];
  127. } catch( e ) {
  128. return null;
  129. }
  130. };
  131. Reflect.fields = function(o) {
  132. var a = [];
  133. if(o != null) {
  134. var hasOwnProperty = Object.prototype.hasOwnProperty;
  135. for( var f in o ) {
  136. if(f != "__id__" && f != "hx__closures__" && hasOwnProperty.call(o,f)) {
  137. a.push(f);
  138. }
  139. }
  140. }
  141. return a;
  142. };
  143. var $_;
  144. function $bind(o,m) { if( m == null ) return null; if( m.__id__ == null ) m.__id__ = $global.$haxeUID++; var f; if( o.hx__closures__ == null ) o.hx__closures__ = {}; else f = o.hx__closures__[m.__id__]; if( f == null ) { f = m.bind(o); o.hx__closures__[m.__id__] = f; } return f; }
  145. $global.$haxeUID |= 0;
  146. Main.main();
  147. })(typeof window != "undefined" ? window : typeof global != "undefined" ? global : typeof self != "undefined" ? self : this);