Mathway auto-get free answer steps

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

当前为 2024-01-22 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Mathway auto-get free answer steps
// @namespace    https://greasyfork.org/en/users/85671-jcunews
// @version      1.0.1
// @license      AGPL v3
// @author       jcunews
// @description  Context: https://www.reddit.com/r/GreaseMonkey/comments/19cai8u/requestcommission_looking_for_a_mathway_script/
// @match        https://www.mathway.com/*
// @grant        none
// ==/UserScript==

((delayBeforeReplay, jq, ja, exp, tid, busy) => {
  //===== CONFIG BEGIN (delays are in milliseconds. 1000ms = 1 second)
  retryDelay       = 200; //delay before retrying a query
  topicSelectDelay = 100; //delay before selecting a topic for answer
  defaultTopicId   = 395; //default topic ID if not prompted. 395 = Evaluate
  //===== CONFIG END

  function reset() {
    exp = []; tid = undefined; busy = false
  }
  function we(c, a, b) {
    if (a = document.querySelector('#topics-inner')) {
      if (b = a.querySelector(`.topics-single[data-id="${tid}"]`)) {
        return setTimeout(() => b.click(), topicSelectDelay)
      } else if (b = a.querySelector('.topics-more')) {
        return setTimeout(() => b.click(), topicSelectDelay)
      } else if (--c === 0) {
        reset();
        return alert("Topic is not found.")
      }
    }
    setTimeout(we, 100, c)
  }
  function inpExp(ei) {
    if (ei < exp.length) {
      $(`.kbKey[data-id="${exp[ei++]}"]`).mouseup();
      setTimeout(inpExp, 100, ei)
    } else document.querySelector('.editor-buttons>button.enabled').click()
  }
  function wb(a) {
    if (a = document.querySelector('.ch-bubble-border.clickable:has(.ch-bubble-trial)')) return a.click();
    setTimeout(wb, 100)
  }
  function jad(f) {
    var q = this, f_ = f;
    f = function(r) {
      if (/^\/chat\/(editor|topics)/.test(q.url)) {
        if (r.topics) {
          if (busy) we(5)
        } else if (r.messages) {
          if (tid === undefined) tid = defaultTopicId;
          if (r.messages[0]?.action) {
            if (r.messages[0].action.params?.freeTrialToken) {
              wb(busy = false)
            } else {
              busy = true;
              setTimeout(inpExp, retryDelay, 0)
            }
          } else reset()
        }
      }
      return f_.apply(this, arguments)
    };
    return this.done_.apply(this, arguments)
  }
  function jaf(f) {
    var f_ = f;
    f = function(e) {
      reset();
      return f_.apply(this, arguments)
    };
    return this.fail_.apply(this, arguments)
  }
  jq = window.jQuery || window.$; ja = jq.ajax;
  jq.ajax = function(uo) {
    var r;
    if (/^\/chat\/(editor|topics)/.test(uo.url)) {
      if (tid === undefined) tid = JSON.parse(uo.data).topicId;
      r = ja.apply(this, arguments);
      r.url = uo.url;
      if (!r.done_) {
        r.done_ = r.done;
        r.done = jad;
        r.fail_ = r.fail;
        r.fail = jaf
      }
      return r
    }
    return ja.apply(this, arguments)
  };
  reset();
  addEventListener("mousedown", ev => {
    var el = ev.target;
    if (!busy && el.matches && el.matches('.kbKey')) {
      if (el.dataset.id === "backspace") {
        exp.pop(el.dataset.id)
      } else exp.push(el.dataset.id)
    }
  }, true)
})()