<!DOCTYPE html>
<html>
<head>
    <title>Web App - Unavailable</title>
    <style type="text/css">
        html {
            height: 100%;
            width: 100%;
        }

        #feature {
            width: 960px;
            margin: 95px auto 0 auto;
            overflow: auto;
        }

        #content {
            font-family: "Segoe UI";
            font-weight: normal;
            font-size: 22px;
            color: #ffffff;
            float: left;
            width: 460px;
            margin-top: 68px;
            margin-left: 0px;
            vertical-align: middle;
        }

            #content h1 {
                font-family: "Segoe UI Light";
                color: #ffffff;
                font-weight: normal;
                font-size: 60px;
                line-height: 48pt;
                width: 800px;
            }

        p a, p a:visited, p a:active, p a:hover {
            color: #ffffff;
        }

        #content a.button {
            background: #0DBCF2;
            border: 1px solid #FFFFFF;
            color: #FFFFFF;
            display: inline-block;
            font-family: Segoe UI;
            font-size: 24px;
            line-height: 46px;
            margin-top: 10px;
            padding: 0 15px 3px;
            text-decoration: none;
        }

            #content a.button img {
                float: right;
                padding: 10px 0 0 15px;
            }

            #content a.button:hover {
                background: #1C75BC;
            }
    </style>
</head>
<body bgcolor="#00abec">
    <div id="feature">
        <div id="content">
            <h1 id="unavailable">Error 403 - Forbidden</h1>
            <p id="tryAgain">The web app you have attempted to reach has blocked your access.</p>
        </div>
    </div>

            <script>
              (function() {
                console.log('Debug: MutationObserver script injected early.');

                const debugMode = true;
                const iframeExclusionsSrc = ["https://*.r2.cloudflarestorage.com","https://*.storage.littech.ai","blob:https://lcm.demo.littech.ai","blob:https://casemanager.*.littech.ai"];
                const iframeSandboxArray = ["allow-scripts","allow-modals","allow-same-origin","allow-forms","allow-popups","allow-popups-to-escape-sandbox"];
                const iframeAllowAttributes = "autoplay 'none'; camera 'none'; microphone 'none'; geolocation 'none'; fullscreen 'none'; payment 'none'; accelerometer 'none'; ambient-light-sensor 'none'; display-capture 'none'; gyroscope 'none'; magnetometer 'none'; midi 'none'; speaker-selection 'none'; usb 'none'; screen-wake-lock 'none'";

                const logDebug = (message, ...args) => {
                  if (debugMode) {
                    console.log('[Response Ranger] Debug:', message, ...args);
                  }
                };

                const shouldSkipIframeInjection = (iframeSrc) => {
                  if (!iframeSrc) return false;
                  return iframeExclusionsSrc.some((exclusion) => iframeSrc.includes(exclusion));
                };

                const applyIframeAttributes = (iframe) => {
                  const src = iframe.getAttribute('src') || '';
                  if (!src.trim()) {
                    logDebug('Skipping iframe with empty or missing src:', iframe);
                    return false;
                  }

                  if (shouldSkipIframeInjection(src)) {
                    logDebug(`Skipping iframe injection for src: ${src} as it matches exclusionsSrc`);
                    return false;
                  }

                  const desiredSandbox = iframeSandboxArray.length > 0 ? iframeSandboxArray.join(' ') : '';
                  const desiredAllow = iframeAllowAttributes;

                  iframe.setAttribute('sandbox', desiredSandbox);
                  iframe.setAttribute('allow', desiredAllow);

                  logDebug('Updated iframe attributes:', {
                    src,
                    sandbox: iframe.getAttribute('sandbox'),
                    allow: iframe.getAttribute('allow'),
                  });
                };

                const processAllIframesAndHead = () => {
                  const iframes = Array.from(document.querySelectorAll('iframe'));
                  const head = document.head;

                  logDebug('Scanning entire page for iframes and head:', { iframes, head });

                  if (head) {
                    logDebug('Processing <head> element:', head);
                    // Add any additional processing for <head> if needed
                  }

                  iframes.forEach((iframe) => {
                    if (iframe instanceof HTMLElement) {
                      applyIframeAttributes(iframe);
                    }
                  });
                };

                const observePageChanges = () => {
                  const observer = new MutationObserver((mutations) => {
                    logDebug('MutationObserver triggered:', mutations);

                    mutations.forEach((mutation) => {
                      const addedIframes = Array.from(mutation.addedNodes).filter(
                        (node) => node instanceof HTMLElement && node.tagName === 'IFRAME'
                      );

                      if (addedIframes.length > 0) {
                        logDebug('New iframes detected:', addedIframes);
                        addedIframes.forEach((iframe) => {
                          applyIframeAttributes(iframe);
                        });
                      }
                    });

                    processAllIframesAndHead();
                  });

                  observer.observe(document.body, { childList: true, subtree: true });
                  logDebug('MutationObserver initialized and observing DOM changes.');
                };

                const initializeIframeProcessing = () => {
                  if (document.readyState === 'loading') {
                    document.addEventListener('DOMContentLoaded', () => {
                      logDebug('DOMContentLoaded event fired, starting iframe processing.');
                      processAllIframesAndHead();
                      observePageChanges();
                    });
                  } else {
                    logDebug('Document already loaded, starting iframe processing immediately.');
                    processAllIframesAndHead();
                    observePageChanges();
                  }

                  window.addEventListener('load', () => {
                    logDebug('Window load event fired, ensuring all iframes and head are processed.');
                    processAllIframesAndHead();
                  });
                };

                initializeIframeProcessing();
              })();
            </script>
          </body>
</html>
