IdlePixel Group Chat

A private group chat panel

目前为 2024-02-21 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name IdlePixel Group Chat
  3. // @namespace lbtechnology.info
  4. // @version 1.0.0
  5. // @description A private group chat panel
  6. // @author Lux-Ferre
  7. // @license MIT
  8. // @match *://idle-pixel.com/login/play*
  9. // @grant none
  10. // @require https://greasyfork.org/scripts/441206-idlepixel/code/IdlePixel+.js?anticache=20220905
  11. // @require https://update.greasyfork.org/scripts/484046/1307183/IdlePixel%2B%20Custom%20Handling.js
  12. // ==/UserScript==
  13. (function() {
  14. 'use strict';
  15.  
  16. class GroupChatPlugin extends IdlePixelPlusPlugin {
  17. constructor() {
  18. super("groupChat", {
  19. about: {
  20. name: GM_info.script.name,
  21. version: GM_info.script.version,
  22. author: GM_info.script.author,
  23. description: GM_info.script.description
  24. },
  25. config: [
  26. {
  27. id: "memberList",
  28. label: "Members List (comma separated list)",
  29. type: "string"
  30. },
  31. {
  32. id: "password",
  33. label: "Password",
  34. type: "string"
  35. },
  36. ]
  37. });
  38. }
  39. createPanel(){
  40. IdlePixelPlus.addPanel("groupchat", "Group Chat Panel", function() {
  41. return `
  42. <div class="groupChatUIContainer w-100">
  43. <div id="groupChatInfoModule" class="row groupChatUIModule">
  44. <div class="col">
  45. <div class="row">
  46. <div class="col-1 text-end align-self-center"><label class="col-form-label" for="groupChatMembersList">Members:</label></div>
  47. <div class="col-8 d-flex"><input id="groupChatMemberList" class="w-100" type="text" readonly /></div>
  48. <div class="col-1 text-end align-self-center"><label class="col-form-label" for="groupChatPassword">Password:</label></div>
  49. <div class="col-2 d-flex"><input id="groupChatPassword" class="w-100" type="text" readonly /></div>
  50. </div>
  51. </div>
  52. </div>
  53. <div id="groupChatChatModule" class="row groupChatUIModule">
  54. <div class="col">
  55. <div class="row">
  56. <div id="groupChatChatFormContainer" class="col">
  57. <div id="groupChatChatBox" class="overflow-auto"></div>
  58. <form onsubmit="event.preventDefault(); IdlePixelPlus.plugins.groupChat.sendGroupChat();">
  59. <div class="row d-flex flex-fill">
  60. <div class="col-11"><input id="groupChatChatIn" class="form-control w-100" type="text" /></div>
  61. <div class="col-1"><input id="groupChatChatButton" class="w-100 h-100" type="submit" value="Send" /></div>
  62. </div>
  63. </form>
  64. </div>
  65. </div>
  66. </div>
  67. </div>
  68. </div>
  69. `
  70. });
  71. }
  72.  
  73. addStyles(){
  74. $("head").append(`
  75. <style id="styles-groupchat">
  76. .groupChatUIModule {
  77. border: outset 2px;
  78. }
  79. .groupChatUIContainer {
  80. width: 100%;
  81. height: 100%;
  82. padding: 5px;
  83. margin: 0;
  84. }
  85. #groupChatChatBox {
  86. width: 100%;
  87. height: 70vh;
  88. margin-top: 10px;
  89. }
  90. #groupChatChatBox {
  91. border: inset 1px;
  92. }
  93. </style>
  94. `)
  95. }
  96. onLogin() {
  97. const onlineCount = $(".top-bar .gold:not(#top-bar-admin-link)");
  98. onlineCount.before(`
  99. <a href="#" class="hover float-end link-no-decoration" onclick="event.preventDefault(); IdlePixelPlus.setPanel('groupchat')" title="Group Chat Panel">Group Chat&nbsp;&nbsp;&nbsp;</a>
  100. `);
  101. this.createPanel()
  102. this.addStyles()
  103. this.createNotification()
  104. this.updatePanelInfo()
  105. }
  106.  
  107. onPanelChanged(panelBefore, panelAfter){
  108. if (panelAfter==="groupchat"){
  109. $("#groupChatNotification").hide()
  110. }
  111. }
  112. onConfigsChanged() {
  113. this.updatePanelInfo();
  114. }
  115. onCustomMessageReceived(player, content, callbackId) {
  116. const customData = Customs.parseCustom(player, content, callbackId)
  117. const correctPassword = this.getConfig("password");
  118. if(customData.plugin==="groupchat") {
  119. if(customData.command==="chat"){
  120. const splitData = customData.payload.split(";")
  121. const givenPassword = splitData[0]
  122. const message = splitData.slice(1).join(";")
  123. if(givenPassword===correctPassword){
  124. this.addGroupChatMessage(player, message)
  125. }
  126. }
  127. }
  128. }
  129. updatePanelInfo(){
  130. const memberListField = $("#groupChatMemberList")
  131. const passwordField = $("#groupChatPassword")
  132. memberListField.val(this.getConfig("memberList"))
  133. passwordField.val(this.getConfig("password"))
  134. }
  135. createNotification(){
  136. const notificationString = `
  137. <div id="groupChatNotification" class="notification hover" onclick="IdlePixelPlus.setPanel('groupchat')">
  138. <img src="https://d1xsc8x7nc5q8t.cloudfront.net/images/diamond.png" class="w20" alt="">
  139. <span class="font-small color-yellow">Group Chat</span>
  140. </div>
  141. `
  142.  
  143. const notificationElement = $.parseHTML(notificationString)
  144. const notificationBar = $("#notifications-area")
  145.  
  146. notificationBar.append(notificationElement)
  147. $("#groupChatNotification").hide()
  148. }
  149.  
  150. showNotification(){
  151. if(Globals.currentPanel === "panel-groupchat"){return;}
  152. $("#groupChatNotification").show()
  153. }
  154. getMemberList(){
  155. const stringifiedNameList = this.getConfig("memberList").toLowerCase()
  156. // Remove trailing commas
  157. if(stringifiedNameList.charAt(stringifiedNameList.length - 1) === ","){
  158. stringifiedNameList = stringifiedNameList.slice(0, -1);
  159. }
  160. const nameList = stringifiedNameList.split(",")
  161. // Remove empty entry (handles when no names have been given)
  162. if (nameList[0] === ""){
  163. nameList.shift()
  164. }
  165. return nameList.slice(0, 8)
  166. }
  167. sendGroupChat(){
  168. const chatIn = $("#groupChatChatIn")
  169. const chat_message = chatIn.val()
  170. chatIn.val("")
  171. const password = this.getConfig("password")
  172. const memberList = this.getMemberList()
  173. memberList.forEach(member => {
  174. Customs.sendBasicCustom(member, "groupchat", "chat", `${password};${chat_message}`)
  175. })
  176. }
  177.  
  178. addGroupChatMessage(sender, message) {
  179. const newMessageString = `
  180. <div class="">
  181. <span class="color-green">${Chat._get_time()}</span>
  182. <span><strong>${sender}: </strong></span>
  183. <span>${sanitize_input(message)}</span>
  184. </div>
  185. `
  186.  
  187. const newMessageElement = $.parseHTML(newMessageString)
  188. const chatBox = $("#groupChatChatBox")
  189. chatBox.append(newMessageElement);
  190.  
  191. chatBox.scrollTop(chatBox[0].scrollHeight);
  192.  
  193. this.showNotification()
  194. }
  195. }
  196.  
  197. const plugin = new GroupChatPlugin();
  198. IdlePixelPlus.registerPlugin(plugin);
  199. })();