atcoder-refactor

Rewrites variable names in AtCoder problem statements.

Verze ze dne 15. 06. 2020. Zobrazit nejnovější verzi.

  1. // ==UserScript==
  2. // @name atcoder-refactor
  3. // @namespace https://github.com/yoshrc
  4. // @version 0.1
  5. // @description Rewrites variable names in AtCoder problem statements.
  6. // @author yoshrc
  7. // @match https://atcoder.jp/contests/*/tasks/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. // TODO
  12. // - Inline edit like IDE's multiselection instead of popup
  13. // - Save variable name mapping in localStorage
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. const ID_ATTR = 'data-atcoder-refactor-id';
  19.  
  20. const isAlpha = str => str.match(/^[A-Za-z]+$/);
  21.  
  22. const rewriteVariables = id => {
  23. // TODO: Use current variable name instead of id
  24. const newName = prompt('Variable Name', id);
  25. document.querySelectorAll(`[${ID_ATTR}=${id}]`).forEach(varElem => {
  26. varElem.textContent = newName;
  27. })
  28. }
  29.  
  30. // TODO: Use MathJax hook instead of wait 1000ms
  31. setTimeout(() => {
  32. document.querySelectorAll('.mjx-char').forEach(varElem => {
  33. const varId = varElem.textContent;
  34. if (!isAlpha(varId)) {
  35. return;
  36. }
  37.  
  38. varElem.setAttribute(ID_ATTR, varId);
  39. varElem.onclick = () => rewriteVariables(varId);
  40. });
  41. }, 1000);
  42. })();