> ## 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.

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

大约40%的网络用户使用广告拦截工具，这会显著降低广告收入。我们提供了一套**Adblock API**，让您能够提示使用广告拦截的用户来支持网站。
Roughly 40% of web users use adblock, which can significantly reduce ad revenue.
We offer an **Adblock API** which enables you to prompt adblock users to support
the site.

该API作为独立模块提供，只需在页面中额外引入一个script标签即可启用。该模块设计轻量、加载迅速，gzip压缩后体积**不足0.3kB**，且无任何外部依赖。
This API is available as a separate module that you can add to your site by
including an additional script tag. We designed this module to be fast and
lightweight, with a gzipped size of **under 0.3 kB** and requiring no external
dependencies.

## 启用Adblock API模块 Enable Wrapper Adblock API

将以下Adblock API模块代码添加到网页的`<head>`标签中。我们为其添加了`id`，方便您识别其用途
Add the Adblock API Module code to the \<head> section of your webpage. We put an `id` so you can remember what
it's for :)

```html theme={null}
<script id="rev-adblock-api">
  (function () {
    var e = `rev_adblock`,
      t = localStorage,
      n = atob(
        `aHR0cHM6Ly9wYWdlYWQyLmdvb2dsZXN5bmRpY2F0aW9uLmNvbS9wYWdlYWQvanMvYWRzYnlnb29nbGUuanM`,
      ),
      r = fetch(n, { method: `HEAD` })
        .then((e) => !(e.ok && !e.redirected))
        .catch(() => !0)
        .then((n) => n && (t[e] = !0)),
      i = (globalThis.reviq ||= []);
    ((i.checkAdblock = () => r),
      (i.onAdblock = (e) => r.then((t) => t && e())),
      i.push((n) => r.then(() => t[e] && n.setKv(e, 1))));
  })();
</script>
```

## 使用Adblock API Using the Adblock API

<Info>
  注意：这些函数**不应**通过命令队列（`reviq.push(cmd)`）调用。Adblock API
  模块加载完成后，这些函数即可立即使用。 Note: You should **NOT** use the
  command queue (`reviq.push(cmd)`) for these functions. These functions are
  always available immediately after the Adblock API Module is loaded.
</Info>

假设您有一个名为`showAdblockModal`的函数，用于向用户显示弹窗，提示其关闭广告拦截。
Suppose you have some function `showAdblockModal` that shows a modal asking
users to disable adblock.

`reviq.onAdblock(cb)`: 检测到广告拦截时，执行回调函数`cb` runs a callback `cb` if adblock is detected

```js theme={null}
reviq.onAdblock(() => {
  // 仅在检测到广告拦截时执行 This only runs if adblock is detected
  showAdblockModal();
});
```

`checkAdblock()`：需配合`await`使用，检测到广告拦截时返回`true`，否则返回`false` must be awaited and returns true if adblock is detected, false otherwise

```js theme={null}
// 检测广告拦截 Check for adblock
const hasAdblock = await reviq.checkAdblock();
if (hasAdblock) {
  showAdblockModal();
}
```
