Mathway auto-get free answer steps

Context: https://www.reddit.com/r/GreaseMonkey/comments/19cai8u/requestcommission_looking_for_a_mathway_script/

目前为 2024-01-22 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Mathway auto-get free answer steps
  3. // @namespace https://greasyfork.org/en/users/85671-jcunews
  4. // @version 1.0.1
  5. // @license AGPL v3
  6. // @author jcunews
  7. // @description Context: https://www.reddit.com/r/GreaseMonkey/comments/19cai8u/requestcommission_looking_for_a_mathway_script/
  8. // @match https://www.mathway.com/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. ((delayBeforeReplay, jq, ja, exp, tid, busy) => {
  13. //===== CONFIG BEGIN (delays are in milliseconds. 1000ms = 1 second)
  14. retryDelay = 200; //delay before retrying a query
  15. topicSelectDelay = 100; //delay before selecting a topic for answer
  16. defaultTopicId = 395; //default topic ID if not prompted. 395 = Evaluate
  17. //===== CONFIG END
  18.  
  19. function reset() {
  20. exp = []; tid = undefined; busy = false
  21. }
  22. function we(c, a, b) {
  23. if (a = document.querySelector('#topics-inner')) {
  24. if (b = a.querySelector(`.topics-single[data-id="${tid}"]`)) {
  25. return setTimeout(() => b.click(), topicSelectDelay)
  26. } else if (b = a.querySelector('.topics-more')) {
  27. return setTimeout(() => b.click(), topicSelectDelay)
  28. } else if (--c === 0) {
  29. reset();
  30. return alert("Topic is not found.")
  31. }
  32. }
  33. setTimeout(we, 100, c)
  34. }
  35. function inpExp(ei) {
  36. if (ei < exp.length) {
  37. $(`.kbKey[data-id="${exp[ei++]}"]`).mouseup();
  38. setTimeout(inpExp, 100, ei)
  39. } else document.querySelector('.editor-buttons>button.enabled').click()
  40. }
  41. function wb(a) {
  42. if (a = document.querySelector('.ch-bubble-border.clickable:has(.ch-bubble-trial)')) return a.click();
  43. setTimeout(wb, 100)
  44. }
  45. function jad(f) {
  46. var q = this, f_ = f;
  47. f = function(r) {
  48. if (/^\/chat\/(editor|topics)/.test(q.url)) {
  49. if (r.topics) {
  50. if (busy) we(5)
  51. } else if (r.messages) {
  52. if (tid === undefined) tid = defaultTopicId;
  53. if (r.messages[0]?.action) {
  54. if (r.messages[0].action.params?.freeTrialToken) {
  55. wb(busy = false)
  56. } else {
  57. busy = true;
  58. setTimeout(inpExp, retryDelay, 0)
  59. }
  60. } else reset()
  61. }
  62. }
  63. return f_.apply(this, arguments)
  64. };
  65. return this.done_.apply(this, arguments)
  66. }
  67. function jaf(f) {
  68. var f_ = f;
  69. f = function(e) {
  70. reset();
  71. return f_.apply(this, arguments)
  72. };
  73. return this.fail_.apply(this, arguments)
  74. }
  75. jq = window.jQuery || window.$; ja = jq.ajax;
  76. jq.ajax = function(uo) {
  77. var r;
  78. if (/^\/chat\/(editor|topics)/.test(uo.url)) {
  79. if (tid === undefined) tid = JSON.parse(uo.data).topicId;
  80. r = ja.apply(this, arguments);
  81. r.url = uo.url;
  82. if (!r.done_) {
  83. r.done_ = r.done;
  84. r.done = jad;
  85. r.fail_ = r.fail;
  86. r.fail = jaf
  87. }
  88. return r
  89. }
  90. return ja.apply(this, arguments)
  91. };
  92. reset();
  93. addEventListener("mousedown", ev => {
  94. var el = ev.target;
  95. if (!busy && el.matches && el.matches('.kbKey')) {
  96. if (el.dataset.id === "backspace") {
  97. exp.pop(el.dataset.id)
  98. } else exp.push(el.dataset.id)
  99. }
  100. }, true)
  101. })()