Global Protect Articles

document.addEventListener('DOMContentLoaded', () => { console.log(`[Iframe ${window.location.pathname}] DOM Loaded. Attaching link listeners.`); document.body.addEventListener('click', (event) => { // Use event delegation - listen on the body, check if the target was a link let targetElement = event.target; // Traverse up the DOM tree if the click was inside a link (e.g., on an tag) while (targetElement && targetElement.tagName !== 'A') { targetElement = targetElement.parentElement; } // Check if we found an anchor tag if (targetElement && targetElement.tagName === 'A') { const link = targetElement; const href = link.getAttribute('href'); console.log(`[Iframe ${window.location.pathname}] Link clicked: href="./downloaded_linked_pages/${href}"`); // --- Filter out unwanted links --- if (!href || href.startsWith('#') || href.startsWith('javascript:') || link.target === '_blank') { console.log(`[Iframe ${window.location.pathname}] Ignoring link (anchor, javascript, blank target, or no href).`); return; // Let the browser handle these normally (or do nothing) } // --- Check if it's an external link --- try { // Resolve the href relative to the current page's URL to get the absolute target URL const targetUrl = new URL(href, window.location.href); // Compare the origin of the target URL with the origin of the current page if (targetUrl.origin !== window.location.origin) { console.log(`[Iframe ${window.location.pathname}] Ignoring external link: ${href}`); return; // Let the browser handle external links normally (open in new tab if target="_blank") } } catch (e) { // Handle cases where href is not a valid URL fragment (e.g., "mailto:") if (!href.startsWith('/') && !href.startsWith('.') && !href.includes(':')) { // Assume it's a relative path if it doesn't look like an absolute path, fragment, or protocol console.log(`[Iframe ${window.location.pathname}] Treating potentially invalid href as relative: ${href}`); } else { console.warn(`[Iframe ${window.location.pathname}] Could not parse URL or non-HTTP link: ${href}`, e); return; // Ignore invalid URLs or other protocols for now } } // --- Prevent default navigation for internal links --- event.preventDefault(); console.log(`[Iframe ${window.location.pathname}] Prevented default navigation for: ${href}`); // --- Calculate absolute path and determine label --- // Use URL constructor for robust path resolution relative to current page const absoluteUrl = new URL(href, window.location.href); // Get path relative to origin (e.g., /topics/core/sub.html or /index.html) const absolutePath = absoluteUrl.pathname; // Use link text as label, trim whitespace const label = link.textContent?.trim() || absolutePath.substring(absolutePath.lastIndexOf('/') + 1) || 'Link'; // Fallback label // --- Send message to parent --- const message = { type: 'navigate', // Remove leading slash from pathname for consistency with how paths are stored in index.html state path: absolutePath.startsWith('/') ? absolutePath.substring(1) : absolutePath, label: label }; console.log(`[Iframe ${window.location.pathname}] Sending message to parent:`, message); // IMPORTANT: Use window.location.origin in production instead of '*' for security window.parent.postMessage(message, '*'); } // End if targetElement is 'A' }); // End body click listener });