Greasy Fork is available in English.

MPP Custom Tags

MPP Custom Tags (MPPCT)

  1. // ==UserScript==
  2. // @name MPP Custom Tags
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.9.3
  5. // @description MPP Custom Tags (MPPCT)
  6. // @author НУУЕ (discord - hyye.xyz)
  7. // @match *://multiplayerpiano.net/*
  8. // @match *://multiplayerpiano.org/*
  9. // @match *://www.multiplayerpiano.org/*
  10. // @license MIT
  11. // @icon https://www.google.com/s2/favicons?sz=64&domain=multiplayerpiano.net
  12. // ==/UserScript==
  13.  
  14. /* globals MPP */
  15.  
  16. console.log('%cLoaded MPPCT! uwu','color:orange; font-size:15px;');
  17. if (!localStorage.tag) localStorage.tag = JSON.stringify({ text: 'None', color: '#000000' });
  18. if (!localStorage.knownTags) localStorage.knownTags = JSON.stringify({});
  19.  
  20. const ver = '1.9.3';
  21. const knownTags = JSON.parse(localStorage.knownTags)
  22.  
  23. let tag = JSON.parse(localStorage.tag);
  24.  
  25. MPP.client.on('hi', () => {
  26. MPP.client.sendArray([{m: '+custom'}]);
  27. });
  28.  
  29. function gradientTest(gradient) {
  30. if (!gradient) return false;
  31.  
  32. const gradients = ['linear-gradient', 'radial-gradient', 'repeating-radial-gradient', 'conic-gradient', 'repeating-conic-gradient'];
  33.  
  34. let gradientAllowed = false;
  35.  
  36. if (!gradient.includes('"') && !gradient.includes(';') && !gradient.includes(':')) {
  37. gradients.forEach((Gradient) => {
  38. if (gradient.startsWith(Gradient)) {
  39. if (gradientAllowed) return;
  40. else gradientAllowed = true;
  41. }
  42. });
  43. }
  44.  
  45. return gradientAllowed;
  46. }
  47.  
  48. function updtag(text, color, _id, gradient) {
  49. if (text.length > 50) return;
  50. if (!MPP.client.ppl[_id]) return;
  51. if (document.getElementById(`nametag-${_id}`) != null) {
  52. document.getElementById(`nametag-${_id}`).remove();
  53. }
  54. knownTags[_id] = { text: text, color: color };
  55. let tagDiv = document.createElement('div')
  56. tagDiv.className = 'nametag';
  57. tagDiv.id = `nametag-${_id}`;
  58. tagDiv.style['background-color'] = color;
  59. if (gradientTest(gradient)) {
  60. tagDiv.style.background = gradient;
  61. knownTags[_id].gradient = gradient;
  62. }
  63. localStorage.knownTags = JSON.stringify(knownTags);
  64. tagDiv.innerText = text;
  65. document.getElementById(`namediv-${_id}`).prepend(tagDiv);
  66. document.getElementById(`namediv-${_id}`).title = 'This is an MPPCT user.';
  67. }
  68.  
  69.  
  70. function sendTag(id) {
  71. if (id) MPP.client.sendArray([{ m: 'custom', data: { m: 'mppct', text: tag.text, color: tag.color, gradient: tag.gradient }, target: { mode: 'id', id: id } }]);
  72. else MPP.client.sendArray([{m: 'custom', data: {m: 'mppct', text: tag.text, color: tag.color, gradient: tag.gradient}, target: { mode: 'subscribed' } }]);
  73. }
  74. function askForTags() {
  75. MPP.client.sendArray([{ m: 'custom', data: { m: 'mppctreq' }, target: { mode: 'subscribed' } }]);
  76. }
  77.  
  78. MPP.client.on('custom', (c) => {
  79. const tag = c.data;
  80. if (tag.m == 'mppct') {
  81. if (typeof tag.text == 'string' &&
  82. (typeof tag.color == 'string' ||
  83. tag.gradient == 'string')
  84. ) {
  85. if (MPP.client.ppl[c.p]) {
  86. updtag(tag.text || 'None', tag.color || '#000000', c.p, tag.gradient);
  87. }
  88. }
  89. }
  90. if (tag.m == 'mppctreq') {
  91. if (MPP.client.ppl[c.p]) sendTag(c.p);
  92. }
  93. });
  94. MPP.client.on('p', (p) => {
  95. if (p._id == MPP.client.getOwnParticipant()._id) {
  96. updtag(tag.text, tag.color, MPP.client.getOwnParticipant()._id, tag.gradient);
  97. sendTag();
  98. }
  99. });
  100. MPP.client.on('c', () => {
  101. askForTags();
  102. sendTag();
  103. updtag(tag.text, tag.color, MPP.client.getOwnParticipant()._id, tag.gradient);
  104. });
  105.  
  106. // Tags in chat
  107. function addChatTag(msg) {
  108. if (msg.m == 'dm') return;
  109. if (!knownTags[msg.p._id]) return;
  110. let aTag;
  111. if (msg.p._id == MPP.client.getOwnParticipant()._id) aTag = tag;
  112. else aTag = knownTags[msg.p._id];
  113.  
  114. let chatMessage = document.getElementById('msg-' + msg.id);
  115. let Span = document.createElement('span'); // <span style="background-color: ${aTag.color};color:#ffffff;" class="nametag">${aTag.text}</span>
  116. Span.style['background-color'] = aTag.color;
  117. if (knownTags[msg.p._id]) Span.style.background = aTag.gradient;
  118. Span.style.color = '#ffffff';
  119. Span.className = 'nametag';
  120. Span.innerText = aTag.text;
  121. chatMessage.appendChild(Span);
  122. }
  123.  
  124. MPP.client.on('a', (msg) => {
  125. addChatTag(msg);
  126. });
  127.  
  128. MPP.client.on('c', (msg) => {
  129. if (!Array.isArray(msg.c)) return;
  130. msg.c.forEach((msg) => {
  131. addChatTag(msg);
  132. });
  133. });
  134.  
  135.  
  136. // Buttons
  137. const container = document.body.getElementsByClassName('dialog').rename;
  138.  
  139. const a = document.createElement('input');
  140. a.id = 'tagtext';
  141. a.name = 'tagtext';
  142. a.type = 'text';
  143. a.placeholder = 'Tag';
  144. a.maxLength = '50';
  145. a.className = 'text';
  146. a.style = 'width: 100px; height: 20px;';
  147. container.appendChild(a);
  148.  
  149. const q = document.createElement('input');
  150. q.id = 'tagcolor';
  151. q.name = 'tagcolor';
  152. q.type = 'color';
  153. q.placeholder = '';
  154. q.maxlength = '7';
  155. q.className = 'color';
  156. container.appendChild(q);
  157.  
  158. const e = document.createElement('button');
  159. e.addEventListener('click', () => {
  160. let tagtext = document.getElementById('tagtext');
  161. let tagcolor = document.getElementById('tagcolor');
  162. if (tagtext.value == '') return;
  163. let newTag = JSON.parse(localStorage.tag);
  164. newTag.text = tagtext.value || 'None';
  165. newTag.color = tagcolor.value || '#000000';
  166. localStorage.tag = JSON.stringify(newTag);
  167. tag = newTag;
  168. updtag(newTag.text, newTag.color, MPP.client.getOwnParticipant()._id, newTag.gradient);
  169. sendTag();
  170. });
  171. e.innerText = 'SET TAG';
  172. e.className = 'top-button';
  173. e.style.position = 'fixed';
  174. e.style.height = '30px';
  175. container.appendChild(e);
  176.  
  177. document.getElementById('tagtext').value = tag.text;
  178. document.getElementById('tagcolor').value = tag.color;
  179.  
  180.  
  181. //Version checker
  182. function checkVersion() {
  183. fetch('https://raw.githubusercontent.com/Hyye123/MPPCT/main/version.json').then(r => r.json().then(json => {
  184. if (ver != json.latest) {
  185. setInterval(function() {
  186. MPP.chat.receive({
  187. "m": "a",
  188. "t": Date.now(),
  189. "a": "Please update MPPCT https://greasyfork.org/ru/scripts/455137-mpp-custom-tags",
  190. "p": {
  191. "_id": "MPPCT",
  192. "name": "MPPCT (eng)",
  193. "color": "#ffffff",
  194. "id": "MPPCT"
  195. }
  196. });
  197. MPP.chat.receive({
  198. "m": "a",
  199. "t": Date.now(),
  200. "a": "Пожалуйста обновите MPPCT https://greasyfork.org/ru/scripts/455137-mpp-custom-tags",
  201. "p": {
  202. "_id": "MPPCT",
  203. "name": "MPPCT (rus)",
  204. "color": "#ffffff",
  205. "id": "MPPCT"
  206. }
  207. });
  208. }, 30000);
  209. }
  210. }));
  211. }
  212. setInterval(checkVersion, 300000);
  213. setTimeout(checkVersion, 10000);