Skip to content

A continuation of HomeBox the inventory and organization system built for the Home User

3.4 KB
Raw Blame
---
const { hasSidebar } = Astro.locals.starlightRoute;

import LanguageSelect from 'starlight-theme-nova/components/LanguageSelect.astro';
import Default from '@astrojs/starlight/components/Header.astro';
---

<Default>
  <Fragment slot="before-title">
    <!-- Starlight will handle all the layout -->
  </Fragment>
</Default>

<script is:inline>
  (() => {
    const RAW_GITHUB_HOST = 'raw.githubusercontent.com';
    const RAW_GITHUB_FALLBACK_ORIGIN = 'https://demo.homebox.software';

    const resolveOrigin = (hostname) => {
      if (hostname === RAW_GITHUB_HOST) {
        return RAW_GITHUB_FALLBACK_ORIGIN;
      }

      return window.location.origin;
    };

    const normalizeApiUrl = (value) => {
      if (typeof value !== 'string' || value.length === 0) {
        return value;
      }

      let parsed;
      try {
        parsed = new URL(value, window.location.origin);
      } catch {
        return value;
      }

      if (!parsed.pathname.startsWith('/api')) {
        return value;
      }

      const targetOrigin = resolveOrigin(parsed.hostname);
      return `${targetOrigin}${parsed.pathname}${parsed.search}${parsed.hash}`;
    };

    const normalizeEmbeddedRawUrl = (value) => {
      if (typeof value !== 'string' || !value.includes(RAW_GITHUB_HOST)) {
        return value;
      }

      return value.replace(
        /https?:\/\/raw\.githubusercontent\.com(\/api[^\s"'<>]*)/g,
        `${RAW_GITHUB_FALLBACK_ORIGIN}$1`
      );
    };

    const updateElementValue = (el) => {
      if (!(el instanceof HTMLInputElement || el instanceof HTMLOptionElement)) {
        return;
      }

      const normalized = normalizeEmbeddedRawUrl(normalizeApiUrl(el.value));
      if (normalized === el.value) {
        return;
      }

      el.value = normalized;
      el.dispatchEvent(new Event('input', { bubbles: true }));
      el.dispatchEvent(new Event('change', { bubbles: true }));
    };

    const updateLinkTargets = (el) => {
      if (!(el instanceof HTMLAnchorElement || el instanceof HTMLImageElement)) {
        return;
      }

      for (const attr of ['href', 'src']) {
        const current = el.getAttribute(attr);
        if (!current) {
          continue;
        }

        const normalized = normalizeEmbeddedRawUrl(normalizeApiUrl(current));
        if (normalized !== current) {
          el.setAttribute(attr, normalized);
        }
      }
    };

    const updateTextNode = (node) => {
      const current = node.textContent;
      if (!current) {
        return;
      }

      const normalized = normalizeEmbeddedRawUrl(current);
      if (normalized !== current) {
        node.textContent = normalized;
      }
    };

    const applyApiUrlGuard = () => {
      if (!window.location.pathname.includes('/api')) {
        return;
      }

      for (const el of document.querySelectorAll('input, option')) {
        updateElementValue(el);
      }

      for (const el of document.querySelectorAll('a, img')) {
        updateLinkTargets(el);
      }

      const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT);
      let node = walker.nextNode();
      while (node) {
        updateTextNode(node);
        node = walker.nextNode();
      }
    };

    const observer = new MutationObserver(applyApiUrlGuard);

    window.addEventListener('load', () => {
      applyApiUrlGuard();
      observer.observe(document.body, { childList: true, subtree: true });
    });
  })();
</script>