(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); })(window,document,'script','dataLayer','GTM-P9L7B2NK'); // Use an Immediately Invoked Function Expression (IIFE) for safety. (function() { // --- LWC Timing Fix: Use MutationObserver to wait for the host element to appear --- // The known stable class name for the grid layout component host. var TARGET_SELECTOR = 'div.flexi-grid-flexigridlayout2'; // Function containing the core wrapping logic function executeImageWrapping() { console.log('--- EXECUTING WRAPPING LOGIC VIA OBSERVER ---'); // 1. Target the known stable component wrapper class. var containerHost = document.querySelector(TARGET_SELECTOR); if (!containerHost) { console.log('Host element not found, re-checking later.'); return; // Exit if the element still isn't fully ready } // --- NEW AGGRESSIVE SEARCH STRATEGY: Attempt to pierce Shadow DOM --- // We look for a collection of potential Shadow Host elements that contain the image var hostCandidates = containerHost.querySelectorAll('div[class*="image-wrapper"], c-sp-featured-category'); var pictureElements = []; // Try to access elements either directly or via a shadowRoot Array.prototype.forEach.call(hostCandidates, function(host) { var searchRoot = host.shadowRoot || host; // Query for picture tags inside the host or its shadow root var foundPictures = searchRoot.querySelectorAll('picture'); Array.prototype.forEach.call(foundPictures, function(pic) { // Collect all found picture elements into the main array pictureElements.push(pic); }); }); console.log('Total elements found (Aggressive Search):', pictureElements.length); // 2. Iterate over all found picture elements. Array.prototype.forEach.call(pictureElements, function(pictureElement, index) { var linkToClone = null; // --- 3. Find the Link Sibling (Traversal remains the same, assuming it's outside the Shadow Root) --- var imageWrapper = pictureElement.parentNode; // We must traverse UP from the picture element until we exit the shadow root (if one exists) // and then continue the up-up-down traversal to find the link (which is outside the shadow root). var currentElement = pictureElement; while(currentElement.parentNode) { if (currentElement.parentNode.tagName === 'C-SP-FEATURED-CATEGORY' || currentElement.parentNode.classList.contains('flexi-grid-flexigridlayout2')) { // We have likely hit the boundary of the external DOM. break; } currentElement = currentElement.parentNode; } // Now currentElement is the host component itself or a container just outside the shadow root. var grandParent = currentElement.parentNode.parentNode; // Check if we found a valid grandparent (this means we successfully climbed out of the LWC) if (grandParent) { var siblings = grandParent.children; // Look through the grandparent's children for the link component for (var i = 0; i < siblings.length; i++) { var linkCandidate = siblings[i].querySelector('a[href]'); if (linkCandidate) { linkToClone = linkCandidate; break; } } } // Log the result of the traversal attempt console.log('Item ' + index + ' - Link Found:', !!linkToClone); // 4. If both the link and the picture element are found, proceed with wrapping. if (linkToClone && pictureElement) { console.log('Item ' + index + ' - SUCCESS: Performing wrap.'); // Final wrapping logic var url = linkToClone.getAttribute('href'); var target = linkToClone.getAttribute('target') || '_self'; var title = linkToClone.getAttribute('title') || ''; var newLink = document.createElement('a'); newLink.href = url; newLink.target = target; newLink.title = title; newLink.style.display = 'block'; newLink.style.position = 'absolute'; newLink.style.top = '0'; newLink.style.left = '0'; newLink.style.width = '100%'; newLink.style.height = '100%'; newLink.style.zIndex = '10'; // The element to wrap is the parent of the tag (imageWrapper) // We assume the wrapping needs to happen around the visible image container structure. var elementToWrap = imageWrapper; elementToWrap.parentNode.insertBefore(newLink, elementToWrap); newLink.appendChild(elementToWrap); } else { console.warn('Item ' + index + ' - FAILURE: Skipping item due to missing link or image element.'); } }); console.log('--- WRAPPING COMPLETE ---'); } // Check if the element already exists on page load (in case of fast rendering) if (document.querySelector(TARGET_SELECTOR)) { executeImageWrapping(); return; } // Otherwise, create an observer to wait for the element to be inserted. var observer = new MutationObserver(function(mutations, obs) { if (document.querySelector(TARGET_SELECTOR)) { // Element found! Disconnect the observer and run the script. obs.disconnect(); executeImageWrapping(); } }); // Start observing the entire document body for changes in the subtree (new elements being added). observer.observe(document.body, { childList: true, // Watch for new nodes added subtree: true // Watch the entire DOM tree }); })();