> ## 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 Using the 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;
})();

## 同步API Sync API

为确保您的代码在RevIQ脚本加载完成后执行，您可以使用 `reviq` 命令队列。
To ensure that your code runs after the RevIQ script is loaded, you can use the
`reviq` command queue.

<Info>
  RevIQ脚本采用异步加载方式，以避免阻塞页面渲染。请务必确认API已就绪后再进行调用。
  The RevIQ script loads asynchronously in order to not block the page. It's
  important to make sure that the API is ready before you call it.
</Info>

<AccordionGroup>
  <Accordion title="Using Command Queue" icon="js">
    ```js index.js theme={null}
    globalThis.reviq = globalThis.reviq || [];
    reviq.push((reviq) => {
        // 在此编写您的代码 Your code here
        reviq.setKv('foo', 'bar');
    });
    ```
  </Accordion>

  <Accordion title="Using Promise" icon="js">
    您也可以创建一个Promise，在首次调用 `reviq` 命令后resolve。
    Alternatively, you can create a Promise that resolves after the first `reviq`
    command is called.

    ```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>
