midjourney-click-all

Click all the buttons in a row at once

当前为 2023-05-19 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name midjourney-click-all
// @namespace https://github.com/mefengl
// @version 0.0.4
// @description Click all the buttons in a row at once
// @author mefengl
// @match https://discord.com/channels/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=midjourney.com
// @grant none
// @license MIT
// ==/UserScript==
"use strict";
(() => {
  var __async = (__this, __arguments, generator) => {
    return new Promise((resolve, reject) => {
      var fulfilled = (value) => {
        try {
          step(generator.next(value));
        } catch (e) {
          reject(e);
        }
      };
      var rejected = (value) => {
        try {
          step(generator.throw(value));
        } catch (e) {
          reject(e);
        }
      };
      var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
      step((generator = generator.apply(__this, __arguments)).next());
    });
  };

  // ../../packages/midkit/dist/index.mjs
  function sleep(ms) {
    return new Promise((resolve) => setTimeout(resolve, ms));
  }
  function getButtonContainers() {
    return Array.from(document.querySelectorAll("[id^='message-accessorie'] [class^='children']"));
  }
  var CLONE_BUTTON_CLASS = "cloned-button";
  function getButtons(temp) {
    const buttons = temp.querySelectorAll("button");
    const originalButtons = Array.from(buttons).filter((button) => !button.classList.contains(CLONE_BUTTON_CLASS));
    return originalButtons;
  }
  function oneMoreButton(temp) {
    const buttons = getButtons(temp);
    const lastButton = buttons[buttons.length - 1];
    const cloneButton = lastButton.cloneNode();
    cloneButton.classList.add(CLONE_BUTTON_CLASS);
    lastButton.parentNode.insertBefore(cloneButton, lastButton.nextSibling);
    return cloneButton;
  }
  function getSubmitButton() {
    return document.querySelector('button[type="submit"]');
  }
  function submitForm() {
    return __async(this, null, function* () {
      const submitButton = getSubmitButton();
      if (!submitButton) {
        console.log("No submit button found");
        return;
      }
      submitButton.click();
    });
  }
  function getFormNumber() {
    const forms = document.querySelectorAll("form");
    return forms.length;
  }
  function clickAllButtonsWithConfirm(temp) {
    return __async(this, null, function* () {
      const buttons = getButtons(temp);
      for (const button of buttons) {
        button.click();
        while (getFormNumber() === 1) {
          yield sleep(500);
          console.log("Opening...");
        }
        submitForm();
        while (getFormNumber() === 2) {
          yield sleep(500);
          console.log("Submitting...");
        }
      }
    });
  }
  function isClicking(button) {
    return button.querySelector("div > div").getAttribute("aria-hidden") === "true";
  }
  function clickAllButtons(temp) {
    return __async(this, null, function* () {
      const buttons = getButtons(temp);
      buttons.pop();
      for (const button of buttons) {
        button.click();
        while (!isClicking(button)) {
          yield sleep(10);
          console.log("Waiting for clicking...");
        }
        while (isClicking(button)) {
          yield sleep(500);
          console.log("Clicking...");
        }
      }
    });
  }
  function smartClickAllButtons(temp) {
    return __async(this, null, function* () {
      const buttons = getButtons(temp);
      const testText = buttons[0].textContent;
      if (testText == null ? void 0 : testText.startsWith("U")) {
        yield clickAllButtons(temp);
      } else {
        yield clickAllButtonsWithConfirm(temp);
      }
    });
  }

  // src/index.ts
  function initialize() {
    return __async(this, null, function* () {
      yield new Promise((resolve) => window.addEventListener("load", resolve));
      yield new Promise((resolve) => setTimeout(resolve, 1e4));
    });
  }
  function main() {
    return __async(this, null, function* () {
      yield initialize();
      const buttonContainers = getButtonContainers();
      for (const buttonContainer of buttonContainers) {
        const clickAllButton = oneMoreButton(buttonContainer);
        clickAllButton.textContent = "ALL";
        clickAllButton.addEventListener("click", () => {
          smartClickAllButtons(buttonContainer);
        });
      }
    });
  }
  (function() {
    main();
  })();
})();