> ## Documentation Index
> Fetch the complete documentation index at: https://docs.rev.iq/llms.txt
> Use this file to discover all available pages before exploring further.

# JavaScript API 사용법

export const domain = (() => {
  const placeholder = "your-domain.com";
  if (typeof window !== "undefined" && !globalThis.__revDomainSetup) {
    globalThis.__revDomainSetup = true;
    function textNodes(node) {
      if (node.nodeType === Node.TEXT_NODE) return [node];
      const nodes = [];
      for (const child of node.childNodes) {
        nodes.push(...textNodes(child));
      }
      return nodes;
    }
    function anchorNodes(node) {
      if (node.nodeType === Node.ELEMENT_NODE && node.tagName === "A") return [node];
      const nodes = [];
      for (const child of node.childNodes) {
        nodes.push(...anchorNodes(child));
      }
      return nodes;
    }
    const domain = globalThis.__revDomain || new URLSearchParams(globalThis.location.search).get('domain') || "yourdomain.com";
    globalThis.__revDomain = domain;
    if (globalThis.__revDomainObs) return;
    const obs = globalThis.__revDomainObs || new MutationObserver(mutations => {
      for (const {addedNodes, removedNodes, target} of mutations) {
        for (const node of [...addedNodes, target]) {
          if (node instanceof HTMLScriptElement) continue;
          if (node instanceof HTMLStyleElement) continue;
          for (const textNode of textNodes(node)) {
            if (textNode.nodeValue && textNode.nodeValue.includes(placeholder)) {
              setTimeout(() => {
                textNode.nodeValue = textNode.nodeValue.replace(placeholder, domain);
              }, 100);
            }
          }
          for (const anchorNode of anchorNodes(node)) {
            if (anchorNode.href && anchorNode.href.includes(placeholder)) {
              setTimeout(() => {
                anchorNode.href = anchorNode.href.replace(placeholder, domain);
              }, 100);
            }
          }
        }
      }
    });
    obs.observe(document.body, {
      childList: true,
      subtree: true
    });
  }
  return placeholder;
})();

## Sync API

RevIQ 스크립트가 로드된 후 코드가 실행되도록 하려면 `reviq` 커맨드 큐를
사용할 수 있습니다.

<Info>
  RevIQ 스크립트는 페이지를 차단하지 않도록 비동기적으로 로드됩니다.
  API를 호출하기 전에 API가 준비되었는지 확인하는 것이 중요합니다.
</Info>

<AccordionGroup>
  <Accordion title="커맨드 큐 사용" icon="js">
    ```js index.js theme={null}
    globalThis.reviq = globalThis.reviq || [];
    reviq.push((reviq) => {
        // 여기에 코드를 작성하세요
        reviq.setKv('foo', 'bar');
    });
    ```
  </Accordion>

  <Accordion title="Promise 사용" icon="js">
    또는 첫 번째 `reviq` 커맨드가 호출된 후 resolve되는 Promise를 생성할 수 있습니다.

    ```js index.js theme={null}
    const reviqReady = new Promise((resolve) => {
        reviq = reviq || [];
        reviq.push((reviq) => {
            resolve(reviq);
        });
    });
    async function doSetKv() {
        const reviq = await reviqReady;
        reviq.setKv('foo', 'bar');
    }
    ```
  </Accordion>
</AccordionGroup>
