IdlePixel+ New Card Interface

Improved interface for opening new cards & receiving cards in trade

当前为 2024-03-28 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name IdlePixel+ New Card Interface
  3. // @namespace lbtechnology.info
  4. // @version 1.1.1
  5. // @description Improved interface for opening new cards & receiving cards in trade
  6. // @author Zlef
  7. // @license MIT
  8. // @match *://idle-pixel.com/login/play*
  9. // @grant none
  10. // @icon https://d1xsc8x7nc5q8t.cloudfront.net/images/tcg_back_50.png
  11. // @require https://greasyfork.org/scripts/441206-idlepixel/code/IdlePixel+.js?anticache=20220905
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. class NewCard extends IdlePixelPlusPlugin {
  18. constructor() {
  19. super("newcard", {
  20. about: {
  21. name: GM_info.script.name,
  22. version: GM_info.script.version,
  23. author: GM_info.script.author,
  24. description: GM_info.script.description
  25. }
  26. });
  27.  
  28. this.showPopup = false;
  29. this.messageStart = "You got a"
  30. this.trade = false;
  31. this.card = "";
  32. }
  33.  
  34. onLogin() {
  35. if (!CardData.data) {
  36. CardData.fetchData();
  37. }
  38. this.monitorRevealTCG();
  39. }
  40.  
  41. monitorRevealTCG() {
  42. const originalWebSocketSend = WebSocket.prototype.send;
  43. const self = this;
  44. WebSocket.prototype.send = function(data) {
  45. try {
  46. originalWebSocketSend.call(this, data);
  47. if (data === 'REVEAL_TCG_CARD') {
  48. self.showPopup = true;
  49. }
  50. } catch (error) {
  51. console.error('Error in overridden WebSocket send:', error);
  52. }
  53. };
  54. }
  55.  
  56. onMessageReceived(data){
  57. const originalOpenImageModal = Modals.open_image_modal;
  58. const self = this;
  59.  
  60. if (data.includes("OPEN_DIALOGUE")){
  61. Modals.open_image_modal = function(title, imgUrl, description, footerText, closeBtnText, anotherBtnText, isModalDismissible) {
  62. // console.log(`title: ${title}, imgUrl: ${imgUrl}, description: ${description}, footerText: ${footerText}, closeBtnText: ${closeBtnText}, anotherBtnText: ${anotherBtnText}, isModalDismissible: ${isModalDismissible}`);
  63.  
  64. if (description.includes("You were given a card from")) {
  65. // console.log("Found trade received modal call, blocking");
  66.  
  67. const usernameRegex = /You were given a card from (.*?)<br \/>/;
  68. const cardRegex = /<span class='color-grey'>(.*?)<\/span>/;
  69.  
  70. const usernameMatch = description.match(usernameRegex);
  71. const cardMatch = description.match(cardRegex);
  72.  
  73. let username = "";
  74. let card = "";
  75.  
  76. if (usernameMatch && usernameMatch.length > 1) {
  77. username = usernameMatch[1];
  78. }
  79.  
  80. if (cardMatch && cardMatch.length > 1) {
  81. self.card = cardMatch[1].replace(/ \(holo\)$/, '');
  82. }
  83.  
  84.  
  85. self.messageStart = `${username} sent you a`
  86. self.trade = true;
  87. self.showPopup = true;
  88. // console.log(`username: ${username}, card: ${self.card}, showPopup ${self.showPopup}, trade: ${self.trade}, messageStart: ${self.messageStart}`);
  89. } else {
  90. originalOpenImageModal.call(this, title, imgUrl, description, footerText, closeBtnText, anotherBtnText, isModalDismissible);
  91. }
  92. }
  93. }
  94.  
  95. if (data.includes("REFRESH_TCG")){
  96. // console.log(`showPopup: ${this.showPopup}, showPotradepup: ${this.trade}, card: ${this.card}, messageStart: ${this.messageStart}`)
  97. if (this.showPopup){
  98. if (this.trade){
  99. this.getCardInfo(data, true);
  100. this.trade = false;
  101. this.card = "";
  102. } else {
  103. this.getCardInfo(data, false);
  104. }
  105. this.showPopup = false;
  106. this.messageStart = "You got a "
  107. }
  108. }
  109. }
  110.  
  111. chunks(array, chunkSize) {
  112. const result = [];
  113. for (let i = 0; i < array.length; i += chunkSize) {
  114. const chunk = array.slice(i, i + chunkSize);
  115. result.push(chunk);
  116. }
  117. return result;
  118. }
  119.  
  120. getCardInfo(data, isTrade) {
  121. // console.log("getCardInfo called with data:", data, "isTrade:", isTrade);
  122.  
  123. const cardData = data.split('~');
  124. // console.log("Splitted cardData:", cardData);
  125.  
  126. const chunkedData = this.chunks(cardData, 3); // id, nameKey, holo
  127. // console.log("Chunked data:", chunkedData);
  128.  
  129. let relevantCard = chunkedData[0]; // Default to the first card for non-trade
  130. // console.log("Default relevant card:", relevantCard);
  131.  
  132. if (isTrade) {
  133. // console.log(`Trade detected. Looking for card: ${this.card}`);
  134. for (let i = 0; i < chunkedData.length; i++) {
  135. const [id, nameKey, holo] = chunkedData[i];
  136. // console.log(`Checking chunk: ${i}, ID: ${id}, NameKey: ${nameKey}, Holo: ${holo}`);
  137.  
  138. if (this.card === nameKey) {
  139. console.log("Matching card found in trade:", chunkedData[i]);
  140. relevantCard = chunkedData[i];
  141. break;
  142. }
  143. }
  144. }
  145.  
  146. // console.log("Relevant card to display:", relevantCard);
  147. this.displayNewCard(relevantCard[0], relevantCard[1], relevantCard[2]);
  148. }
  149.  
  150.  
  151. displayNewCard(cardId, cardNameKey, holo) {
  152. const cardName = cardNameKey.replace('tcg_', '').replace(/_/g, ' ').replace(" icon", "");
  153. const isHolo = holo === 'true';
  154.  
  155. const message = isHolo ? `${this.messageStart} holo ${cardName} card!` : `${this.messageStart} ${cardName} card!`;
  156. const cardHTML = CardData.getCardHTML(cardId, cardNameKey, isHolo);
  157.  
  158. this.createPopup(message, cardHTML);
  159.  
  160. }
  161.  
  162. createPopup(message, cardHTML) {
  163. // Check and remove existing overlay
  164. const existingOverlay = document.getElementById('newCardOverlay');
  165.  
  166. if (existingOverlay) {
  167. document.body.removeChild(existingOverlay);
  168. }
  169.  
  170. // Element setup
  171. const overlay = document.createElement('div');
  172. overlay.id = 'newCardOverlay';
  173. overlay.style.position = 'fixed';
  174. overlay.style.top = '0';
  175. overlay.style.left = '0';
  176. overlay.style.width = '100%';
  177. overlay.style.height = '100%';
  178. overlay.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
  179. overlay.style.zIndex = '1000';
  180. overlay.style.display = 'flex';
  181. overlay.style.justifyContent = 'center';
  182. overlay.style.alignItems = 'center';
  183.  
  184. const popupBox = document.createElement('div');
  185. popupBox.id = 'newCardPopupBox';
  186. popupBox.style.width = '300px';
  187. popupBox.style.margin = '0 auto';
  188. popupBox.style.backgroundColor = '#fff';
  189. popupBox.style.boxShadow = '0 0 10px rgba(0, 0, 0, 0.5)';
  190. popupBox.style.borderRadius = '8px';
  191. popupBox.style.padding = '20px';
  192. popupBox.style.textAlign = 'center';
  193.  
  194. const messageP = document.createElement('p');
  195. messageP.textContent = message;
  196. messageP.style.fontSize = '18px';
  197. messageP.style.fontWeight = 'bold';
  198.  
  199. const cardContainer = document.createElement('div');
  200. cardContainer.innerHTML = cardHTML;
  201. cardContainer.firstChild.style.marginTop = '0px';
  202.  
  203. const cardTitle = cardContainer.querySelector('.tcg-card-title');
  204. const cardInnerText = cardContainer.querySelector('.tcg-card-inner-text');
  205. if (cardTitle) {
  206. cardTitle.style.textAlign = 'left';
  207. }
  208. if (cardInnerText) {
  209. cardInnerText.style.textAlign = 'left';
  210. }
  211.  
  212. const openAnotherButton = document.createElement('button');
  213. openAnotherButton.textContent = 'OPEN ANOTHER';
  214. openAnotherButton.style.padding = '10px 20px';
  215. openAnotherButton.style.fontSize = '16px';
  216. openAnotherButton.style.cursor = 'pointer';
  217. openAnotherButton.style.marginRight = '10px';
  218.  
  219. const closeButton = document.createElement('button');
  220. closeButton.textContent = 'CLOSE';
  221. closeButton.style.padding = '10px 20px';
  222. closeButton.style.fontSize = '16px';
  223. closeButton.style.cursor = 'pointer';
  224.  
  225. // Append elements
  226. popupBox.appendChild(messageP);
  227. popupBox.appendChild(cardContainer);
  228. popupBox.appendChild(openAnotherButton);
  229. popupBox.appendChild(closeButton);
  230. overlay.appendChild(popupBox);
  231. document.body.appendChild(overlay);
  232.  
  233. // Event listeners
  234. openAnotherButton.addEventListener('click', () => {
  235. IdlePixelPlus.sendMessage("REVEAL_TCG_CARD");
  236. document.body.removeChild(overlay);
  237. });
  238.  
  239. const tcg_unknown = IdlePixelPlus.getVarOrDefault("tcg_unknown", 0, "int");
  240. openAnotherButton.disabled = tcg_unknown <= 1;
  241.  
  242. closeButton.addEventListener('click', () => {
  243. document.body.removeChild(overlay);
  244. window.removeEventListener('resize', adjustPopupPosition);
  245. });
  246.  
  247. const adjustPopupPosition = () => {
  248. const viewportHeight = window.innerHeight;
  249. const popupHeight = popupBox.offsetHeight;
  250. const topPosition = (viewportHeight - popupHeight) / 2;
  251. popupBox.style.position = 'absolute';
  252. popupBox.style.top = `${topPosition > 0 ? topPosition : 0}px`;
  253. };
  254. adjustPopupPosition();
  255.  
  256. window.addEventListener('resize', adjustPopupPosition);
  257.  
  258. overlay.addEventListener('click', (event) => {
  259. if (event.target === overlay) {
  260. document.body.removeChild(overlay);
  261. window.removeEventListener('resize', adjustPopupPosition);
  262. }
  263. });
  264.  
  265. popupBox.addEventListener('click', (event) => {
  266. event.stopPropagation();
  267. });
  268. }
  269.  
  270.  
  271. }
  272.  
  273. const plugin = new NewCard();
  274. IdlePixelPlus.registerPlugin(plugin);
  275.  
  276. })();