PageAutomator

Automate the actions on the page

Stan na 27-01-2023. Zobacz najnowsza wersja.

Ten skrypt nie powinien być instalowany bezpośrednio. Jest to biblioteka dla innych skyptów do włączenia dyrektywą meta // @require https://update.greasyfork.org/scripts/458951/1142464/PageAutomator.js

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         PageAutomator
// @description  Automate the actions on the page
// @version      1.0.0
// @author       aolko
// @match        *
// @namespace    https://greasyfork.org/ru/users/5008-aolko
// @run-at       document-start
// @grant        none
// @require      https://cdnjs.cloudflare.com/ajax/libs/mousetrap/1.6.5/mousetrap.min.js
// ==/UserScript==

function PageAutomator() {
  var Mousetrap = require("mousetrap");

  // Mouse events
  mouse: {
    this.hover = function (selector) {
      var element = document.querySelector(selector);
      element.dispatchEvent(new MouseEvent("mouseover"));
    };

    this.click = function (selector, button) {
      var element = document.querySelector(selector);
      if (button === "left") {
        element.dispatchEvent(new MouseEvent("click"));
      } else if (button === "right") {
        element.dispatchEvent(new MouseEvent("contextmenu"));
      }
    };

    this.scroll = function (amount) {
      window.scrollBy(0, amount);
    };

    this.scrollTo = function (element) {
      element.scrollIntoView({
        behavior: "smooth",
        block: "start",
        inline: "nearest",
      });
    };

    this.hold = function (selector, button) {
      var element = document.querySelector(selector);
      if (button === "left") {
        element.dispatchEvent(new MouseEvent("mousedown"));
      } else if (button === "right") {
        element.dispatchEvent(
          new MouseEvent("mousedown", {
            button: 2,
          })
        );
      }
    };

    this.moveToPosition = function (x, y) {
      window.dispatchEvent(
        new MouseEvent("mousemove", {
          clientX: x,
          clientY: y,
        })
      );
    };
    this.moveToElement = function (selector) {
      var element = document.querySelector(selector);
      var rect = element.getBoundingClientRect();
      var x = rect.left + rect.width / 2;
      var y = rect.top + rect.height / 2;
      this.moveToPosition(x, y);
    };

    this.getPosition = function () {
      var position = {
        x: 0,
        y: 0,
      };
      document.addEventListener("mousemove", function (event) {
        position.x = event.clientX;
        position.y = event.clientY;
      });
      return position;
    };
  }

  // Keyboard events
  keyboard: {
    this.keyPress = function (key) {
      var event = new KeyboardEvent("keypress", {
        key: key,
      });
      document.dispatchEvent(event);
    };

    this.keyUp = function (key) {
      var event = new KeyboardEvent("keyup", {
        key: key,
      });
      document.dispatchEvent(event);
    };

    this.keyDown = function (key) {
      var event = new KeyboardEvent("keydown", {
        key: key,
      });
      document.dispatchEvent(event);
    };

    this.holdKey = function (key, action) {
      var keys = {
        ctrl: 17,
        shift: 16,
        alt: 18,
        win: 91,
      };
      var event = new KeyboardEvent("keydown", {
        keyCode: keys[key],
        which: keys[key],
      });
      document.dispatchEvent(event);
      action();
      var event = new KeyboardEvent("keyup", {
        keyCode: keys[key],
        which: keys[key],
      });
      document.dispatchEvent(event);
    };

    this.holdKeySequence = function (sequence, action) {
      Mousetrap.bind(
        sequence,
        function () {
          action();
          Mousetrap.unbind(sequence);
        },
        "keydown"
      );
    };

    this.setKeyState = function (key, state) {
      if (key === "numlock") {
        var event = new KeyboardEvent("keydown", {
          key: "NumLock",
          code: "NumLock",
        });
        document.dispatchEvent(event);
      } else if (key === "scrolllock") {
        var event = new KeyboardEvent("keydown", {
          key: "ScrollLock",
          code: "ScrollLock",
        });
        document.dispatchEvent(event);
      } else if (key === "capslock") {
        var event = new KeyboardEvent("keydown", {
          key: "CapsLock",
          code: "CapsLock",
        });
        document.dispatchEvent(event);
      }
    };
  }

  input: {
    // Block input
    this.blockInput = function () {
      document.addEventListener("keydown", function (event) {
        event.preventDefault();
      });
      document.addEventListener("mousedown", function (event) {
        event.preventDefault();
      });
    };
  }

  timer: {
    // Timer events
    this.wait = function (ms) {
      var start = new Date().getTime();
      var end = start;
      while (end < start + ms) {
        end = new Date().getTime();
      }
    };

    this.waitForElement = function (selector) {
      var element = document.querySelector(selector);
      while (!element) {
        element = document.querySelector(selector);
      }
    };

    this.waitForMouse = function (cursor) {
      var currentCursor = document.body.style.cursor;
      while (currentCursor !== cursor) {
        currentCursor = document.body.style.cursor;
      }
    };
  }

  // Conditionals
  this.ifElement = function (selector, condition, value) {
    var element = document.querySelector(selector);
    if (condition === "contains") {
      if (element.innerHTML.includes(value)) {
        return true;
      } else {
        return false;
      }
    } else if (condition === "does not contain") {
      if (!element.innerHTML.includes(value)) {
        return true;
      } else {
        return false;
      }
    } else if (condition === "is") {
      if (element.innerHTML === value) {
        return true;
      } else {
        return false;
      }
    } else if (condition === "is not") {
      if (element.innerHTML !== value) {
        return true;
      } else {
        return false;
      }
    }
  };
  
  dialogs: {
    // Dialogs/Message Boxes
    this.showNotification = function (title, text) {
      var notification = new Notification(title, {
        body: text,
      });
    };

    this.showDialog = function (title, text) {
      var dialog = document.createElement("dialog");
      var titleElement = document.createElement("strong");
      titleElement.innerHTML = title;
      var textElement = document.createElement("p");
      textElement.innerHTML = text;
      dialog.appendChild(titleElement);
      dialog.appendChild(textElement);
      document.body.appendChild(dialog);
      dialog.show();
    };

    this.showCustomDialog = function (html) {
      var dialog = document.createElement("dialog");
      dialog.innerHTML = html;
      document.body.appendChild(dialog);
      dialog.show();
    };
  }

  clipboard: {
    // Clipboard
    this.getClipboardText = function () {
      return navigator.clipboard.readText().then((text) => {
        return text;
      });
    };

    this.setClipboardText = function (text) {
      navigator.clipboard.writeText(text);
    };

    this.clearClipboard = function () {
      navigator.clipboard.writeText("");
    };
  }
}