Greasy Fork is available in English.

AtCoder Registration Autofill

AtCoderの参加登録情報を保存し、自動入力します。

  1. // ==UserScript==
  2. // @name AtCoder Registration Autofill
  3. // @namespace https://github.com/Raclamusi
  4. // @version 1.0.0
  5. // @description AtCoderの参加登録情報を保存し、自動入力します。
  6. // @author Raclamusi
  7. // @supportURL https://github.com/Raclamusi/atcoder-registration-autofill
  8. // @match https://atcoder.jp/contests/*/register
  9. // @grant none
  10. // @license CC0
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. "use strict";
  15.  
  16. const storageKey = "atcoder_registration_autofill__data";
  17. const getAutofillData = () => {
  18. return JSON.parse(localStorage.getItem(storageKey));
  19. };
  20. const setAutofillData = (name, type, value) => {
  21. const data = getAutofillData() ?? {};
  22. if (!(name in data)) {
  23. data[name] = {};
  24. }
  25. if (type === "radio") {
  26. if (!(type in data[name])) {
  27. data[name][type] = [];
  28. }
  29. data[name][type].unshift(value);
  30. }
  31. else {
  32. data[name][type] = value;
  33. }
  34. localStorage.setItem(storageKey, JSON.stringify(data));
  35. };
  36.  
  37. const form = document.querySelector(".form-horizontal");
  38. if (getAutofillData() === null) {
  39. setAutofillData("日本国内在住か?", "radio", "Yes");
  40. }
  41. for (const [name, values] of Object.entries(getAutofillData())) {
  42. const input = form[name];
  43. if (input) {
  44. const type = (input instanceof RadioNodeList) ? "radio" : input.type;
  45. if (type in values) {
  46. const value = values[type];
  47. if (type === "radio") {
  48. const radioButtons = [...input].map(e => e.value);
  49. for (const v of value) {
  50. if (radioButtons.includes(v)) {
  51. input.value = v;
  52. break;
  53. }
  54. }
  55. }
  56. else if (type === "checkbox") {
  57. input.checked = value;
  58. }
  59. else {
  60. input.value = value;
  61. }
  62. }
  63. }
  64. }
  65.  
  66. for (const input of form) {
  67. if (input.type === "checkbox") {
  68. input.addEventListener("change", e => {
  69. setAutofillData(e.target.name, e.target.type, e.target.checked);
  70. });
  71. }
  72. else {
  73. input.addEventListener("change", e => {
  74. setAutofillData(e.target.name, e.target.type, e.target.value);
  75. });
  76. }
  77. }
  78. })();