(() => { const addToCartByLabel_original = { 4731: { 4733: "BOLRUSHPSF", 4734: "BOLRUSHPSI", 4735: "7110266L", }, 4732: { 4735: "SCAPPESHR", 4736: "SCAPPESHW", }, }; let isInProcess = false; const bodyLabels = document.body.className.match(/\bex-l-\d{4}/g)?.map((key) => key.replace("ex-l-", "")) || []; const addToCartByLabel = {}; bodyLabels.forEach((label) => { if (label in addToCartByLabel_original) { const transformed = Object.fromEntries( Object.entries(addToCartByLabel_original[label]).map(([key, value]) => [key, { product_sku: value }]) ); Object.assign(addToCartByLabel, transformed); // merge into final object } }); if (Object.keys(addToCartByLabel).length === 0) return; const productWrapper = document.createElement("div"); productWrapper.classList.add("fetched-product", "hide"); const bodyFooter = document.querySelector("footer.footer"); if (!bodyFooter) return; bodyFooter.append(productWrapper); function createGlobalObject(scriptText) { // Find the variable name (e.g. obj_1093142) const varMatch = scriptText.match(/var\s+(obj_\d+)\s*=/); if (!varMatch) return; const varName = varMatch[1]; // Build $t_item object from assignments const assignmentRegex = /\$t_item\.(\w+)\s*=\s*(.*?);/g; let match; const t_item = {}; while ((match = assignmentRegex.exec(scriptText))) { let key = match[1]; let value = match[2].trim(); // Remove quotes if it's a string if (/^["'].*["']$/.test(value)) { value = value.slice(1, -1); } else if (value === "JSON.parse('[]')") { value = []; } else if (!isNaN(value)) { value = Number(value); } t_item[key] = value; } // Create global variable if it doesn’t already exist if (typeof window[varName] === "undefined") { window[varName] = JSON.parse(JSON.stringify(t_item)); //console.log("Created:", varName, window[varName]); } //else { //console.log("Skipped, already exists:", varName); //} } function addItemAuto() { const deleteItem = (key) => { const productId = addToCartByLabel[key].product_id; const cartProduct = document.querySelector(`.minicart .item[data-itemid="${productId}"]`) || document.querySelector(`.item_${productId}`); const miniCartProductDelete = cartProduct.querySelector(".delete"); const cartProductDelete = cartProduct.querySelector(".delete-btn"); if (cartProductDelete) { cartProductDelete.click(); } if (miniCartProductDelete) { miniCartProductDelete.dispatchEvent(new Event("click", { bubbles: true })); } }; const addNewItem = (key) => { const productId = Number(addToCartByLabel[key].product_id); const productAmount = Number(addToCartByLabel[key].product_in_cart); const isInCart = $mini_cart.some((cartItem) => Number(cartItem.item_id) === productId); if (isInCart) { const cartProduct = document.querySelector(`.minicart .item[data-itemid="${productId}"]`) || document.querySelector(`.item_${productId}`); const cartProductInput = cartProduct.querySelector("input.minicart_qty") || cartProduct.querySelector("input.quantity"); if (cartProductInput) { cartProductInput.value = productAmount; cartProductInput.dispatchEvent(new Event("change", { bubbles: true })); } } else { addToCartByLabel[key].product_input.value = productAmount; addToCartByLabel[key].product_button.click(); } }; // assign all products in cart to 0 Object.values(addToCartByLabel).forEach((e) => { e.product_in_cart = 0; e.product_in_cart_add = 0; }); // loop over to check labeled products in a cart $mini_cart.forEach((e) => { const labelIds = e.product_labels?.split(" ") || []; const itemId = Number(e.item_id); const qty = Number(e.item_order_qty); // Update by label const foundId = labelIds.find((id) => addToCartByLabel[id]); if (foundId) { addToCartByLabel[foundId].product_in_cart += qty; } // Update by product_id Object.values(addToCartByLabel).forEach((item) => { if (Number(item.product_id) === itemId) { item.product_in_cart_add += qty; } }); }); // Check if we have duplicates const hasDuplicate = Object.entries(addToCartByLabel).some(([key, item]) => { const productId = Number(item.product_id); const idLength = $mini_cart.filter((cartItem) => Number(cartItem.item_id) === productId).length; if (idLength > 1) { deleteItem(key); return true; } return false; }); if (hasDuplicate) return; //Check object if labeled products more then 0 Object.entries(addToCartByLabel).map(([key, item]) => { if (item.product_in_cart > 0 && item.product_in_cart !== item.product_in_cart_add) { addNewItem(key); } else if (item.product_in_cart === 0 && item.product_in_cart !== item.product_in_cart_add) { deleteItem(key); } }); } function createItems(domObj) { const createOneItem = (productAddToCart) => { const productButton = productAddToCart.querySelector("button.btn-add-to-cart"); if (!productButton) return; productButton.removeAttribute("disabled"); const productSku = productAddToCart.getAttribute("data-sku") || ""; const objKey = Object.keys(addToCartByLabel).find((k) => addToCartByLabel[k].product_sku === productSku); if (!objKey) return; if (addToCartByLabel[objKey].product_button && addToCartByLabel[objKey].product_input && addToCartByLabel[objKey].product_id) { return; } const productInput = productAddToCart.querySelector("input.inputquantity") || {}; const productId = productAddToCart.getAttribute("data-item-id") || ""; if (!(productButton && productInput && productId)) return; addToCartByLabel[objKey].product_button = productButton; addToCartByLabel[objKey].product_input = productInput; addToCartByLabel[objKey].product_id = productId; addToCartByLabel[objKey].product_in_cart = 0; addToCartByLabel[objKey].product_in_cart_add = 0; const productScript = productAddToCart.querySelector(".buttons.group script"); if (!productScript) return; const productScriptText = productScript.textContent; createGlobalObject(productScriptText); productWrapper.append(productAddToCart); productWrapper.classList.add("product-added"); }; let products; if (domObj.querySelector("body.ex-product")) { products = domObj.querySelectorAll(".productaddtocart_container .product"); } if (domObj.querySelector("body.ex-searchresult")) { products = domObj.querySelectorAll(".productsgrid_container .tileparent.product"); } if (!products.length) return; products.forEach((product) => { createOneItem(product); }); } document.addEventListener( "cart-total-update", function (e) { setTimeout(addItemAuto, 100); }, false ); (async () => { const itemsSku = Object.values(addToCartByLabel).flatMap((innerObj) => Object.values(innerObj)); const querySku = `/search?query=${encodeURIComponent(itemsSku.join("+"))}&p=1`; try { const response = await fetch(`${window.location.origin}${querySku}`); const htmlText = await response.text(); const parser = new DOMParser(); const doc = parser.parseFromString(htmlText, "text/html"); createItems(doc); } catch (err) {} })(); })();