PixelShop+

Makes it easier to create new custom shops for Idle Pixel

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/488260/1448277/PixelShop%2B.js

  1. // ==UserScript==
  2. // @name Pixel Shop+
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0.8
  5. // @description Makes it easier to create new custom shops for Idle Pixel
  6. // @author Dounford
  7. // @license MIT
  8. // @match *://idle-pixel.com/login/play*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. /*newCoinFormat = {
  13. name:'testCoin',
  14. image:'https://raw.githubusercontent.com/Dounford-Felipe/DHP-Pets/main/images/goldenHeart.gif',
  15. value:2000
  16. }
  17. */
  18. //PixelShopPlus.newCoin(newCoinFormat)
  19.  
  20. //newShopFormat = PixelShopPlus.newShop('fish','testCoin')
  21.  
  22. /*newItemFormat = [{
  23. name:"blueCat",
  24. imageUrl:"https://raw.githubusercontent.com/Dounford-Felipe/DHP-Pets/main/images/blueCat.png",
  25. coin:"testCoin",
  26. price:1000,
  27. tooltipText:"<span class='color-primary'>Blue Cat</span><br /><br />This cute cat wants to be your friend",
  28. buyText:"Adopt this cat to be your friend",
  29. boughtText:"Charlie will forever love you",
  30. callback: function(){IdlePixelPlus.plugins.test.bought(pirate)}
  31. }]*/
  32. //PixelShopPlus.newItems('fish',newItemFormat)
  33.  
  34. if (!document.getElementById('shopButtons')) {
  35. (function PixelShopPlus() {
  36. 'use strict';
  37.  
  38. const PixelShopPlus = {
  39. shops: ['vanilla'],
  40. coins: {},
  41. items: {},
  42. initialize: function () {
  43. PixelShopPlus.newModals();
  44. PixelShopPlus.newShopDivs();
  45. },
  46. //Creates
  47. newModals: function () {
  48. let customShopModalDiv = `<dialog id="customShopModal" onclick="event.target==this && this.close()" class="dounfordModal">
  49. <div class="dounfordModalHeader">
  50. <h5 class="modal-title text-secondary">Purchace Item</h5>
  51. <button type="button" class="btn-close" onclick="document.getElementById('customShopModal').close()" style="padding: 0.5rem;"></button>
  52. <input type="hidden" id="customShopModalShop">
  53. <input type="hidden" id="customShopModalItem">
  54. </div>
  55. <div class="dounfordModalBody">
  56. <span>
  57. <center>
  58. <img id="customShopModalImage" src="" title="Custom Shop Item" style="height:100px">
  59. </center>
  60. <br>
  61. <br>
  62. </span>
  63. <div class="center" id="customShopModalText">Purchase this item?</div>
  64. </div>
  65.  
  66. <div class="dounfordModalFooter">
  67. <button onclick="document.getElementById('customShopModal').close()">
  68. <span class="font-pixel hover">Cancel</span>
  69. </button>
  70. <button id="customShopModalBuy" class="background-primary">
  71. <span class="font-pixel hover">Purchase</span>
  72. </button>
  73. </div>
  74. </dialog>`
  75. document.getElementById('content').insertAdjacentHTML('beforeend', customShopModalDiv);
  76. document.getElementById('customShopModalBuy').addEventListener('click', PixelShopPlus.buyItem);
  77. },
  78. //Adds the change shop button
  79. newShopDivs: function () {
  80. let shopButton = document.createElement('div');
  81. shopButton.id = "shopButtons";
  82. shopButton.style.cssText = "white-space: nowrap; overflow-x: auto;";
  83. shopButton.innerHTML = `<div id="vanilla-shop" onclick="PixelShopPlus.changeShopTabs('vanilla')" style="background-color: rgb(247, 122, 129); white-space: normal;" class="quest-tab-button hover">
  84. VANILLA SHOP
  85. </div>`
  86. let vanillaShop = document.createElement('div');
  87. vanillaShop.id = "vanillaShop";
  88. while (document.getElementById('panel-shop').firstChild) {
  89. vanillaShop.appendChild(document.getElementById('panel-shop').firstChild);
  90. }
  91. document.getElementById('panel-shop').appendChild(shopButton);
  92. document.getElementById('panel-shop').insertAdjacentHTML('beforeend', '<hr>');
  93. document.getElementById('panel-shop').appendChild(vanillaShop);
  94. },
  95. //Change which shop is shown
  96. changeShopTabs: function(value) {
  97. // Oculta todos os tabs e remove o destaque
  98. for (const shopId of PixelShopPlus.shops) {
  99. document.getElementById(shopId + 'Shop').style.display = "none";
  100. document.getElementById(shopId + '-shop').style.backgroundColor = "";
  101. }
  102.  
  103. document.getElementById(value + 'Shop').style.display = "";
  104. document.getElementById(value + '-shop').style.backgroundColor = "#f77a81";
  105. },
  106. //Register a new coin
  107. newCoin: function(coin) {
  108. if (typeof PixelShopPlus.coins[coin.name] == 'undefined') {
  109. PixelShopPlus.coins[coin.name] = {...coin}
  110. }
  111. },
  112. //Increase the value of a coin
  113. coinIncrease: function(coin,increase) {
  114. PixelShopPlus.coins[coin].value += increase;
  115. PixelShopPlus.saveCoins();
  116. if (document.querySelector('[coin-value="' + coin + '"]')) {
  117. let coinElements = document.querySelectorAll('[coin-value="' + coin + '"]');
  118. coinElements.forEach(function(coinElement) {
  119. coinElement.textContent = PixelShopPlus.coins[coin].value.toLocaleString("en-US");
  120. });
  121. }
  122. },
  123. //Decrease the value of a coin
  124. coinDecrease: function(coin,decrease) {
  125. PixelShopPlus.coins[coin].value -= decrease;
  126. PixelShopPlus.saveCoins();
  127. if (document.querySelector('[coin-value="' + coin + '"]')) {
  128. let coinElements = document.querySelectorAll('[coin-value="' + coin + '"]');
  129. coinElements.forEach(function(coinElement) {
  130. coinElement.textContent = PixelShopPlus.coins[coin].value.toLocaleString("en-US");
  131. });
  132. }
  133. },
  134. //Set the value of a coin
  135. coinSet: function(coin,setValue) {
  136. PixelShopPlus.coins[coin].value = setValue;
  137. PixelShopPlus.saveCoins();
  138. if (document.querySelector('[coin-value="' + coin + '"]')) {
  139. let coinElements = document.querySelectorAll('[coin-value="' + coin + '"]');
  140. coinElements.forEach(function(coinElement) {
  141. coinElement.textContent = PixelShopPlus.coins[coin].value.toLocaleString("en-US");
  142. });
  143. }
  144. },
  145. //Register a new shop
  146. newShop: function (shop,coin,beforeFunction) {
  147. PixelShopPlus.shops.push(shop);
  148. PixelShopPlus.items[shop] = {};
  149. let newTabButton = document.createElement('div');
  150. newTabButton.id = shop + "-shop";
  151. newTabButton.className = "quest-tab-button hover";
  152. newTabButton.style.marginLeft = "20px";
  153. newTabButton.innerHTML = shop.toLocaleUpperCase() + ' SHOP';
  154. newTabButton.onclick = function() {
  155. PixelShopPlus.changeShopTabs(shop);
  156. };
  157. document.getElementById('shopButtons').appendChild(newTabButton);
  158. let newShopPanel = document.createElement('div');
  159. newShopPanel.id = shop + "Shop"
  160. newShopPanel.style.display = "none"
  161. newShopPanel.innerHTML = `<h1>${shop.toLocaleUpperCase()} SHOP</h1>`
  162. if (typeof coin == 'string') {
  163. let newCoinImage = document.createElement('img');
  164. newCoinImage.src = PixelShopPlus.coins[coin].image
  165. newCoinImage.className = "w30"
  166. let newCoinValue = document.createElement('span');
  167. newCoinValue.setAttribute('coin-value', coin)
  168. newCoinValue.innerText = PixelShopPlus.coins[coin].value.toLocaleString("en-US")
  169. newCoinValue.style.marginLeft = "5px"
  170. newCoinValue.style.fontSize = "1.3rem"
  171. newCoinValue.style.verticalAlign = "middle"
  172. newShopPanel.appendChild(newCoinImage);
  173. newShopPanel.appendChild(newCoinValue);
  174. }
  175. if (typeof beforeFunction == 'function') {
  176. PixelShopPlus.items[shop].beforeFunction = beforeFunction;
  177. }
  178. newShopPanel.insertAdjacentHTML('beforeend', '<hr>');
  179. document.getElementById('panel-shop').appendChild(newShopPanel);
  180. },
  181. //Register an array of new buyable items
  182. newItems: function(shop,items) {
  183. items.forEach(function(item) {
  184. PixelShopPlus.items[shop][item.name] = item;
  185. let newShopItem = `<div id="shop-${item.name}" onclick="PixelShopPlus.openBuyModal('${shop}','${item.name}')" class="game-shop-box hover shadow" data-tooltip="shop-${item.name}" data-bs-toggle="tooltip" data-bs-placement="right" data-bs-html="true" title="" data-bs-original-title="${item.tooltipText}" style="margin-right: 5px;">
  186. <div class="center mt-2">
  187. <img src="${item.imageUrl}" title="${item.name}" style="height: 50px;">
  188. </div>
  189. <div class="center mt-2">
  190. <img src="${PixelShopPlus.coins[item.coin].image}" title="${item.coin}" class="w20">
  191. <span id="${item.name}-shop-cost">${format_number(item.price)}</span>
  192. </div>
  193. </div>`
  194. document.getElementById(shop + 'Shop').insertAdjacentHTML('beforeend',newShopItem)
  195. $('#shop-' + item.name).tooltip()
  196. })
  197. },
  198. //In case you want to remove an Item
  199. removeItem: function(shop,item) {
  200. let removedItem = document.getElementById('shop-' + item);
  201. removedItem.parentNode.removeChild(removedItem);
  202. delete PixelShopPlus.items[shop][item];
  203. console.log(item + ' from ' + shop + ' Shop was removed');
  204. },
  205. //Open Buy Confirm
  206. openBuyModal: function(shop,item) {
  207. if (PixelShopPlus.items[shop][item]) {
  208. document.getElementById('customShopModalShop').value = shop;
  209. document.getElementById('customShopModalItem').value = item;
  210. document.getElementById('customShopModalImage').src = PixelShopPlus.items[shop][item].imageUrl;
  211. document.getElementById('customShopModalText').innerText = PixelShopPlus.items[shop][item].buyText || "Buy?";
  212. document.getElementById('customShopModalBuy').style.display='';
  213. document.getElementById('customShopModal').showModal();
  214. }
  215. },
  216. //Buy Item
  217. buyItem: async function() {
  218. let shop = document.getElementById('customShopModalShop').value;
  219. let item = document.getElementById('customShopModalItem').value;
  220. let price = PixelShopPlus.items[shop][item].price
  221. let coin = PixelShopPlus.items[shop][item].coin;
  222. if (PixelShopPlus.coins[coin].value >= price) {
  223. document.getElementById('customShopModalBuy').style.display="none";
  224. if (typeof PixelShopPlus.items[shop].beforeFunction == "function") {
  225. const success = await PixelShopPlus.items[shop].beforeFunction(item)
  226. if (!success) {
  227. document.getElementById('customShopModalBuy').style.display='none';
  228. document.getElementById('customShopModalText').innerText = "You are unable to buy this, sorry.";
  229. return
  230. }
  231. }
  232. PixelShopPlus.coinDecrease(coin,price);
  233. document.getElementById('shop-' + item).style.display = "none";
  234. document.getElementById('customShopModalText').innerText = PixelShopPlus.items[shop][item].boughtText || "Bought!";
  235. if(typeof PixelShopPlus.items[shop][item].callback == "function") {
  236. PixelShopPlus.items[shop][item].callback(item)
  237. }
  238. } else {
  239. document.getElementById('customShopModalBuy').style.display='none';
  240. document.getElementById('customShopModalText').innerText = "You can't afford this, sorry.";
  241. }
  242. },
  243. //Save the coin amount on localstorage
  244. saveCoins: function() {
  245. let key = `ShopPlusCoins-${var_username}`;
  246. localStorage.setItem(key, JSON.stringify(PixelShopPlus.coins));
  247. },
  248. //Load the coin amount on localstorage
  249. loadCoins: function() {
  250. let key = `ShopPlusCoins-${var_username}`;
  251. let loadedCoins = localStorage.getItem(key);
  252. if (loadedCoins) {
  253. PixelShopPlus.coins = JSON.parse(loadedCoins);
  254. }
  255. },
  256. };
  257. window.PixelShopPlus = PixelShopPlus;
  258. PixelShopPlus.initialize();
  259. })()
  260. }