🏠 Home 

Greasy Fork is available in English.

Choose Form Field Selections On Hotkey

Choose predefined selections for drop-down form fields upon pressing a hotkey. For e.g. country, state, city. If the state or city fields changes dynamically based on the selected country, the hokey will need to be pressed again.


安装此脚本?
  1. // ==UserScript==
  2. // @name Choose Form Field Selections On Hotkey
  3. // @namespace ChooseFormFieldSelectionsOnHotkey
  4. // @version 1.0.1
  5. // @description Choose predefined selections for drop-down form fields upon pressing a hotkey. For e.g. country, state, city. If the state or city fields changes dynamically based on the selected country, the hokey will need to be pressed again.
  6. // @author jcunews
  7. // @match *://*/*
  8. // @grant none
  9. // ==/UserScript==
  10. var
  11. //hokey configuration
  12. hotkeyKey = "l", //key name. should be lowercase
  13. hotkeyShift = false, //need SHIFT key?
  14. hotkeyCtrl = true, //need CTRL key?
  15. hotkeyAlt = false, //need ALYT key?
  16. //Selections to choose if the form fields have them. Case insensitive substring (partial match).
  17. fieldValues = [
  18. "United States", //country name
  19. "Washington", //state name
  20. "Seattle", //city name
  21. "General Manager", //occupation
  22. "English" //language
  23. //Can be any number and any text. The script will select it if it matches.
  24. ];
  25. fieldValues.forEach(
  26. function(v, i) {
  27. fieldValues[i] = v.toLowerCase();
  28. }
  29. );
  30. addEventListener("keydown", function(ev) {
  31. if ((ev.key === hotkeyKey) && (ev.ctrlKey === hotkeyCtrl) && (ev.shiftKey === hotkeyShift) && (ev.altKey === hotkeyAlt)) {
  32. Array.prototype.slice.call(document.querySelectorAll("SELECT")).forEach(
  33. function(select) {
  34. Array.prototype.slice.call(select.options).some(
  35. function(option, i) {
  36. option = option.textContent.toLowerCase();
  37. if (fieldValues.some(
  38. function(v) {
  39. return option.indexOf(v) >= 0;
  40. }
  41. )) {
  42. if (select.selectedIndex !== i) {
  43. select.selectedIndex = i;
  44. select.dispatchEvent(new Event("change"));
  45. }
  46. return true;
  47. }
  48. }
  49. );
  50. }
  51. );
  52. ev.preventDefault();
  53. ev.stopPropagation();
  54. ev.stopImmediatePropagation();
  55. }
  56. }, true);