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