IdlePixel+ New Card Interface

Improved interface for opening new cards

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

  1. // ==UserScript==
  2. // @name IdlePixel+ New Card Interface
  3. // @namespace lbtechnology.info
  4. // @version 1.0.1
  5. // @description Improved interface for opening new cards
  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. this.showPopup = false;
  28. }
  29.  
  30. onLogin() {
  31. if (!CardData.data) {
  32. CardData.fetchData();
  33. }
  34. this.monitorRevealTCG();
  35. }
  36.  
  37. monitorRevealTCG() {
  38. const originalWebSocketSend = WebSocket.prototype.send;
  39. const self = this;
  40. WebSocket.prototype.send = function(data) {
  41. try {
  42. originalWebSocketSend.call(this, data);
  43. if (data === 'REVEAL_TCG_CARD') {
  44. self.showPopup = true;
  45. }
  46. } catch (error) {
  47. console.error('Error in overridden WebSocket send:', error);
  48. }
  49. };
  50. }
  51.  
  52. onMessageReceived(data){
  53. if (data.includes("REFRESH_TCG")){
  54. if (this.showPopup){
  55. this.displayNewCard(data);
  56. this.showPopup = false;
  57. }
  58. }
  59. }
  60.  
  61. displayNewCard(data) {
  62. const cardData = data.split('~');
  63. const cardId = cardData[0];
  64. const cardNameKey = cardData[1]
  65. const cardName = cardData[1].replace('tcg_', '').replace(/_/g, ' ').replace(" icon", "");
  66. const isHolo = cardData[2] === 'true';
  67. const message = isHolo ? `You got a holo ${cardName} card!` : `You got a ${cardName} card!`;
  68.  
  69. const cardHTML = CardData.getCardHTML(cardId, cardNameKey, isHolo);
  70.  
  71. this.createPopup(message, cardHTML);
  72.  
  73. }
  74.  
  75. createPopup(message, cardHTML) {
  76. // Check and remove existing overlay
  77. const existingOverlay = document.getElementById('newCardOverlay');
  78.  
  79. if (existingOverlay) {
  80. document.body.removeChild(existingOverlay);
  81. }
  82.  
  83. // Element setup
  84. const overlay = document.createElement('div');
  85. overlay.id = 'newCardOverlay';
  86. overlay.style.position = 'fixed';
  87. overlay.style.top = '0';
  88. overlay.style.left = '0';
  89. overlay.style.width = '100%';
  90. overlay.style.height = '100%';
  91. overlay.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
  92. overlay.style.zIndex = '1000';
  93. overlay.style.display = 'flex';
  94. overlay.style.justifyContent = 'center';
  95. overlay.style.alignItems = 'center';
  96.  
  97. const popupBox = document.createElement('div');
  98. popupBox.id = 'newCardPopupBox';
  99. popupBox.style.width = '300px';
  100. popupBox.style.margin = '0 auto';
  101. popupBox.style.backgroundColor = '#fff';
  102. popupBox.style.boxShadow = '0 0 10px rgba(0, 0, 0, 0.5)';
  103. popupBox.style.borderRadius = '8px';
  104. popupBox.style.padding = '20px';
  105. popupBox.style.textAlign = 'center';
  106.  
  107. const messageP = document.createElement('p');
  108. messageP.textContent = message;
  109. messageP.style.fontSize = '18px';
  110. messageP.style.fontWeight = 'bold';
  111.  
  112. const cardContainer = document.createElement('div');
  113. cardContainer.innerHTML = cardHTML;
  114. cardContainer.firstChild.style.marginTop = '0px';
  115.  
  116. const openAnotherButton = document.createElement('button');
  117. openAnotherButton.textContent = 'OPEN ANOTHER';
  118. openAnotherButton.style.padding = '10px 20px';
  119. openAnotherButton.style.fontSize = '16px';
  120. openAnotherButton.style.cursor = 'pointer';
  121. openAnotherButton.style.marginRight = '10px';
  122.  
  123. const closeButton = document.createElement('button');
  124. closeButton.textContent = 'CLOSE';
  125. closeButton.style.padding = '10px 20px';
  126. closeButton.style.fontSize = '16px';
  127. closeButton.style.cursor = 'pointer';
  128.  
  129. // Append elements
  130. popupBox.appendChild(messageP);
  131. popupBox.appendChild(cardContainer);
  132. popupBox.appendChild(openAnotherButton);
  133. popupBox.appendChild(closeButton);
  134. overlay.appendChild(popupBox);
  135. document.body.appendChild(overlay);
  136.  
  137. // Event listeners
  138. openAnotherButton.addEventListener('click', () => {
  139. IdlePixelPlus.sendMessage("REVEAL_TCG_CARD");
  140. document.body.removeChild(overlay);
  141. });
  142.  
  143. const tcg_unknown = IdlePixelPlus.getVarOrDefault("tcg_unknown", 0, "int");
  144. openAnotherButton.disabled = tcg_unknown <= 1;
  145.  
  146. closeButton.addEventListener('click', () => {
  147. document.body.removeChild(overlay);
  148. window.removeEventListener('resize', adjustPopupPosition);
  149. });
  150.  
  151. const adjustPopupPosition = () => {
  152. const viewportHeight = window.innerHeight;
  153. const popupHeight = popupBox.offsetHeight;
  154. const topPosition = (viewportHeight - popupHeight) / 2;
  155. popupBox.style.position = 'absolute';
  156. popupBox.style.top = `${topPosition > 0 ? topPosition : 0}px`;
  157. };
  158. adjustPopupPosition();
  159.  
  160. window.addEventListener('resize', adjustPopupPosition);
  161.  
  162. overlay.addEventListener('click', (event) => {
  163. if (event.target === overlay) {
  164. document.body.removeChild(overlay);
  165. window.removeEventListener('resize', adjustPopupPosition);
  166. }
  167. });
  168.  
  169. popupBox.addEventListener('click', (event) => {
  170. event.stopPropagation();
  171. });
  172. }
  173.  
  174.  
  175. }
  176.  
  177. const plugin = new NewCard();
  178. IdlePixelPlus.registerPlugin(plugin);
  179.  
  180. })();