lucianjp-library

Core library to handle webpages dom with userscripts

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.greatest.deepsurf.us/scripts/464986/1182404/lucianjp-library.js

  1. // ==UserScript==
  2. // @name lucianjp-library
  3. // @namespace lucianjp
  4. // @version 1.0.0
  5. // @author lucianjp
  6. // @grant GM_addStyle
  7. // @description Core library to handle webpages dom with userscripts
  8. // ==/UserScript==
  9.  
  10. class LucianJP_Menu {
  11. constructor() {
  12. this.menuContainer = null;
  13. this.isActive = true;
  14. }
  15. get container() {
  16. return this.menuContainer = this.menuContainer || document.querySelector('.lucianjp-menu') || this.createMenuContainer();
  17. }
  18.  
  19. createMenuContainer() {
  20. this.menuContainer = document.createElement('div');
  21. this.menuContainer.classList.add('lucianjp-menu');
  22. document.body.appendChild(this.menuContainer);
  23.  
  24. // Handle shift keypress to show/hide the menu
  25. document.addEventListener('keydown', (event) => {
  26. if (event.key === 'Shift') {
  27. this.toggleMenu();
  28. }
  29. });
  30.  
  31. return this.menuContainer;
  32. }
  33.  
  34. add(text, onClick) {
  35. if (typeof onClick !== 'function') {
  36. console.error('LucianJP_Menu.add(): onClick argument must be a function.');
  37. return;
  38. }
  39.  
  40. const menuItem = document.createElement('div');
  41. menuItem.innerText = text;
  42. menuItem.classList.add('lucianjp-menu-item');
  43. menuItem.addEventListener('click', onClick);
  44. this.container.appendChild(menuItem);
  45. }
  46.  
  47. toggleMenu() {
  48. this.isActive = !this.isActive;
  49. this.container.classList.toggle('lucianjp-menu-close', !this.isActive);
  50. }
  51. }
  52.  
  53. const lucianjp = {
  54. observe: (t, cb, once = false) => {
  55. let complete = true;
  56.  
  57. const disconnectHandler = VM.observe(document, () => {
  58. if (!complete) return true;
  59. complete = false;
  60. const node = document.querySelector(t);
  61.  
  62. if (node) {
  63. cb(node);
  64. return true;
  65. }
  66.  
  67. complete = true;
  68. });
  69. if (once) ready(disconnectHandler);
  70. },
  71. ready: callback => {
  72. if (document.readyState != "loading") callback();
  73. else document.addEventListener("DOMContentLoaded", callback);
  74. },
  75.  
  76. menu: new LucianJP_Menu()
  77. }
  78.  
  79. // Add styles
  80. GM_addStyle(`
  81. .lucianjp-menu {
  82. position: fixed;
  83. right: -6px;
  84. top: 50%;
  85. transform: translateY(-50%);
  86. z-index: 9999;
  87. transition: transform 0.3s ease-in-out;
  88. }
  89.  
  90. .lucianjp-menu-close {
  91. transform: translateX(calc(100% - 20px)) translateY(-50%);
  92. }
  93.  
  94. .lucianjp-menu-item {
  95. padding: 8px 16px;
  96. font-size: 14px;
  97. font-weight: bold;
  98. cursor: pointer;
  99. color: #fff;
  100. background: #007aff;
  101. border-radius: 8px;
  102. box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  103. margin: 8px 0;
  104. }
  105.  
  106. .lucianjp-menu-item:hover {
  107. background: #0066cc;
  108. }
  109. `);