WaniKani Cursor Behavior

change cursor behavior so that it doesn't jump to the end when editing kana

  1. // ==UserScript==
  2. // @name WaniKani Cursor Behavior
  3. // @namespace https://www.wanikani.com
  4. // @description change cursor behavior so that it doesn't jump to the end when editing kana
  5. // @version 0.1.0
  6. // @include https://www.wanikani.com/review/session
  7. // @include https://www.wanikani.com/lesson/session
  8. // @include http://www.wanikani.com/review/session
  9. // @include http://www.wanikani.com/lesson/session
  10. // @run-at document-end
  11. // @grant none
  12. // ==/UserScript==
  13.  
  14. /*global console, wanakana*/
  15.  
  16. /*
  17. source very slightly modified from:
  18. https://github.com/WaniKani/WanaKana
  19. License: The MIT License (MIT)
  20. */
  21.  
  22. (function () {
  23. 'use strict';
  24. wanakana._onInput = function (event) {
  25. var input, newText, normalizedInputString, range, startingCursor, startingLength;
  26. input = event.target;
  27. startingCursor = input.selectionStart;
  28. startingLength = input.value.length;
  29. normalizedInputString = wanakana._convertFullwidthCharsToASCII(input.value);
  30. newText = wanakana.toKana(normalizedInputString, {
  31. IMEMode: true
  32. });
  33. if (normalizedInputString !== newText) {
  34. input.value = newText;
  35. if (typeof input.selectionStart === "number") {
  36. // input.selectionStart = input.selectionEnd = input.value.length; // original line
  37. input.selectionStart = input.selectionEnd = startingCursor + input.value.length - startingLength;
  38. } else if (typeof input.createTextRange !== "undefined") {
  39. input.focus();
  40. range = input.createTextRange();
  41. range.collapse(false);
  42. range.select();
  43. }
  44. }
  45. };
  46. console.log('WaniKani Cursor Behavior: script load end');
  47. }());