Updated Quizlet Micromatch bot

Win micromatch in less than a second! This is based off of the original script by Danielv123, but far upgraded

目前为 2021-04-13 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Updated Quizlet Micromatch bot
  3. // @namespace BenjaminHinchliff
  4. // @version 0.3
  5. // @description Win micromatch in less than a second! This is based off of the original script by Danielv123, but far upgraded
  6. // @author Benjamin Hinchliff
  7. // @match https://quizlet.com/*/match*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. // modify this to change the total delay of all the clicking
  12. const delay = 0.5;
  13. // artificial slowing of the delay (in ms) to because Quizlet's timer starts late sometimes
  14. const delayEpsilon = 10;
  15.  
  16. // a little function to allow the program to click on elements
  17. function clickEvent(element) {
  18. let eve = new CustomEvent("pointerdown", { bubbles: true });
  19. element.dispatchEvent(eve);
  20. }
  21.  
  22. function getTerms() {
  23. // eslint-disable-next-line no-undef
  24. let rawTerms = Quizlet.matchModeData.terms;
  25. let terms = {};
  26. for (const term of rawTerms) {
  27. terms[term.word] = term.definition;
  28. }
  29. return terms
  30. }
  31.  
  32. // only works if 1 solve per load (which is true rn)
  33. let solving = false;
  34.  
  35. // try to run on load or click
  36. function solveMatch(terms) {
  37. // get all the nodes (and convert to array)
  38. let nodes = document.querySelector(".MatchModeQuestionGridBoard-tiles")?.childNodes;
  39. if (!nodes) {
  40. return;
  41. }
  42. // prevent multiple execution
  43. if (solving) {
  44. return;
  45. }
  46. solving = true;
  47. nodes = Array.from(nodes);
  48. const perElementDelay = ((delay * 1000) / nodes.length) + delayEpsilon;
  49. for (let i = 0, len = nodes.length; i < len; i += 1) {
  50. const node = nodes[i];
  51. // get term
  52. let search = node.firstChild?.firstChild?.firstChild?.firstChild?.innerHTML;
  53. // match with definition
  54. let dictionaryResult = terms[search];
  55. if(dictionaryResult) {
  56. // find term in nodes and send click events
  57. const targetNode = nodes.find((node) => node.innerHTML.includes(dictionaryResult));
  58. if (targetNode !== undefined) {
  59. setTimeout(() => {
  60. clickEvent(node.firstChild);
  61. clickEvent(targetNode.firstChild);
  62. }, perElementDelay * i);
  63. }
  64. }
  65. }
  66. }
  67.  
  68.  
  69. const terms = getTerms();
  70.  
  71. solveMatch(terms);
  72. // probably really slow but who cares
  73. let observer = new MutationObserver((mutations) => {
  74. mutations.forEach((mutation) => {
  75. if (!mutation.addedNodes) {
  76. return;
  77. }
  78. solveMatch(terms);
  79. })
  80. });
  81.  
  82. // tbd whether this is smart or just really dumb
  83. observer.observe(document.body, {
  84. childList: true,
  85. subtree: true,
  86. attributes: false,
  87. characterData: false,
  88. });