MPP - AdvancedChatLogger

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

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