CMB - Custom Mpp Buttons by gtnntg

Custom buttons for Multiplayer Piano with panel navigation

当前为 2024-02-06 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name CMB - Custom Mpp Buttons by gtnntg
  3. // @name:ru КПМ - Настраиваемые кнопки Mpp от gtnntg
  4. // @version 0.0.1
  5. // @description Custom buttons for Multiplayer Piano with panel navigation
  6. // @description:ru Настраиваемые кнопки для Multiplayer Piano с системой навигации по панелям
  7. // @author gtnntg
  8. // @license GPL-3.0-or-later
  9. // @namespace https://vscode.dev/?connectTo=tampermonkey
  10. // @match *://multiplayerpiano.org/*
  11. // @match *://multiplayerpiano.net/*
  12. // @match *://piano.ourworldofpixels.com/*
  13. // @match *://mppclone.com/*
  14. // @match *://playground-mpp.hyye.tk/*
  15. // @match *://rgbmpp.qwerty0301.repl.co/*
  16. // @match *://mpp.hyye.tk/*
  17. // @grant GM_setValue
  18. // @grant GM_getValue
  19. // ==/UserScript==
  20.  
  21. /*---------[Author info]-------------
  22. [discord: gtnntg]
  23. [e-mail: developer.georgiyshvedov@mail.ru] <-- best
  24. [github: https://github.com/gtnntg2009]
  25. --------------------------------------*/
  26.  
  27. /*---------[RU:info]------------
  28. настоящая версия скрипта: 0.5.1
  29.  
  30. Лицензия и авторское право:
  31. Copyright (C) 2023 Georgiy Shvedov (developer.georgiyshvedov@mail.ru)
  32.  
  33. Эта программа является свободным программным обеспечением: вы можете распространять ее и/или модифицировать
  34. ее в соответствии с условиями GNU General Public License, опубликованной Free Software Foundation,
  35. в версии 3 лицензии или (по вашему выбору) любой последующей версии.
  36.  
  37. Эта программа распространяется в надежде, что она будет полезной,
  38. но БЕЗ КАКИХ-ЛИБО ГАРАНТИЙ; даже без подразумеваемой гарантии
  39. ТОВАРНОГО ВИДА или ПРИГОДНОСТИ ДЛЯ ОПРЕДЕЛЕННЫХ ЦЕЛЕЙ. См.
  40. GNU General Public License для получения более подробных сведений.
  41.  
  42. Вы должны были получить копию GNU General Public License
  43. вместе с этой программой. Если нет, см.
  44. <https://www.gnu.org/licenses/>.
  45. -----------------------------*/
  46.  
  47. /*---------[EN:info]------------
  48. Current script version: 0.5.1
  49.  
  50. License and Copyright:
  51. Copyright (C) 2024 Georgiy Shvedov (developer.georgiyshvedov@mail.ru)
  52.  
  53. This program is free software: you can redistribute it and/or modify
  54. it under the terms of the GNU General Public License as published by
  55. the Free Software Foundation, either version 3 of the License, or
  56. (at your option) any later version.
  57.  
  58. This program is distributed in the hope that it will be useful,
  59. but WITHOUT ANY WARRANTY; without even the implied warranty of
  60. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  61. GNU General Public License for more details.
  62.  
  63. You should have received a copy of the GNU General Public License
  64. along with this program. If not, see <https://www.gnu.org/licenses/>.
  65. -----------------------------*/
  66.  
  67. //-------script-----------
  68. (function() {
  69. 'use strict';
  70.  
  71. const settingsKey = 'customMppButtonsSettings';
  72. const defaultSettings = {
  73. panels: [
  74. {
  75. title: 'Panel 1',
  76. left: '10px',
  77. top: '50px',
  78. categories: [
  79. {
  80. title: '1',
  81. buttons: [
  82. { name: 'Button 1', action: () => sendChat('Button 1.1 pressed') },
  83. { name: 'Button 2', action: () => sendChat('Button 1.2 pressed') }
  84. ]
  85. },
  86. /*
  87. {
  88. title: 'name for Category',
  89. buttons: [
  90. { name: 'name for button', action: () => you.function() },
  91. { name: 'name for button', action: () => you.function() }, <-- , for more
  92. { name: 'name for button', action: () => you.function() } <-- no ,
  93. ]
  94. },
  95. */
  96. {
  97. title: '2',
  98. buttons: [
  99. { name: 'Button 1', action: () => sendChat('Button 2.1 pressed') },
  100. { name: 'Button 2', action: () => sendChat('Button 2.2 pressed') }
  101. ]
  102. },
  103. {
  104. title: '3',
  105. buttons: [
  106. { name: 'Button 1', action: () => sendChat('Button 3.1 pressed') },
  107. { name: 'Button 2', action: () => sendChat('Button 3.2 pressed') }
  108. ]
  109. }
  110. ]
  111. },//the end panel 1
  112. {
  113. title: 'Panel 2',
  114. left: 'calc(10px + 200px + 100px)', // Отступ от первой панели + ширина первой панели + 100 пикселей
  115. top: '50px',
  116. categories: [
  117. {
  118. title: 'Category 1',
  119. buttons: [
  120. { name: 'Button 1', action: () => sendChat('Button 1.1 pressed') },
  121. { name: 'Button 2', action: () => sendChat('Button 1.2 pressed') }
  122. ]
  123. },//the end for Category 1
  124. {
  125. title: 'Category 2',
  126. buttons: [
  127. { name: 'Button 1', action: () => sendChat('Button 2.1 pressed') },
  128. { name: 'Button 2', action: () => sendChat('Button 2.2 pressed') }
  129. ]//the end for buttons in Category 2
  130. }//the end for Category 2
  131. ] //the end categories
  132. }//the end panel 2 none ,
  133. ]
  134. };
  135.  
  136. const storedSettings = loadSettings();
  137.  
  138. const dragHandleHeight = 20;
  139. const dragHandleColor = 'black';
  140.  
  141. // Создаем кнопку для открытия панели 1
  142. const togglePanel1Button = createToggleButton('Open Panel 1', '10px', '510px', () => togglePanel('Panel 1'));
  143. document.body.appendChild(togglePanel1Button);
  144.  
  145. // Создаем кнопку для открытия панели 2
  146. const togglePanel2Button = createToggleButton('Open Panel 2', '10px', '460px', () => togglePanel('Panel 2'));
  147. document.body.appendChild(togglePanel2Button);
  148.  
  149. /* Создаем кнопку для открытия панели 3
  150. const togglePanel3Button = createToggleButton('Open Panel 3', '10px', '?px', () => togglePanel('Panel 3'));
  151. document.body.appendChild(togglePanel3Button);
  152. */
  153. // Создаем кнопку для закрытия панели
  154. const closePanelButton = createToggleButton('Close Panel', '10px', '600px', closeMenu);
  155. document.body.appendChild(closePanelButton);
  156.  
  157. // Создаем всплывающее меню
  158. const popupMenu = createPopupMenu();
  159. document.body.appendChild(popupMenu);
  160.  
  161. // Создаем квадратик для перетаскивания
  162. const dragHandle = createDragHandle();
  163. popupMenu.appendChild(dragHandle);
  164.  
  165. function createToggleButton(title, left, bottom, clickHandler) {
  166. const button = document.createElement('button');
  167. button.innerHTML = title;
  168. button.style.position = 'fixed';
  169. button.style.bottom = bottom;
  170. button.style.left = left;
  171. button.style.zIndex = '10000';
  172. button.addEventListener('click', clickHandler);
  173.  
  174. setStyles(button, {
  175. backgroundColor: 'rgba(0, 0, 0, 0.7)',
  176. color: 'white',
  177. border: 'none',
  178. borderRadius: '5px',
  179. padding: '15px',
  180. cursor: 'pointer',
  181. fontSize: '16px',
  182. });
  183.  
  184. return button;
  185. }
  186.  
  187. function createPopupMenu() {
  188. const popupMenu = document.createElement('div');
  189. popupMenu.id = 'popup-menu';
  190. popupMenu.style.display = 'none';
  191.  
  192. setStyles(popupMenu, {
  193. position: 'fixed',
  194. top: '50px',
  195. left: '250px',
  196. width: '200px',
  197. backgroundColor: 'rgba(0, 0, 0, 0.7)',
  198. border: '1px solid #ccc',
  199. borderRadius: '5px',
  200. padding: '15px',
  201. boxShadow: '0 0 10px rgba(0, 0, 0, 0.2)',
  202. zIndex: '9999',
  203. color: 'white',
  204. userSelect: 'none', // Запрещаем выделение текста
  205. });
  206.  
  207. return popupMenu;
  208. }
  209.  
  210. function createDragHandle() {
  211. const dragHandle = document.createElement('div');
  212. dragHandle.id = 'drag-handle';
  213.  
  214. setStyles(dragHandle, {
  215. position: 'absolute',
  216. top: '0',
  217. left: '0',
  218. right: '0',
  219. height: `${dragHandleHeight}px`,
  220. backgroundColor: dragHandleColor,
  221. cursor: 'grab',
  222. });
  223.  
  224. dragHandle.addEventListener('mousedown', startDrag);
  225.  
  226. return dragHandle;
  227. }
  228.  
  229. function setStyles(element, styles) {
  230. for (const property in styles) {
  231. element.style[property] = styles[property];
  232. }
  233. }
  234.  
  235. let isDragging = false;
  236. let offsetX, offsetY;
  237.  
  238. function startDrag(e) {
  239. isDragging = true;
  240. offsetX = e.clientX - popupMenu.offsetLeft;
  241. offsetY = e.clientY - popupMenu.offsetTop;
  242. document.addEventListener('mousemove', handleDrag);
  243. document.addEventListener('mouseup', stopDrag);
  244. popupMenu.style.cursor = 'grabbing';
  245. }
  246.  
  247. function handleDrag(e) {
  248. if (isDragging) {
  249. const x = e.clientX - offsetX;
  250. const y = e.clientY - offsetY;
  251. popupMenu.style.left = `${x}px`;
  252. popupMenu.style.top = `${y}px`;
  253. }
  254. }
  255.  
  256. function stopDrag() {
  257. isDragging = false;
  258. document.removeEventListener('mousemove', handleDrag);
  259. document.removeEventListener('mouseup', stopDrag);
  260. popupMenu.style.cursor = 'grab';
  261. }
  262.  
  263. function togglePanel(panelTitle) {
  264. closeMenu();
  265. clearCategories();
  266. const panel = storedSettings.panels.find(panel => panel.title === panelTitle);
  267. openMenu(panel);
  268. }
  269.  
  270. function openMenu(panel) {
  271. popupMenu.style.display = 'block';
  272. addCategories(panel.categories, panel.title);
  273. }
  274.  
  275. function closeMenu() {
  276. popupMenu.style.display = 'none';
  277. }
  278.  
  279. function addCategories(categories, panelTitle) {
  280. const panelNumber = storedSettings.panels.findIndex(panel => panel.title === panelTitle) + 1;
  281. categories.forEach(category => {
  282. const categoryContainer = document.createElement('div');
  283. categoryContainer.className = 'category';
  284.  
  285. const categoryTitleElement = document.createElement('div');
  286. categoryTitleElement.className = 'category-title';
  287. categoryTitleElement.textContent = `${category.title}`;
  288. categoryContainer.appendChild(categoryTitleElement);
  289.  
  290. const categoryButtonsContainer = document.createElement('div');
  291. categoryButtonsContainer.className = 'category-buttons';
  292. addButtonsToContainer(categoryButtonsContainer, category.buttons);
  293.  
  294. categoryContainer.appendChild(categoryButtonsContainer);
  295. popupMenu.appendChild(categoryContainer);
  296. });
  297. }
  298.  
  299. function addButtonsToContainer(container, buttons) {
  300. buttons.forEach(buttonInfo => {
  301. const button = document.createElement('div');
  302. button.className = 'menu-button';
  303. button.textContent = buttonInfo.name;
  304.  
  305. setStyles(button, {
  306. border: '1px solid #ccc',
  307. borderRadius: '5px',
  308. margin: '5px 0',
  309. padding: '5px',
  310. cursor: 'pointer',
  311. backgroundColor: 'rgba(0, 0, 0, 0.5)',
  312. color: 'white'
  313. });
  314.  
  315. button.addEventListener('click', buttonInfo.action);
  316. container.appendChild(button);
  317. });
  318. }
  319.  
  320. function clearCategories() {
  321. const categoryContainers = document.querySelectorAll('.category');
  322. categoryContainers.forEach(container => {
  323. container.innerHTML = '';
  324. });
  325. }
  326.  
  327. function sendChat(message) {
  328. MPP.chat.send(message); //no error
  329. }
  330.  
  331. console.log("Script CMB - Custom Mpp Buttons with Panel Navigation was activated.")
  332. console.log("Script is working now.")
  333. console.log("RU: Скрипт КПМ - Настраиваемые кнопки для Multiplayer Piano с системой навигации по панелям был активирован.")
  334. console.log("RU: Скрипт работает сейчас.")
  335.  
  336. function saveSettings(settings) {
  337. localStorage.setItem(settingsKey, JSON.stringify(settings));
  338. }
  339.  
  340. function loadSettings() {
  341. const storedSettingsJson = localStorage.getItem(settingsKey);
  342. return storedSettingsJson ? JSON.parse(storedSettingsJson) : defaultSettings;
  343. }
  344.  
  345. })();