RohanPhuyal

[Console] Less Error

Sep 10th, 2023 (edited)
1,323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 5.62 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.             console.log("pricesofAll()");
  25.             var prices = document
  26.                 .querySelector("shopping-page-base")
  27.                 ?.shadowRoot.querySelector("shopping-homepage")
  28.                 ?.shadowRoot.querySelector("cs-feed-layout")
  29.                 ?.shadowRoot.querySelector("msn-shopping-game-pane").displayedShoppingEntities;
  30.  
  31.             var loopTimes = prices.length;
  32.             for (let i = 0; i < loopTimes; i++) {
  33.                 pricesAll.push(prices[i].priceInfo.originalPrice); // Add original price to pricesAll array
  34.                 discountAll.push(prices[i].dealPercentage); // Add discount to discountAll array
  35.             }
  36.         }
  37.  
  38.         async function calculateDiscount() {
  39.             console.log("calculateDiscount()");
  40.             for (let i = 0; i < pricesAll.length; i++) {
  41.                 console.log("Price before removing sign: "+pricesAll[i]);
  42.                 let initPrice = parseFloat(pricesAll[i].replace(/[^\d.]/g, ""));
  43.                 console.log("Price after removing sign: "+initPrice);
  44.                 console.log("Discount before removing sign: "+discountAll[i]);
  45.                 let discountPercentage = parseFloat(discountAll[i].replace(/[^\d]/g, ""));
  46.                 console.log("Discount after removing sign: "+discountPercentage);
  47.                 let discountedPrice = (initPrice - ((initPrice * discountPercentage) / 100));
  48.                 finalPrice.push(discountedPrice);
  49.             }
  50.         }
  51.         async function compareValues() {
  52.             for (let i = 0; i < finalPrice.length; i++) {
  53.               for (let j = i + 1; j < finalPrice.length; j++) {
  54.                 const diff = Math.abs(finalPrice[i] - finalPrice[j]);
  55.                 if (diff < 0.09) {
  56.                   alert("Values differ by less than 0.09. Reload...");
  57.                   window.location.reload();
  58.                   return; // Terminate the function
  59.                 }
  60.               }
  61.             }
  62.             console.log("All values are at least 0.89 different from each other.");
  63.           }
  64.         async function findCheapestIndex(finalPrice) {
  65.             await compareValues();
  66.             var cheapestPrice = Math.min(...finalPrice); // Find the lowest value in the finalPrice array
  67.             var cheapIndex = finalPrice.indexOf(cheapestPrice); // Get the index of the lowest value
  68.             return cheapIndex;
  69.         }
  70.  
  71.         async function highlightAndRemoveItems(correctIndex, items) {
  72.             for (let i = 0; i < items.length; i++) {
  73.                 if (i === correctIndex) {
  74.                     items[i].style.borderColor = "green";
  75.                 } else {
  76.                     items[i].style.display = "none";
  77.                 }
  78.             }
  79.         }
  80.         async function playAgainFunc() {
  81.             const gamePanelContainer = document.querySelector("shopping-page-base")
  82.                 ?.shadowRoot.querySelector("shopping-homepage")
  83.                 ?.shadowRoot.querySelector("cs-feed-layout")
  84.                 ?.shadowRoot.querySelector("msn-shopping-game-pane")
  85.                 ?.shadowRoot.querySelector("div.shopping-game-pane-container > div.game-panel-container > div.game-panel-header-2");
  86.        
  87.             if (gamePanelContainer) {
  88.                 const playAgainButtons = gamePanelContainer.querySelectorAll("button");
  89.        
  90.                 playAgain = Array.from(playAgainButtons).find(button => button.textContent.toLowerCase().includes("play again"));
  91.        
  92.                 if (playAgain !== null) {
  93.                     console.log("Terminating playAgainFunc(): playAgain button found!");
  94.                     playAgain.click();
  95.                     clearInterval(fixIntervalId);
  96.                     playAgain = null;
  97.                     pricesAll = [];
  98.                     discountAll = [];
  99.                     finalPrice = [];
  100.                     cheapestIndex = null;
  101.                     setTimeout(() => executeScript(), 9000);
  102.                 }
  103.             }
  104.         }
  105.        
  106.         await pricesOfAll();
  107.         await calculateDiscount();
  108.         cheapestIndex = await findCheapestIndex(finalPrice);
  109.         console.log("Original Price: " + pricesAll);
  110.         console.log("Discount%: " + discountAll);
  111.         console.log("Final Price: " + finalPrice);
  112.         console.log("Answer: " + cheapestIndex);
  113.         await highlightAndRemoveItems(cheapestIndex, shoppingGame.getElementsByClassName("shopping-game-card-outline"));
  114.         // Only schedule the setTimeout if playAgain is still null
  115.         var fixIntervalId = setInterval(async function () {
  116.             await playAgainFunc();
  117.         }, 100);
  118.     }
  119.         await executeScript();
  120. })();
Tags: js console MSN
Advertisement
Add Comment
Please, Sign In to add comment