RohanPhuyal

[CONSOLE] SCRIPT

Aug 29th, 2023
1,599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 4.83 KB | Source Code | 0 0
  1. (async function () {
  2.     var playAgain = null;
  3.     document.querySelector("shopping-page-base")
  4.         ?.shadowRoot.querySelector("shopping-homepage")
  5.         ?.shadowRoot.querySelector("cs-feed-layout")
  6.         ?.shadowRoot.querySelector("msn-shopping-game-pane").setAttribute('gamestate', 'active');
  7.  
  8.     var shoppingGame = document.querySelector("shopping-page-base")
  9.         ?.shadowRoot.querySelector("shopping-homepage")
  10.         ?.shadowRoot.querySelector("cs-feed-layout")
  11.         ?.shadowRoot.querySelector("msn-shopping-game-pane")
  12.         ?.shadowRoot.querySelector("msft-stripe");
  13.     shoppingGame.scrollIntoView({ behavior: "smooth" });
  14.  
  15.     async function executeScript() {
  16.         console.log("Executing");
  17.         var pricesAll = []; // Array to store original prices
  18.         var discountAll = []; // Array to store discounts
  19.         var finalPrice = [];
  20.         var cheapestIndex; // Store the index of the cheapest item
  21.  
  22.  
  23.         async function pricesOfAll() {
  24.             var prices = document
  25.                 .querySelector("shopping-page-base")
  26.                 ?.shadowRoot.querySelector("shopping-homepage")
  27.                 ?.shadowRoot.querySelector("cs-feed-layout")
  28.                 ?.shadowRoot.querySelector("msn-shopping-game-pane").displayedShoppingEntities;
  29.  
  30.             var loopTimes = prices.length;
  31.             for (let i = 0; i < loopTimes; i++) {
  32.                 pricesAll.push(prices[i].priceInfo.originalPrice); // Add original price to pricesAll array
  33.                 discountAll.push(prices[i].dealPercentage); // Add discount to discountAll array
  34.             }
  35.         }
  36.  
  37.         async function calculateDiscount() {
  38.             for (let i = 0; i < pricesAll.length; i++) {
  39.                 let initPrice = parseFloat(pricesAll[i].replace(/[$,]/g, ""));
  40.                 let discountPercentage = parseFloat(discountAll[i].replace("%", ""));
  41.                 let discountedPrice = (initPrice - ((initPrice * discountPercentage) / 100));
  42.                 finalPrice.push(discountedPrice);
  43.             }
  44.         }
  45.         async function findCheapestIndex(finalPrice) {
  46.             var cheapestPrice = Math.min(...finalPrice); // Find the lowest value in the finalPrice array
  47.             var cheapIndex = finalPrice.indexOf(cheapestPrice); // Get the index of the lowest value
  48.             return cheapIndex;
  49.         }
  50.  
  51.         async function highlightAndRemoveItems(correctIndex, items) {
  52.             for (let i = 0; i < items.length; i++) {
  53.                 if (i === correctIndex) {
  54.                     items[i].style.borderColor = "green";
  55.                 } else {
  56.                     items[i].style.display = "none";
  57.                 }
  58.             }
  59.             pricesAll = [];
  60.             discountAll = [];
  61.             finalPrice = [];
  62.             cheapestIndex = null;
  63.  
  64.         }
  65.         async function playAgainFunc() {
  66.             // Get the initial shadow DOM element
  67.             const firstShadowRoot = document.querySelector("#root > div > div > fluent-design-system-provider > div > div:nth-child(4) > div > shopping-page-base").shadowRoot;
  68.  
  69.             // Traverse through the shadow DOM to find the desired elements
  70.             const shoppingHomepage = firstShadowRoot.querySelector("div > div.shopping-page-content > shopping-homepage").shadowRoot;
  71.             const csFeedLayout = shoppingHomepage.querySelector("div > cs-feed-layout").shadowRoot;
  72.             const shoppingGamePane = csFeedLayout.querySelector("msn-shopping-game-pane").shadowRoot;
  73.             const gamePanelContainer = shoppingGamePane.querySelector("div.shopping-game-pane-container > div.game-panel-container > div.game-panel-header-2");
  74.             if(gamePanelContainer){
  75.                 // Search for a button with the text "Play Again" within the div
  76.             playAgain = gamePanelContainer.querySelectorAll("button")[0];
  77.             }
  78.             if (playAgain !== null) {
  79.                 console.log("Terminating playAgainFunc(): playAgain button found!");
  80.                 playAgain.click();
  81.                 clearInterval(fixIntervalId);
  82.                 playAgain=null;
  83.                 setTimeout(() => executeScript(), 9000);
  84.             }
  85.         }
  86.         await pricesOfAll();
  87.         await calculateDiscount();
  88.         cheapestIndex = await findCheapestIndex(finalPrice);
  89.         console.log("Original Price: " + pricesAll);
  90.         console.log("Discount%: " + discountAll);
  91.         console.log("Final Price: " + finalPrice);
  92.         console.log("Answer: " + cheapestIndex);
  93.         await highlightAndRemoveItems(cheapestIndex, shoppingGame.getElementsByClassName("shopping-game-card-outline"));
  94.         // Only schedule the setTimeout if playAgain is still null
  95.         var fixIntervalId = setInterval(async function () {
  96.             await playAgainFunc();
  97.         }, 100);
  98.     }
  99.         await executeScript();
  100. })();
  101.  
Advertisement
Add Comment
Please, Sign In to add comment