Accented Character Input

Add functionality to input accented characters for non accented keyboards when the current input focus is on a text input element, or a content-editable element.

  1. // ==UserScript==
  2. // @name Accented Character Input
  3. // @namespace https://greasyfork.org/en/users/85671-jcunews
  4. // @version 1.0.6
  5. // @license GNU AGPLv3
  6. // @author jcunews
  7. // @description Add functionality to input accented characters for non accented keyboards when the current input focus is on a text input element, or a content-editable element.
  8. // @match *://*/*
  9. // @grant none
  10. // @run-at document-start
  11. // ==/UserScript==
  12.  
  13. /*
  14. Note: accented character input will not work on a content-editable element when there's a selection which spans across other element).
  15.  
  16. Accented character can be inputted by holding the ALT key then pressing the desired character (e.g. <ALT+a> for "à", or <ALT+SHIFT+A> for "À").
  17.  
  18. Whether capital or non capital letter is used depends on the current state of the CapsLock key and whether the SHIFT key is also held down or not.
  19.  
  20. While the ALT key is still being held, pressing the same character again will change the generated character to the next available accented character (e.g. <ALT+e> then <e> then <e> will generate the "ê" character).
  21.  
  22. If a diffrent character is pressed while the ALT key is held down, the accented character of the new character will be generated (e.g. <ALT+a> then <e> will generate "àè").
  23.  
  24. The typed characters and their available accented characters are configurable in the `charMap` variable within the script.
  25. */
  26.  
  27. (function(charMap, ele, inp, prevIdx, prevChar) {
  28.  
  29. charMap = {
  30. "A": "ÀÁÂÃÄÅ",
  31. "a": "àáâãäå",
  32. "C": "Ç",
  33. "c": "ç",
  34. "E": "ÈÉÊË",
  35. "e": "èéêë",
  36. "I": "ÌÍÎÏ",
  37. "i": "ìíîï",
  38. "N": "Ñ",
  39. "n": "ñ",
  40. "O": "ÒÓÔÕÖ",
  41. "o": "òóôõö",
  42. "U": "ÙÚÛÜ",
  43. "u": "ùúûü",
  44. "Y": "Ý",
  45. "y": "ýÿ"
  46. };
  47.  
  48. function init(e) {
  49. ele = e;
  50. if (ele) {
  51. inp = !ele.disabled && ((ele.tagName === "TEXTAREA") || ((ele.tagName === "INPUT") && (ele.type === "text")) || (ele.contentEditable === "true"));
  52. prevIdx = -1;
  53. prevChar = "";
  54. }
  55. return ele;
  56. }
  57.  
  58. addEventListener("blur", function(ev) {
  59. ele = null;
  60. }, true);
  61.  
  62. addEventListener("keydown", function(ev, chars, i, sel, cs, ce) {
  63. if (!ev.altKey) {
  64. ele = null;
  65. init(ev.target);
  66. return;
  67. } else if (!(chars = charMap[ev.key]) || (!ele && !init(ev.target))) return;
  68. if ((ev.target !== ele) && !init(ev.target)) return;
  69. if (ev.key !== prevChar) prevIdx = -1;
  70. if (prevIdx >= 0) {
  71. i = prevIdx + 1;
  72. if (i >= chars.length) i = 0;
  73. } else i = 0;
  74. if (inp) {
  75. cs = ele.selectionStart;
  76. ce = ele.selectionEnd;
  77. if (prevIdx >= 0) cs--;
  78. ele.value = ele.value.substring(0, cs) + chars[i] + ele.value.substr(ce);
  79. ele.selectionStart = cs + 1;
  80. ele.selectionEnd = cs + 1;
  81. } else if (ele.contentEditable === "true") {
  82. sel = getSelection();
  83. if (!sel || !sel.anchorNode || (sel.anchorNode !== sel.focusNode) || !sel.anchorNode.data) return;
  84. if ((cs = sel.anchorOffset) > (ce = sel.focusOffset)) {
  85. cs = sel.focusOffset;
  86. ce = sel.anchorOffset;
  87. }
  88. if (prevIdx >= 0) cs--;
  89. sel.anchorNode.data = sel.anchorNode.data.substring(0, cs) + chars[i] + sel.anchorNode.data.substr(ce);
  90. sel.collapse(sel.anchorNode, cs + 1);
  91. }
  92. prevIdx = i;
  93. prevChar = ev.key;
  94. ev.preventDefault();
  95. ev.stopPropagation();
  96. ev.stopImmediatePropagation();
  97. }, true);
  98.  
  99. })();