Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

9.8 KB
Raw Blame
<!doctype html>
<meta charset="utf-8">
<!--
  Zhihu bot detection harness.

  This is the reusable, single-page probe we use to compare Chromium and
  Moli on the bot-detection-facing surface that the Zhihu challenge keeps
  touching: automation markers, server-side escape markers, document.all,
  reserved window aliases, native-looking globals, navigator/screen/media
  surface, and MediaSource/canvas/webgl essentials.

  Run with:
    chromium --headless --disable-gpu --no-sandbox --allow-file-access-from-files --dump-dom file:///.../moli-renderer-v8/src/script_vm/fixtures/zhihu-bot-detection-harness.html
    target/debug/moli fetch file:///.../moli-renderer-v8/src/script_vm/fixtures/zhihu-bot-detection-harness.html
-->
<pre id="out">boot</pre>
<script>
(function () {
  function safe(fn) {
    try {
      return fn();
    } catch (error) {
      return { error: String(error) };
    }
  }

  function ctorName(value) {
    try {
      return (value && value.constructor && value.constructor.name) || null;
    } catch (_) {
      return null;
    }
  }

  function nativeNamed(value, name) {
    return typeof value === "function" &&
      value.name === name &&
      new RegExp("^function " + name + "\\(").test(String(value)) &&
      /\[native code\]/.test(String(value));
  }

  function namedGetter(owner, key, name) {
    const getter = Object.getOwnPropertyDescriptor(owner, key)?.get;
    return typeof getter === "function" &&
      getter.name === "get " + name &&
      new RegExp("^function get " + name + "\\(").test(String(getter)) &&
      /\[native code\]/.test(String(getter));
  }

  function descriptorSummary(owner, key) {
    const d = Object.getOwnPropertyDescriptor(owner, key);
    return {
      present: !!d,
      enumerable: !!d?.enumerable,
      configurable: !!d?.configurable,
      writable: typeof d?.writable === "boolean" ? d.writable : null,
      hasGetter: typeof d?.get === "function",
      hasSetter: typeof d?.set === "function",
    };
  }

  function namedCollectionShape(value) {
    return {
      ctor: ctorName(value),
      tag: safe(function () { return Object.prototype.toString.call(value); }),
      length: value && typeof value.length === "number" ? value.length : null,
      itemType: typeof value?.item,
      namedItemType: typeof value?.namedItem,
    };
  }

  if (!document.documentElement) {
    const html = document.createElement("html");
    document.appendChild(html);
  }
  if (!document.body) {
    const body = document.createElement("body");
    document.documentElement.appendChild(body);
  }

  for (const id of ["window", "self", "top", "parent", "frames", "probe"]) {
    const node = document.createElement("div");
    node.id = id;
    document.body.appendChild(node);
  }
  const probeNode = document.getElementById("probe");

  const automationKeys = [
    "__phantomas",
    "_phantom",
    "WebPage",
    "fxdriver_id",
    "__fxdriver_unwrapped",
    "ubot",
    "CasperError",
    "casper",
    "$cdc_asdjflasutopfhvcZLmcfl_",
    "__webdriver_script_fn",
    "patchRequire",
  ];
  const serverWindowKeys = [
    "_resourceLoader",
    "_sessionHistory",
    "_virtualConsole",
    "_parent",
    "_origin",
    "_document",
    "process",
    "global",
  ];
  const serverDynamicKeys = [
    "process",
    "global",
    "require",
    "module",
    "exports",
    "__dirname",
    "__filename",
    "executeUserEntryPoint",
    "node",
    "JSDOM",
    "ws",
  ];

  const mediaSource = safe(function () {
    const instance = new MediaSource();
    return {
      ctorType: typeof MediaSource,
      name: MediaSource.name,
      staticName: MediaSource.isTypeSupported && MediaSource.isTypeSupported.name,
      avc1: MediaSource.isTypeSupported('video/mp4; codecs="avc1.42E01E"'),
      vp09: MediaSource.isTypeSupported('video/webm; codecs="vp09.00.10.08"'),
      instanceOf: instance instanceof MediaSource,
      prototypeMatches: Object.getPrototypeOf(instance) === MediaSource.prototype,
      ownKeys: Object.keys(instance),
    };
  });

  const result = {
    windowAliases: safe(function () {
      return {
        windowEqGlobalThis: window === globalThis,
        selfEqGlobalThis: self === globalThis,
        topEqGlobalThis: top === globalThis,
        parentEqGlobalThis: parent === globalThis,
        framesEqGlobalThis: frames === globalThis,
        namedWindowType: typeof document.getElementById("window"),
        namedProbeHit: document.all("probe") === probeNode,
      };
    }),
    automationMarkers: Object.fromEntries(
      automationKeys.map(function (key) { return [key, typeof window[key]]; })
    ),
    serverEscape: {
      window: Object.fromEntries(
        serverWindowKeys.map(function (key) { return [key, typeof window[key]]; })
      ),
      dynamic: Object.fromEntries(
        serverDynamicKeys.map(function (key) {
          return [key, new Function("return typeof " + key)()];
        })
      ),
    },
    globalFunctions: safe(function () {
      return {
        addEventListenerType: typeof addEventListener,
        removeEventListenerType: typeof removeEventListener,
        dispatchEventType: typeof dispatchEvent,
        addEventListenerLength: addEventListener.length,
        removeEventListenerLength: removeEventListener.length,
        dispatchEventLength: dispatchEvent.length,
        blurType: typeof blur,
        blurLength: blur.length,
        findType: typeof find,
        findLength: find.length,
        stopType: typeof stop,
        printType: typeof print,
        openType: typeof open,
        bareMatchesWindowAdd: addEventListener === window.addEventListener,
        ownAddEventListener: Object.prototype.hasOwnProperty.call(globalThis, "addEventListener"),
      };
    }),
    descriptors: safe(function () {
      return {
        window: descriptorSummary(globalThis, "window"),
        document: descriptorSummary(globalThis, "document"),
        self: descriptorSummary(globalThis, "self"),
        parent: descriptorSummary(globalThis, "parent"),
        frames: descriptorSummary(globalThis, "frames"),
      };
    }),
    nativeSurface: safe(function () {
      return {
        open: nativeNamed(window.open, "open"),
        print: nativeNamed(window.print, "print"),
        stop: nativeNamed(window.stop, "stop"),
        blur: nativeNamed(window.blur, "blur"),
        find: nativeNamed(window.find, "find"),
        addEventListener: nativeNamed(window.addEventListener, "addEventListener"),
        removeEventListener: nativeNamed(window.removeEventListener, "removeEventListener"),
        dispatchEvent: nativeNamed(window.dispatchEvent, "dispatchEvent"),
        getRandomValues: nativeNamed(crypto.getRandomValues, "getRandomValues"),
        isTypeSupported: nativeNamed(MediaSource.isTypeSupported, "isTypeSupported"),
        documentAllGetter: namedGetter(Document.prototype, "all", "all"),
        webdriverGetter: namedGetter(Navigator.prototype, "webdriver", "webdriver"),
        screenAvailWidthGetter: namedGetter(Screen.prototype, "availWidth", "availWidth"),
      };
    }),
    documentAll: safe(function () {
      const live = document.all;
      const detached = new Document().all();
      return {
        type: typeof live,
        looseUndefined: live == undefined,
        strictUndefined: live === undefined,
        lengthType: typeof live?.length,
        tag: Object.prototype.toString.call(live),
        ctor: ctorName(live),
        detachedType: typeof detached,
        detachedLengthType: typeof detached?.length,
        detachedTag: Object.prototype.toString.call(detached),
        detachedString: String(detached),
        namedHit: document.all("probe") === probeNode,
      };
    }),
    navigatorProfile: safe(function () {
      return {
        userAgent: navigator.userAgent,
        appVersion: navigator.appVersion,
        vendor: navigator.vendor,
        platform: navigator.platform,
        language: navigator.language,
        languages: Array.from(navigator.languages || []),
        webdriver: navigator.webdriver,
        hardwareConcurrency: navigator.hardwareConcurrency,
        maxTouchPoints: navigator.maxTouchPoints,
        deviceMemory: navigator.deviceMemory,
        pdfViewerEnabled: navigator.pdfViewerEnabled,
      };
    }),
    pluginsMimeTypes: safe(function () {
      return {
        plugins: namedCollectionShape(navigator.plugins),
        mimeTypes: namedCollectionShape(navigator.mimeTypes),
      };
    }),
    screenTouch: safe(function () {
      return {
        width: screen.width,
        height: screen.height,
        availWidth: screen.availWidth,
        availHeight: screen.availHeight,
        availLeft: screen.availLeft,
        availTop: screen.availTop,
        colorDepth: screen.colorDepth,
        pixelDepth: screen.pixelDepth,
        orientationType: screen.orientation && screen.orientation.type,
        touchType: typeof Touch,
        touchEventType: typeof TouchEvent,
        ontouchstartType: typeof window.ontouchstart,
      };
    }),
    canvasWebgl: safe(function () {
      const canvas = document.createElement("canvas");
      const ctx2d = canvas.getContext && canvas.getContext("2d");
      const gl = canvas.getContext && canvas.getContext("webgl");
      return {
        canvasCtor: ctorName(canvas),
        canvasTag: canvas.tagName,
        getContextType: typeof canvas.getContext,
        toDataURLType: typeof canvas.toDataURL,
        context2dCtor: ctorName(ctx2d),
        webglCtor: ctorName(gl),
        webglGetParameterType: typeof gl?.getParameter,
      };
    }),
    mediaSource: mediaSource,
    storage: safe(function () {
      return {
        localStorageType: typeof localStorage,
        sessionStorageType: typeof sessionStorage,
        localStorageGetItemType: typeof localStorage?.getItem,
        sessionStorageGetItemType: typeof sessionStorage?.getItem,
      };
    }),
  };

  document.getElementById("out").textContent = JSON.stringify(result, null, 2);
})();
</script>