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