ASCII Control Codes Keyboard Input Helper for Firefox

This is a script to help inputting ASCII Control Codes such as TAB, CR, LF, etc. on Firefox web browsers using ALT+Numpad keys. Inputted control codes must begin with 0 (e.g. 09, 013, etc.), and only control code 1 to 31 are accepted. The control character will only be generated when the current focus is on a TEXTAREA element, or a text typed INPUT element.

目前为 2017-09-11 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name ASCII Control Codes Keyboard Input Helper for Firefox
  3. // @namespace ASCIIControlCodesKeyboardInputHelperForFirefox
  4. // @version 1.0.1
  5. // @description This is a script to help inputting ASCII Control Codes such as TAB, CR, LF, etc. on Firefox web browsers using ALT+Numpad keys. Inputted control codes must begin with 0 (e.g. 09, 013, etc.), and only control code 1 to 31 are accepted. The control character will only be generated when the current focus is on a TEXTAREA element, or a text typed INPUT element.
  6. // @author jcunews
  7. // @include *://*/*
  8. // @grant none
  9. // @run-at document-start
  10. // ==/UserScript==
  11.  
  12. var numpadKeyCodes = [45, 35, 40, 34, 37, 12, 39, 36, 38, 33]; //Insert, End, Down, PgDn, Left, Clear, Right, Home, Up, PgUp
  13. var code = "";
  14.  
  15. function numFromKeyCode(keyCode) {
  16. if ((keyCode >= 96) && (keyCode <= 105)) { //Numpad0 - Numpad9
  17. return keyCode - 96;
  18. } else return numpadKeyCodes.indexOf(keyCode);
  19. }
  20.  
  21. function keyUp(ev, ele, cc, i, s) {
  22. ele = document.activeElement;
  23. if ((ele.tagName === "TEXTAREA") || ((ele.tagName === "INPUT") && (ele.type === "text"))) {
  24. ev = ev || window.event;
  25. if (ev.altKey) {
  26. cc = numFromKeyCode(ev.keyCode);
  27. if (cc >= 0) code += cc;
  28. } else {
  29. if ((ev.keyCode === 18 /*ALT*/) && (code.charAt(0) === "0") && (code = parseInt(code, 10)) && (code < 32)) {
  30. i = ele.selectionStart;
  31. s = ele.value;
  32. ele.value = s.substring(0, i) + String.fromCharCode(code) + s.substring(ele.selectionEnd, s.length);
  33. ele.selectionEnd = i + 1;
  34. }
  35. code = "";
  36. }
  37. } else code = "";
  38. }
  39.  
  40. if (window.InstallTrigger) document.addEventListener("keyup", keyUp);