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.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Choose Form Field Selections On Hotkey
// @namespace    ChooseFormFieldSelectionsOnHotkey
// @version      1.0.1
// @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.
// @author       jcunews
// @match        *://*/*
// @grant        none
// ==/UserScript==

var

  //hokey configuration
  hotkeyKey   = "l",   //key name. should be lowercase
  hotkeyShift = false, //need SHIFT key?
  hotkeyCtrl  = true,  //need CTRL key?
  hotkeyAlt   = false, //need ALYT key?

  //Selections to choose if the form fields have them. Case insensitive substring (partial match).
  fieldValues = [
    "United States",   //country name
    "Washington",      //state name
    "Seattle",         //city name
    "General Manager", //occupation
    "English"          //language
    //Can be any number and any text. The script will select it if it matches.
  ];

fieldValues.forEach(
  function(v, i) {
    fieldValues[i] = v.toLowerCase();
  }
);

addEventListener("keydown", function(ev) {
  if ((ev.key === hotkeyKey) && (ev.ctrlKey === hotkeyCtrl) && (ev.shiftKey === hotkeyShift) && (ev.altKey === hotkeyAlt)) {
    Array.prototype.slice.call(document.querySelectorAll("SELECT")).forEach(
      function(select) {
        Array.prototype.slice.call(select.options).some(
          function(option, i) {
            option = option.textContent.toLowerCase();
            if (fieldValues.some(
              function(v) {
                return option.indexOf(v) >= 0;
              }
            )) {
              if (select.selectedIndex !== i) {
                select.selectedIndex = i;
                select.dispatchEvent(new Event("change"));
              }
              return true;
            }
          }
        );
      }
    );
    ev.preventDefault();
    ev.stopPropagation();
    ev.stopImmediatePropagation();
  }
}, true);