Mturk Radio Keybinds

Keybinds to select radios

目前为 2017-02-22 提交的版本。查看 最新版本

// ==UserScript==
// @name         Mturk Radio Keybinds
// @namespace    https://gist.github.com/Kadauchi
// @version      2.0.1
// @description  Keybinds to select radios
// @author       Kadauchi
// @icon         http://i.imgur.com/oGRQwPN.png
// @include      /^https://(www\.mturkcontent|s3\.amazonaws)\.com/
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==

document.body.insertAdjacentHTML(`afterbegin`,
  `<div style="background-color: lightgreen;">` +
    `<label style="color: black; margin-left: 10px;">Script: Mturk Radio Keybinds</label>` +
    `<span style="margin-left: 3px;cursor:help" title="Press 1-9 to select the radio you want. \n\nPress Enter to submit the HIT. \n\nCheck auto submit to have the HIT submit after you press 1-9.">&#10068;</span>` +
    `<label style="color: black; float: right; margin-right: 10px;">Auto Submit: ` +
      `<input id="autosubmit" type="checkbox" ${GM_getValue(`autosubmit`) ? `checked` : ``}></input>` +
    `</label>` +
  `</div>`
);

const autosubmit = document.getElementById(`autosubmit`);

autosubmit.addEventListener(`change`, function (event) {
  GM_setValue(`autosubmit`, autosubmit.checked);
});

window.addEventListener(`keydown`, function (event) {
  const key = event.key;

  if (key.match(/[1-9]/)) {
    const radio = document.querySelectorAll(`[type="radio"]`)[key - 1];
    if (radio) radio.click();

    if (autosubmit.checked) document.querySelector(`[type="submit"]`).click();
  }

  if (key.match(/Enter/)) {
    document.querySelector(`[type="submit"]`).click();
  }
});

window.focus();