Calculadora Atacadao

Pequenina calculadora por peso para o site Atacadao

  1. // ==UserScript==
  2. // @name Calculadora Atacadao
  3. // @license Creative Commons Attribution Non Commercial Share Alike 4.0 International (CC BY-NC-SA 4.0)
  4. // @namespace Violentmonkey Scripts
  5. // @include https://www.atacadao.com.br/*
  6. // @grant none
  7. // @version 1.0
  8. // @author Gabriela Perini
  9. // @description Pequenina calculadora por peso para o site Atacadao
  10. // ==/UserScript==
  11. (function () {
  12. // Wait for the DOM to load
  13. document.addEventListener("DOMContentLoaded", (function () {
  14. console.log("DOM fully loaded. Setting up MutationObserver...");
  15.  
  16. // Set up a MutationObserver to watch for changes in the DOM
  17. const observer = new MutationObserver(function (mutations) {
  18. console.log("MutationObserver callback triggered");
  19.  
  20. // Temporarily disconnect the observer to prevent infinite loops
  21. observer.disconnect();
  22.  
  23. // Find all product cards
  24. const productCards = document.querySelectorAll('[data-product-card-content="true"]');
  25.  
  26. // If product cards are found, process them
  27. if (productCards.length > 0) {
  28. productCards.forEach(function (card) {
  29. // Check if the log content has already been added to this card
  30. if (card.querySelector(".product-log")) {
  31. return; // Skip if already processed
  32. console.log("return");
  33. }
  34.  
  35. // Extract the title
  36. const titleElement = card.querySelector("h3");
  37. if (titleElement) {
  38. const title = titleElement.textContent.trim();
  39.  
  40. // Extract the quantity and unit from the title
  41. const quantityMatch = title.replace(/[^\d,]/g, "");
  42. const quantity = parseFloat((quantityMatch ? quantityMatch : "N/A").replace(",", ".")); // Convert to decimal
  43. const unit = title.replace(/^\D+/g, "").replace(/[^a-z]/gi, "");
  44.  
  45. // Extract the price elements
  46. if (card.querySelector(".flex.items-center.gap-1"))
  47. {
  48. const priceElementSingle = card.querySelector(".text-sm.text-neutral-500.font-bold");
  49. const priceElementBulk = card.querySelector(".flex.items-center.flex-wrap");
  50. const priceSingle = parseFloat(
  51. (priceElementSingle ? priceElementSingle.textContent.trim(): "N/A")
  52. .replace(/[^\d,]/g, "")
  53. .replace(",", ".")
  54. )
  55. const priceBulk = parseFloat(
  56. (priceElementBulk ? priceElementBulk.textContent.trim(): "N/A")
  57. .replace(/[^\d,]/g, "")
  58. .replace(",", ".")
  59. )
  60. console.log("caso1");
  61.  
  62.  
  63.  
  64.  
  65. const logMessage = `Varejo ${(priceSingle/quantity).toFixed(2)} por ${unit} Atacado ${(priceBulk/quantity).toFixed(2)} por ${unit}`
  66.  
  67. // Log to the console
  68. console.log(logMessage);
  69.  
  70. // Create a container for the log content
  71. const logContainer = document.createElement("div");
  72. logContainer.className = "product-log";
  73. logContainer.style.marginTop = "10px";
  74. logContainer.style.padding = "5px";
  75. logContainer.style.backgroundColor = "#f0f0f0";
  76. logContainer.style.borderRadius = "4px";
  77. logContainer.style.fontSize = "12px";
  78. logContainer.style.color = "#333";
  79. logContainer.textContent = logMessage;
  80.  
  81. // Append the log container to the product card
  82. card.appendChild(logContainer);
  83. } else {
  84. const priceElementSingle =card.querySelector(".flex.flex-col.gap-1");
  85. const priceSingle = parseFloat(
  86. (priceElementSingle ? priceElementSingle.textContent.trim(): "N/A")
  87. .replace(/[^\d,]/g, "")
  88. .replace(",", ".")
  89. )
  90. const logMessage = `Varejo ${(priceSingle/quantity).toFixed(2)} por ${unit}`
  91. // Log to the console
  92. console.log(logMessage);
  93.  
  94. // Create a container for the log content
  95. const logContainer = document.createElement("div");
  96. logContainer.className = "product-log";
  97. logContainer.style.marginTop = "10px";
  98. logContainer.style.padding = "5px";
  99. logContainer.style.backgroundColor = "#f0f0f0";
  100. logContainer.style.borderRadius = "4px";
  101. logContainer.style.fontSize = "12px";
  102. logContainer.style.color = "#333";
  103. logContainer.textContent = logMessage;
  104.  
  105. // Append the log container to the product card
  106. card.appendChild(logContainer);
  107. }
  108.  
  109.  
  110. ;
  111.  
  112. // Create a log message
  113.  
  114.  
  115. ;
  116.  
  117.  
  118. }
  119. });
  120. } else {
  121. console.log("No product cards found yet.");
  122. }
  123.  
  124. // Reconnect the observer after processing
  125. observer.observe(document.body, { childList: true, subtree: true });
  126. });
  127.  
  128. // Start observing the document body for changes
  129. console.log("Starting MutationObserver...");
  130. observer.observe(document.body, { childList: true, subtree: true });
  131. })());
  132. })();