Advertisement
MeKLiN2

Untitled

Nov 10th, 2024
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.78 KB | None | 0 0
  1. // ==UserScript==
  2. // @name user table with resizable and collapsible items
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Intercept WebSocket messages on StumbleChat and display them in a resizable and collapsible table overlay
  6. // @author MeKLiN
  7. // @match https://stumblechat.com/room/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=stumblechat.com
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. let pingCount = 0;
  17. let messageSections = {}; // Store sections by handle
  18. let handleMessages = {}; // Track handles and associated messages
  19.  
  20. const reopenButton = document.createElement("button");
  21. reopenButton.textContent = "Open WebSocket Log";
  22. reopenButton.style.position = "fixed";
  23. reopenButton.style.bottom = "10px";
  24. reopenButton.style.left = "10px";
  25. reopenButton.style.padding = "10px 15px";
  26. reopenButton.style.backgroundColor = "#4CAF50";
  27. reopenButton.style.color = "white";
  28. reopenButton.style.border = "none";
  29. reopenButton.style.cursor = "pointer";
  30. reopenButton.style.display = "none";
  31. reopenButton.style.zIndex = "10000";
  32. document.body.appendChild(reopenButton);
  33.  
  34. function createWebSocketOverlay() {
  35. const overlay = document.createElement("div");
  36. overlay.id = "webSocketOverlay";
  37. overlay.style.position = "fixed";
  38. overlay.style.top = "10%";
  39. overlay.style.left = "10%";
  40. overlay.style.fontSize = "12px";
  41. overlay.style.width = "900px";
  42. overlay.style.height = "800px";
  43. overlay.style.backgroundColor = "rgba(0, 0, 0, 0.7)";
  44. overlay.style.zIndex = "9999";
  45. overlay.style.display = "none";
  46. overlay.style.alignItems = "center";
  47. overlay.style.justifyContent = "center";
  48. overlay.style.padding = "10px";
  49. overlay.style.resize = "both";
  50. overlay.style.overflow = "auto";
  51. overlay.style.boxSizing = "border-box";
  52. overlay.style.cursor = "move";
  53.  
  54. let offsetX, offsetY, isDragging = false;
  55.  
  56. overlay.addEventListener('mousedown', (e) => {
  57. isDragging = true;
  58. offsetX = e.clientX - overlay.offsetLeft;
  59. offsetY = e.clientY - overlay.offsetTop;
  60. document.addEventListener('mousemove', dragOverlay);
  61. document.addEventListener('mouseup', () => {
  62. isDragging = false;
  63. document.removeEventListener('mousemove', dragOverlay);
  64. });
  65. });
  66.  
  67. function dragOverlay(e) {
  68. if (isDragging) {
  69. overlay.style.left = (e.clientX - offsetX) + 'px';
  70. overlay.style.top = (e.clientY - offsetY) + 'px';
  71. }
  72. }
  73.  
  74. const closeButton = document.createElement("button");
  75. closeButton.textContent = "Close";
  76. closeButton.style.position = "absolute";
  77. closeButton.style.top = "10px";
  78. closeButton.style.right = "10px";
  79. closeButton.style.padding = "5px 10px";
  80. closeButton.style.backgroundColor = "#ff4b5c";
  81. closeButton.style.color = "white";
  82. closeButton.style.border = "none";
  83. closeButton.style.cursor = "pointer";
  84. closeButton.addEventListener('click', () => {
  85. overlay.style.display = "none";
  86. reopenButton.style.display = "inline-block";
  87. });
  88. overlay.appendChild(closeButton);
  89.  
  90. const table = document.createElement("table");
  91. table.style.width = "100%";
  92. table.style.backgroundColor = "white";
  93. table.style.borderCollapse = "collapse";
  94. overlay.appendChild(table);
  95.  
  96. document.body.appendChild(overlay);
  97.  
  98. return { overlay, table };
  99. }
  100.  
  101. function createCollapsibleSection(handle, content, symbol) {
  102. const section = document.createElement("tr");
  103. const header = document.createElement("td");
  104. header.colSpan = 2;
  105. header.style.padding = "10px";
  106. header.style.cursor = "pointer";
  107. header.style.backgroundColor = "#f2f2f2";
  108. header.innerHTML = `<strong style="color: red;">${symbol}</strong> ${handle}`;
  109.  
  110. header.addEventListener("click", () => {
  111. const contentRow = section.querySelector(".contentRow");
  112. contentRow.style.display = contentRow.style.display === "none" ? "table-row" : "none";
  113. });
  114.  
  115. section.appendChild(header);
  116.  
  117. const contentRow = document.createElement("tr");
  118. contentRow.classList.add("contentRow");
  119. contentRow.style.display = "none";
  120. const contentCell = document.createElement("td");
  121. contentCell.colSpan = 2;
  122. contentCell.style.padding = "10px";
  123. contentCell.style.whiteSpace = "pre-wrap";
  124. contentCell.textContent = content;
  125.  
  126. contentRow.appendChild(contentCell);
  127. section.appendChild(contentRow);
  128.  
  129. return section;
  130. }
  131.  
  132. function displayWebSocketMessage(message, table) {
  133. const data = JSON.parse(message);
  134.  
  135. if (message === "0") {
  136. if (!messageSections["0"]) {
  137. const section = createCollapsibleSection("SYSMSG: 0 ", "🟢 Total Pings: " + pingCount);
  138. table.appendChild(section);
  139. messageSections["0"] = section;
  140. }
  141.  
  142. pingCount++;
  143. const contentRow = messageSections["0"].querySelector(".contentRow td");
  144. let pingLabel = contentRow.querySelector("span");
  145. if (pingLabel) {
  146. pingLabel.textContent = "🟢 Total Pings: " + pingCount;
  147. } else {
  148. pingLabel = document.createElement("span");
  149. pingLabel.textContent = "🟢 Total Pings: " + pingCount;
  150. contentRow.appendChild(pingLabel);
  151. }
  152. } else {
  153. const handle = data.handle;
  154. let symbol = "🔵";
  155.  
  156. switch (data.stumble) {
  157. case "sysmsg":
  158. symbol = "🟢";
  159. break;
  160. case "join":
  161. case "msg":
  162. symbol = "🔵";
  163. break;
  164. }
  165.  
  166. // Check if a section for this handle exists
  167. if (!messageSections[handle]) {
  168. const section = createCollapsibleSection(handle, JSON.stringify(data, null, 2), symbol);
  169. table.appendChild(section);
  170. messageSections[handle] = section; // Store the reference to this handle's section
  171. handleMessages[handle] = [data]; // Store the first message for this handle
  172. } else {
  173. // If the section exists, append the new message to the existing section
  174. const section = messageSections[handle];
  175. const contentRow = section.querySelector(".contentRow td");
  176. contentRow.textContent += `\n${JSON.stringify(data, null, 2)}`;
  177. handleMessages[handle].push(data); // Append the new message to the handle's list
  178. }
  179. }
  180. }
  181.  
  182. const originalWebSocket = window.WebSocket;
  183. window.WebSocket = function(url, protocols) {
  184. console.log('WebSocket URL:', url);
  185.  
  186. const ws = new originalWebSocket(url, protocols);
  187.  
  188. const { overlay, table } = createWebSocketOverlay();
  189.  
  190. ws.addEventListener('message', event => {
  191. displayWebSocketMessage(event.data, table);
  192. overlay.style.display = "flex";
  193. reopenButton.style.display = "none";
  194. });
  195.  
  196. reopenButton.addEventListener('click', () => {
  197. overlay.style.display = "flex";
  198. reopenButton.style.display = "none";
  199. });
  200.  
  201. return ws;
  202. };
  203.  
  204. })();
  205.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement