Mturk Radio Keybinds

Keybinds to select radios

目前为 2017-09-01 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Mturk Radio Keybinds
  3. // @namespace https://gist.github.com/Kadauchi
  4. // @version 2.1.4
  5. // @description Keybinds to select radios
  6. // @author Kadauchi
  7. // @icon http://i.imgur.com/oGRQwPN.png
  8. // @include /^https://(www\.mturkcontent|s3\.amazonaws)\.com/
  9. // @grant GM_getValue
  10. // @grant GM_setValue
  11. // ==/UserScript==
  12.  
  13. if (document.querySelector(`[type="radio"]`).length === 0) return;
  14.  
  15. document.body.insertAdjacentHTML(
  16. `afterbegin`,
  17.  
  18. `<div style="background-color: lightgreen;">` +
  19. `<label style="color: black; margin-left: 10px;">Script: Mturk Radio Keybinds</label>` +
  20. `<span style="margin-left: 3px;cursor:help" title="Press 1 through 9 or z through / to select the radio you want. (0 is 10th) \n\nPress Enter to submit the HIT. \n\nCheck auto submit to have the HIT submit after you press your keybind.">&#10068;</span>` +
  21.  
  22. `<label style="color: black; float: right; margin-right: 10px;">Auto Submit: ` +
  23. `<input id="autosubmit" type="checkbox" ${GM_getValue(`autosubmit`) ? `checked` : ``}></input>` +
  24. `</label>` +
  25.  
  26. `<label style="color: black; float: right; margin-right: 10px;">Use [z-/]: ` +
  27. `<input id="letters" type="checkbox" ${GM_getValue(`letters`) ? `checked` : ``}></input>` +
  28. `</label>` +
  29.  
  30. `<label style="color: black; float: right; margin-right: 10px;">Use [0-9]: ` +
  31. `<input id="numbers" type="checkbox" ${GM_getValue(`numbers`) ? `checked` : ``}></input>` +
  32. `</label>` +
  33.  
  34. `</div>`
  35. );
  36.  
  37. const numbers = document.getElementById(`numbers`);
  38. const letters = document.getElementById(`letters`);
  39. const autosubmit = document.getElementById(`autosubmit`);
  40.  
  41. numbers.addEventListener(`change`, e => GM_setValue(`numbers`, numbers.checked));
  42. letters.addEventListener(`change`, e => GM_setValue(`letters`, letters.checked));
  43. autosubmit.addEventListener(`change`, e => GM_setValue(`autosubmit`, autosubmit.checked));
  44.  
  45. window.addEventListener(`keydown`, e => {
  46. const key = e.key;
  47.  
  48. if (key.length === 1) {
  49. if (numbers.checked && key.match(/[0-9]/)) {
  50. const radio = document.querySelectorAll(`[type="radio"]`)[key !== 0 ? key - 1 : 9];
  51.  
  52. if (radio) radio.click();
  53. if (autosubmit.checked) document.querySelector(`[type="submit"]`).click();
  54. }
  55.  
  56. if (letters.checked && key.match(/z|x|c|v|b|n|m|,|\.|\//)) {
  57. const convert = { 'z': 0, 'x': 1, 'c': 2, 'v': 3, 'b': 4, 'n': 5, 'm': 6, ',': 7, '.': 8, '/': 9 };
  58. const radio = document.querySelectorAll(`[type="radio"]`)[convert[key]];
  59.  
  60. if (radio) radio.click();
  61. if (autosubmit.checked) document.querySelector(`[type="submit"]`).click();
  62. }
  63. }
  64.  
  65. if (key === `Enter`) {
  66. document.querySelector(`[type="submit"]`).click();
  67. }
  68. });
  69.  
  70. window.focus();