Scroll to empty text box

Press F2 to scroll to an empty text box and focus it

目前为 2018-10-14 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Scroll to empty text box
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2.0
  5. // @description Press F2 to scroll to an empty text box and focus it
  6. // @author Valacar
  7. // @match *://*/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. "use strict";
  13.  
  14. // custom key. note: anything besides function keys may not work.
  15. // key code list: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code#Code_values
  16. const config = {
  17. key: "F2"
  18. };
  19.  
  20. function findEmptyInput() {
  21. const inputs =
  22. document.querySelectorAll('textarea:not([readonly]),input[type="text"]:not([readonly])');
  23. for (let input of inputs) {
  24. if (input.value === "") {
  25. return input;
  26. }
  27. }
  28. }
  29.  
  30. addEventListener("keydown", e => {
  31. if (e.defaultPrevented) {
  32. return;
  33. }
  34. if (e.code === config.key) {
  35. const emptyInput = findEmptyInput();
  36. if (emptyInput) {
  37. //console.debug(emptyInput);
  38. emptyInput.scrollIntoView({
  39. behavior: "smooth",
  40. block: "center",
  41. inline: "nearest"
  42. });
  43. emptyInput.focus();
  44. }
  45. }
  46. });
  47.  
  48. })();