createModal

Create-Modal

Tento skript by neměl být instalován přímo. Jedná se o knihovnu, kterou by měly jiné skripty využívat pomocí meta příkazu // @require https://update.greasyfork.org/scripts/528239/1558620/createModal.js

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         createModal
// @namespace    Violentmonkey Scripts
// @description  Create-Modal
// @version      1.1
// @author       maanimis
// @grant        none
// ==/UserScript==

/**
 * createModal
 *
 * @param {string} title
 * @param {string} initialText
 * @param {string} updatedText
 * @param {number} updateDelay
 * @returns {Promise<Element>}
 */
class Modal {
  constructor(title, initialText, updatedText, updateDelay) {
    this.title = title;
    this.initialText = initialText;
    this.updatedText = updatedText;
    this.updateDelay = updateDelay;

    this.createModal();
  }

  createModal() {
    this.modal = document.createElement('div');
    this.modal.id = 'myModal';
    Object.assign(this.modal.style, {
      position: 'fixed',
      zIndex: '1000',
      left: '0',
      top: '0',
      width: '100%',
      height: '100%',
      overflow: 'auto',
      backgroundColor: 'rgba(0, 0, 0, 0.5)',
      justifyContent: 'center',
      alignItems: 'center',
      display: 'flex',
      backdropFilter: 'blur(10px)',
      direction: 'ltr',
    });

    this.modalContent = document.createElement('div');
    Object.assign(this.modalContent.style, {
      backgroundColor: '#fff',
      margin: 'auto',
      padding: '20px',
      border: '1px solid #888',
      width: '80%',
      maxWidth: '500px',
      borderRadius: '5px',
      direction: 'ltr',
    });

    this.closeButton = document.createElement('span');
    this.closeButton.innerHTML = '&times;';
    Object.assign(this.closeButton.style, {
      color: '#aaa',
      float: 'right',
      fontSize: '28px',
      fontWeight: 'bold',
      cursor: 'pointer',
    });
    this.closeButton.onclick = () => this.closeModal();

    this.modalTitle = document.createElement('h2');
    this.modalTitle.innerText = this.title;

    this.modalText = document.createElement('p');
    this.modalText.innerText = this.initialText;

    this.modalContent.appendChild(this.closeButton);
    this.modalContent.appendChild(this.modalTitle);
    this.modalContent.appendChild(this.modalText);
    this.modal.appendChild(this.modalContent);
    document.body.appendChild(this.modal);

    this.openModal();

    if (this.updateDelay) setTimeout(() => this.updateText(), this.updateDelay);

    window.onclick = (event) => {
      if (event.target === this.modal) {
        this.closeModal();
      }
    };
  }

  openModal() {
    this.modal.style.display = 'flex';
  }

  closeModal() {
    this.modal.style.display = 'none';
  }

  updateText() {
    this.modalText.innerText = this.updatedText;
  }
}