createModal

Create-Modal

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/528239/1558620/createModal.js

  1. // ==UserScript==
  2. // @name createModal
  3. // @namespace Violentmonkey Scripts
  4. // @description Create-Modal
  5. // @version 1.1
  6. // @author maanimis
  7. // @grant none
  8. // ==/UserScript==
  9.  
  10. /**
  11. * createModal
  12. *
  13. * @param {string} title
  14. * @param {string} initialText
  15. * @param {string} updatedText
  16. * @param {number} updateDelay
  17. * @returns {Promise<Element>}
  18. */
  19. class Modal {
  20. constructor(title, initialText, updatedText, updateDelay) {
  21. this.title = title;
  22. this.initialText = initialText;
  23. this.updatedText = updatedText;
  24. this.updateDelay = updateDelay;
  25.  
  26. this.createModal();
  27. }
  28.  
  29. createModal() {
  30. this.modal = document.createElement('div');
  31. this.modal.id = 'myModal';
  32. Object.assign(this.modal.style, {
  33. position: 'fixed',
  34. zIndex: '1000',
  35. left: '0',
  36. top: '0',
  37. width: '100%',
  38. height: '100%',
  39. overflow: 'auto',
  40. backgroundColor: 'rgba(0, 0, 0, 0.5)',
  41. justifyContent: 'center',
  42. alignItems: 'center',
  43. display: 'flex',
  44. backdropFilter: 'blur(10px)',
  45. direction: 'ltr',
  46. });
  47.  
  48. this.modalContent = document.createElement('div');
  49. Object.assign(this.modalContent.style, {
  50. backgroundColor: '#fff',
  51. margin: 'auto',
  52. padding: '20px',
  53. border: '1px solid #888',
  54. width: '80%',
  55. maxWidth: '500px',
  56. borderRadius: '5px',
  57. direction: 'ltr',
  58. });
  59.  
  60. this.closeButton = document.createElement('span');
  61. this.closeButton.innerHTML = '&times;';
  62. Object.assign(this.closeButton.style, {
  63. color: '#aaa',
  64. float: 'right',
  65. fontSize: '28px',
  66. fontWeight: 'bold',
  67. cursor: 'pointer',
  68. });
  69. this.closeButton.onclick = () => this.closeModal();
  70.  
  71. this.modalTitle = document.createElement('h2');
  72. this.modalTitle.innerText = this.title;
  73.  
  74. this.modalText = document.createElement('p');
  75. this.modalText.innerText = this.initialText;
  76.  
  77. this.modalContent.appendChild(this.closeButton);
  78. this.modalContent.appendChild(this.modalTitle);
  79. this.modalContent.appendChild(this.modalText);
  80. this.modal.appendChild(this.modalContent);
  81. document.body.appendChild(this.modal);
  82.  
  83. this.openModal();
  84.  
  85. if (this.updateDelay) setTimeout(() => this.updateText(), this.updateDelay);
  86.  
  87. window.onclick = (event) => {
  88. if (event.target === this.modal) {
  89. this.closeModal();
  90. }
  91. };
  92. }
  93.  
  94. openModal() {
  95. this.modal.style.display = 'flex';
  96. }
  97.  
  98. closeModal() {
  99. this.modal.style.display = 'none';
  100. }
  101.  
  102. updateText() {
  103. this.modalText.innerText = this.updatedText;
  104. }
  105. }
  106.  

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址