MPP - AdvancedChatLogger

8/6/2022, 1:13:53 AM

目前为 2022-08-07 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name MPP - AdvancedChatLogger
  3. // @namespace Violentmonkey Scripts
  4. // @match https://mppclone.com/*
  5. // @grant none
  6. // @version 1.1
  7. // @author Wolfy0615
  8. // @description 8/6/2022, 1:13:53 AM
  9. // ==/UserScript==
  10.  
  11. function init() {
  12. let logger = {
  13. messages: [],
  14. log: (msg) => {
  15. this.messages.push(msg + "\n");
  16. },
  17. clear: () => {
  18. this.messages = [];
  19. }
  20. };
  21.  
  22. const protocol = {
  23. chatList: "c",
  24. chat: "a",
  25. directMessage: "dm",
  26. channelUpdate: "ch"
  27. };
  28.  
  29. const client = MPP.client;
  30. let lastChannel = "";
  31.  
  32. client.on(protocol.channelUpdate, (msg) => {
  33. if (lastChannel === msg.ch.id) return;
  34. logger.clear();
  35. console.log("Detected room change. Clearing logs...");
  36. lastChannel = msg.ch.id;
  37. });
  38. client.on(protocol.chatList, (msg) => {
  39. if (msg.c) {
  40. msg.c.forEach((cmsg) => {
  41. if (cmsg.m === "dm") {
  42. const time = new Date(cmsg.t).toLocaleTimeString();
  43. const sender = cmsg.sender;
  44. const senderId = sender.id;
  45. const senderName = sender.name;
  46. const recipient = cmsg.recipient;
  47. const recipientId = recipient.id;
  48. const recipientName = recipient.name;
  49.  
  50. if (senderId === client.getOwnParticipant().id) {
  51. logger.log(
  52. `${time} ${senderId} (DM) ${senderName} to ${recipientId} ${recipientName}: ${cmsg.a}`
  53. );
  54. } else {
  55. logger.log(
  56. `${time} ${recipientId} (DM) ${recipientName} from ${senderId} ${senderName}: ${cmsg.a}`
  57. );
  58. }
  59. } else {
  60. const time = new Date(cmsg.t).toLocaleTimeString();
  61. logger.log(
  62. `${time} ${cmsg.p.id} (--) ${cmsg.p.name}: ${cmsg.a}`
  63. );
  64. }
  65. });
  66. }
  67. });
  68.  
  69. client.on(protocol.chat, (msg) => {
  70. const time = new Date(msg.t).toLocaleTimeString();
  71. logger.log(`${time} ${msg.p.id} (--) ${msg.p.name}: ${msg.a}`);
  72. });
  73.  
  74. client.on(protocol.directMessage, (msg) => {
  75. const time = new Date(msg.t).toLocaleTimeString();
  76. const sender = msg.sender;
  77. const senderId = sender.id;
  78. const senderName = sender.name;
  79. const recipient = msg.recipient;
  80. const recipientId = recipient.id;
  81. const recipientName = recipient.name;
  82.  
  83. if (senderId === client.getOwnParticipant().id) {
  84. logger.log(
  85. `${time} ${senderId} (DM) ${senderName} to ${recipientId} ${recipientName}: ${msg.a}`
  86. );
  87. } else {
  88. logger.log(
  89. `${time} ${recipientId} (DM) ${recipientName} from ${senderId} ${senderName}: ${msg.a}`
  90. );
  91. }
  92. });
  93.  
  94. window.messages = logger.messages;
  95.  
  96. const btn = `<div id="chat-download-btn" class="ugly-button">Download Chat</div>`;
  97.  
  98. $("#bottom .relative").append(btn);
  99.  
  100. $("#chat-download-btn").css({
  101. position: "absolute",
  102. left: "1020px",
  103. top: "32px",
  104. });
  105.  
  106. $("#chat-download-btn").on("click", () => {
  107. let uri = URL.createObjectURL(
  108. new Blob(messages, { type: "text/plain" })
  109. );
  110.  
  111. new MPP.Notification({
  112. id: "chat-download",
  113. class: "classic",
  114. title: "Chat Download",
  115. html: `<a href="${uri}" download="${client.channel.id}">Here</a> is your download.`,
  116. duration: 7000,
  117. target: "#piano",
  118. });
  119. });
  120. }
  121.  
  122. const initInterval = setInterval(() => {
  123. if (window.MPP) {
  124. clearInterval(initInterval);
  125. init();
  126. }
  127. });