[MWI]WebSocket Log Viewer

Intercept and display WebSocket communication data in console with JSON formatting

  1. // ==UserScript==
  2. // @name [MWI]WebSocket Log Viewer
  3. // @name:zh-CN [银河奶牛]WebSocket日志查看器
  4. // @namespace https://cnb.cool/shenhuanjie/skyner-cn/tamper-monkey-script/mwi-websocket-log-viewer
  5. // @version 1.0.0
  6. // @description Intercept and display WebSocket communication data in console with JSON formatting
  7. // @description:zh-CN 拦截WebSocket通信数据并以JSON格式显示在控制台
  8. // @author shenhuanjie
  9. // @license MIT
  10. // @match https://www.milkywayidle.com/game*
  11. // @icon https://www.milkywayidle.com/favicon.svg
  12. // @grant none
  13. // @homepage https://cnb.cool/shenhuanjie/skyner-cn/tamper-monkey-script/mwi-websocket-log-viewer
  14. // @supportURL https://cnb.cool/shenhuanjie/skyner-cn/tamper-monkey-script/mwi-websocket-log-viewer
  15. // @run-at document-start
  16. // @connect *
  17. // ==/UserScript==
  18.  
  19. (function() {
  20. "use strict";
  21.  
  22. // 检查是否存在JSON Formatter库
  23. if (typeof JSONFormatter !== 'undefined') {
  24. console.info("%cJSON Formatter 库已加载", "color: #2196F3; font-weight: bold;");
  25. } else {
  26. console.warn("%cJSON Formatter 库未加载,日志将以普通格式显示", "color: #FF9800; font-weight: bold;");
  27. }
  28.  
  29. // 代理 WebSocket
  30. const OriginalWebSocket = window.WebSocket;
  31. const handlerQueue = [];
  32.  
  33. function MwiWebSocketket(url, protocols) {
  34. const ws = new OriginalWebSocket(url, protocols);
  35.  
  36. ws.addEventListener("message", function(event) {
  37. try {
  38. const msgData = JSON.parse(event.data);
  39. handlerQueue.reduce((prev, handler) => {
  40. return handler(prev);
  41. }, msgData);
  42. } catch (e) {
  43. console.warn("非 JSON 格式消息:", event.data);
  44. }
  45. });
  46.  
  47. ws.addEventListener("open", function() {
  48. console.info("%cWebSocket 连接已建立: %s", "color: #4CAF50; font-weight: bold;", url);
  49. });
  50.  
  51. ws.addEventListener("close", function(event) {
  52. console.info("%cWebSocket 连接已关闭: %s (Code: %d, Reason: %s)",
  53. "color: #F44336; font-weight: bold;",
  54. url, event.code, event.reason);
  55. });
  56.  
  57. ws.addEventListener("error", function(event) {
  58. console.error("%cWebSocket 错误: %s", "color: #F44336; font-weight: bold;", url, event);
  59. });
  60.  
  61. return ws;
  62. }
  63.  
  64. window.WebSocket = MwiWebSocketket;
  65.  
  66. // 动态生成安全RGB颜色的哈希函数
  67. const getDynamicColor = (type) => {
  68. let hash = 0;
  69. for (let i = 0; i < type.length; i++) {
  70. hash = type.charCodeAt(i) + ((hash << 5) - hash);
  71. }
  72. const hue = hash % 360; // 色调范围0-360
  73. return `hsl(${hue}, 65%, 55%)`; // 调整饱和度为65%,亮度为55%,提升深色背景下的可读性
  74. };
  75.  
  76. // 颜色渲染开关,true为启用动态颜色,false为使用默认灰色
  77. const enableColor = false;
  78.  
  79. // 白名单配置,指定不打印的消息类型
  80. const whitelist = ['chat_message_received'];
  81.  
  82. // 美化显示JSON消息
  83. const displayMessageData = (msgData) => {
  84. if (whitelist.includes(msgData.type)) {
  85. return msgData;
  86. }
  87. const timestamp = new Date().toISOString().split('T')[1].substring(0, 12);
  88. const color = enableColor ? getDynamicColor(msgData.type) : '#000000'; // 根据开关选择颜色
  89. console.groupCollapsed(`%c[${timestamp}] ${msgData.type}`, `color: ${color}; font-weight: bold;`);
  90. console.log(msgData);
  91. console.groupEnd();
  92. return msgData;
  93. };
  94.  
  95. // 注册(不可用)消息处理器
  96. handlerQueue.push(displayMessageData);
  97.  
  98. console.info("%cWebSocket 日志查看器已启动", "color: #4CAF50; font-weight: bold;");
  99. console.info("%c所有 WebSocket 消息将在此处显示", "color: #9C27B0; font-weight: bold;");
  100. })();

QingJ © 2025

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