mirror of
https://github.com/CloakHQ/CloakBrowser.git
synced 2026-06-23 11:41:46 +02:00
feat: add page.stealth_evaluate() for undetectable JS execution (#108)
Extract CDP isolated world classes from humanize layer into standalone stealth_eval module. Attach page.stealth_evaluate(expression) to every page automatically — runs JS in a CDP isolated world with clean Error.stack traces and full variable isolation from main world JS.
This commit is contained in:
@@ -177,6 +177,24 @@ if (newVersion) console.log(`Updated to ${newVersion}`);
|
||||
| TLS fingerprint | Mismatch | **Identical to Chrome** |
|
||||
| | | **Tested against 30+ detection sites** |
|
||||
|
||||
## Stealth Evaluate
|
||||
|
||||
`page.stealthEvaluate(expression)` runs JavaScript in a CDP isolated world instead of Playwright's main-world `evaluate()`. This produces clean `Error.stack` traces and full variable isolation from page JS.
|
||||
|
||||
```typescript
|
||||
const browser = await launch();
|
||||
const page = await browser.newPage();
|
||||
await page.goto('https://example.com');
|
||||
|
||||
// Stealth — clean stack trace, invisible to page JS
|
||||
const title = await page.stealthEvaluate('document.title');
|
||||
|
||||
// Regular evaluate — unchanged, use for DOM writes
|
||||
await page.evaluate(() => { document.body.style.display = 'none'; });
|
||||
```
|
||||
|
||||
Always available on every page — no flag needed. Returns JSON-serializable values only. The isolated world context auto-recreates after navigation.
|
||||
|
||||
## Configuration
|
||||
|
||||
| Env Variable | Default | Description |
|
||||
|
||||
+9
-101
@@ -19,6 +19,7 @@ import { HumanConfig, resolveConfig, rand, randRange, sleep } from './config.js'
|
||||
import { RawMouse, RawKeyboard, humanMove, humanClick, clickTarget, humanIdle } from './mouse.js';
|
||||
import { humanType } from './keyboard.js';
|
||||
import { scrollToElement } from './scroll.js';
|
||||
import { StealthEval } from '../stealth-eval.js';
|
||||
|
||||
export { HumanConfig, resolveConfig } from './config.js';
|
||||
export { humanMove, humanClick, clickTarget, humanIdle } from './mouse.js';
|
||||
@@ -29,102 +30,7 @@ export { scrollToElement } from './scroll.js';
|
||||
const SELECT_ALL = process.platform === 'darwin' ? 'Meta+a' : 'Control+a';
|
||||
|
||||
|
||||
// ============================================================================
|
||||
// CDP Isolated World — stealth DOM evaluation
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Manages a CDP isolated execution context for DOM reads.
|
||||
* Produces clean Error.stack traces (no 'eval at evaluate :302:')
|
||||
* and is invisible to querySelector monkey-patches in the main world.
|
||||
*
|
||||
* Context ID is invalidated on navigation and auto-recreated on next call.
|
||||
*/
|
||||
class StealthEval {
|
||||
private cdp: CDPSession | null = null;
|
||||
private contextId: number | null = null;
|
||||
private page: Page;
|
||||
|
||||
constructor(page: Page) {
|
||||
this.page = page;
|
||||
}
|
||||
|
||||
private async ensureCdp(): Promise<CDPSession> {
|
||||
if (!this.cdp) {
|
||||
this.cdp = await this.page.context().newCDPSession(this.page);
|
||||
}
|
||||
return this.cdp;
|
||||
}
|
||||
|
||||
private async createWorld(): Promise<number> {
|
||||
const cdp = await this.ensureCdp();
|
||||
const tree = await cdp.send('Page.getFrameTree');
|
||||
const frameId = tree.frameTree.frame.id;
|
||||
const result = await cdp.send('Page.createIsolatedWorld', {
|
||||
frameId,
|
||||
worldName: '',
|
||||
grantUniveralAccess: true,
|
||||
});
|
||||
const ctxId = result.executionContextId;
|
||||
this.contextId = ctxId;
|
||||
return ctxId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate a JS expression in the isolated world.
|
||||
* Auto-recreates the world if the context was invalidated (navigation).
|
||||
* Returns the result value, or undefined on failure.
|
||||
*/
|
||||
async evaluate(expression: string): Promise<any> {
|
||||
if (this.contextId === null) {
|
||||
await this.createWorld();
|
||||
}
|
||||
|
||||
for (let attempt = 0; attempt < 2; attempt++) {
|
||||
try {
|
||||
const cdp = await this.ensureCdp();
|
||||
const result = await cdp.send('Runtime.evaluate', {
|
||||
expression,
|
||||
contextId: this.contextId!,
|
||||
returnByValue: true,
|
||||
});
|
||||
|
||||
if (result.exceptionDetails) {
|
||||
// Context was likely invalidated by navigation
|
||||
if (attempt === 0) {
|
||||
await this.createWorld();
|
||||
continue;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return result.result?.value;
|
||||
} catch {
|
||||
if (attempt === 0) {
|
||||
this.contextId = null;
|
||||
try {
|
||||
await this.createWorld();
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/** Mark context as stale — call after navigation. */
|
||||
invalidate(): void {
|
||||
this.contextId = null;
|
||||
}
|
||||
|
||||
/** Get the underlying CDP session (reused for Input.dispatchKeyEvent etc.). */
|
||||
async getCdpSession(): Promise<CDPSession> {
|
||||
return this.ensureCdp();
|
||||
}
|
||||
}
|
||||
// StealthEval is defined in stealth-eval.ts and imported at the top of this file.
|
||||
|
||||
|
||||
// ============================================================================
|
||||
@@ -163,7 +69,8 @@ async function isInputElement(
|
||||
|| el.getAttribute('contenteditable') === 'true';
|
||||
})()
|
||||
`);
|
||||
return !!result;
|
||||
if (result !== undefined && result !== null) return !!result;
|
||||
// undefined/null = CDP failed, fall through to page.evaluate
|
||||
} catch {
|
||||
// Fall through to page.evaluate
|
||||
}
|
||||
@@ -197,7 +104,8 @@ async function isSelectorFocused(
|
||||
return el === document.activeElement;
|
||||
})()
|
||||
`);
|
||||
return !!result;
|
||||
if (result !== undefined && result !== null) return !!result;
|
||||
// undefined/null = CDP failed, fall through to page.evaluate
|
||||
} catch {
|
||||
// Fall through to page.evaluate
|
||||
}
|
||||
@@ -246,9 +154,9 @@ function patchPage(page: Page, cfg: HumanConfig, cursor: CursorState): void {
|
||||
(page as any)._original = originals;
|
||||
(page as any)._humanCfg = cfg;
|
||||
|
||||
// --- Stealth infrastructure ---
|
||||
const stealth = new StealthEval(page);
|
||||
(page as any)._stealth = stealth;
|
||||
// --- Stealth infrastructure (reuse if already attached by stealth-eval) ---
|
||||
const stealth = (page as any)._stealthWorld ?? new StealthEval(page);
|
||||
(page as any)._stealthWorld = stealth;
|
||||
|
||||
// CDP session for shift symbol typing (lazy-initialized, reuses stealth's session)
|
||||
let cdpSession: CDPSession | null = null;
|
||||
|
||||
@@ -67,6 +67,10 @@ export async function launch(options: LaunchOptions = {}): Promise<Browser> {
|
||||
patchBrowser(browser, cfg);
|
||||
}
|
||||
|
||||
// Stealth evaluate — always attached
|
||||
const { patchBrowser: patchStealthEval } = await import('./stealth-eval.js');
|
||||
patchStealthEval(browser);
|
||||
|
||||
return browser;
|
||||
}
|
||||
|
||||
@@ -132,6 +136,10 @@ export async function launchContext(
|
||||
patchContext(context, cfg);
|
||||
}
|
||||
|
||||
// Stealth evaluate — always attached
|
||||
const { patchContext: patchStealthEvalCtx } = await import('./stealth-eval.js');
|
||||
patchStealthEvalCtx(context);
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
@@ -197,6 +205,10 @@ export async function launchPersistentContext(
|
||||
patchContext(context, cfg);
|
||||
}
|
||||
|
||||
// Stealth evaluate — always attached
|
||||
const { patchContext: patchStealthEvalCtx2 } = await import('./stealth-eval.js');
|
||||
patchStealthEvalCtx2(context);
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
/**
|
||||
* Stealth evaluate — run JS in a CDP isolated world.
|
||||
*
|
||||
* Provides page.stealthEvaluate(expression) on every page returned by
|
||||
* cloakbrowser launch functions. Produces clean Error.stack traces (no
|
||||
* `eval at evaluate :302:` leak) and full variable isolation from main
|
||||
* world JS. Context auto-recreates after navigation.
|
||||
*
|
||||
* The same StealthEval instances are reused by the humanize layer
|
||||
* (human/index.ts) for stealth DOM queries.
|
||||
*/
|
||||
|
||||
import type { Browser, BrowserContext, Page, CDPSession } from 'playwright-core';
|
||||
|
||||
|
||||
// ============================================================================
|
||||
// Isolated world class
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Manages a CDP isolated execution context for DOM reads.
|
||||
* Produces clean Error.stack traces (no 'eval at evaluate :302:')
|
||||
* and is invisible to querySelector monkey-patches in the main world.
|
||||
*
|
||||
* Context ID is invalidated on navigation and auto-recreated on next call.
|
||||
*/
|
||||
export class StealthEval {
|
||||
private cdp: CDPSession | null = null;
|
||||
private contextId: number | null = null;
|
||||
private page: Page;
|
||||
|
||||
constructor(page: Page) {
|
||||
this.page = page;
|
||||
}
|
||||
|
||||
private async ensureCdp(): Promise<CDPSession> {
|
||||
if (!this.cdp) {
|
||||
this.cdp = await this.page.context().newCDPSession(this.page);
|
||||
}
|
||||
return this.cdp;
|
||||
}
|
||||
|
||||
private async createWorld(): Promise<number> {
|
||||
const cdp = await this.ensureCdp();
|
||||
const tree = await cdp.send('Page.getFrameTree');
|
||||
const frameId = tree.frameTree.frame.id;
|
||||
const result = await cdp.send('Page.createIsolatedWorld', {
|
||||
frameId,
|
||||
worldName: '',
|
||||
grantUniveralAccess: true,
|
||||
});
|
||||
const ctxId = result.executionContextId;
|
||||
this.contextId = ctxId;
|
||||
return ctxId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate a JS expression in the isolated world.
|
||||
* Auto-recreates the world if the context was invalidated (navigation).
|
||||
* Returns the result value, or undefined on failure.
|
||||
*/
|
||||
async evaluate(expression: string): Promise<any> {
|
||||
if (this.contextId === null) {
|
||||
try {
|
||||
await this.createWorld();
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
for (let attempt = 0; attempt < 2; attempt++) {
|
||||
try {
|
||||
const cdp = await this.ensureCdp();
|
||||
const result = await cdp.send('Runtime.evaluate', {
|
||||
expression,
|
||||
contextId: this.contextId!,
|
||||
returnByValue: true,
|
||||
});
|
||||
|
||||
if (result.exceptionDetails) {
|
||||
if (attempt === 0) {
|
||||
await this.createWorld();
|
||||
continue;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return result.result?.value;
|
||||
} catch {
|
||||
if (attempt === 0) {
|
||||
this.contextId = null;
|
||||
try {
|
||||
await this.createWorld();
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/** Mark context as stale — call after navigation. */
|
||||
invalidate(): void {
|
||||
this.contextId = null;
|
||||
}
|
||||
|
||||
/** Get the underlying CDP session (reused for Input.dispatchKeyEvent etc.). */
|
||||
async getCdpSession(): Promise<CDPSession> {
|
||||
return this.ensureCdp();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ============================================================================
|
||||
// Page / context / browser patching
|
||||
// ============================================================================
|
||||
|
||||
function patchPage(page: Page): void {
|
||||
if ((page as any).stealthEvaluate) return;
|
||||
const existing = (page as any)._stealthWorld;
|
||||
const stealth = existing instanceof StealthEval ? existing : new StealthEval(page);
|
||||
(page as any)._stealthWorld = stealth;
|
||||
(page as any).stealthEvaluate = stealth.evaluate.bind(stealth);
|
||||
}
|
||||
|
||||
export function patchContext(context: BrowserContext): void {
|
||||
if ((context as any)._stealthEvalPatched) return;
|
||||
(context as any)._stealthEvalPatched = true;
|
||||
for (const p of context.pages()) {
|
||||
patchPage(p);
|
||||
}
|
||||
|
||||
const origNewPage = context.newPage.bind(context);
|
||||
context.newPage = async (...args: Parameters<BrowserContext['newPage']>) => {
|
||||
const page = await origNewPage(...args);
|
||||
patchPage(page);
|
||||
return page;
|
||||
};
|
||||
|
||||
context.on('page', (page: Page) => patchPage(page));
|
||||
}
|
||||
|
||||
export function patchBrowser(browser: Browser): void {
|
||||
const origNewContext = browser.newContext.bind(browser);
|
||||
browser.newContext = async (...args: Parameters<Browser['newContext']>) => {
|
||||
const ctx = await origNewContext(...args);
|
||||
patchContext(ctx);
|
||||
return ctx;
|
||||
};
|
||||
|
||||
const origNewPage = browser.newPage.bind(browser);
|
||||
browser.newPage = async (...args: Parameters<Browser['newPage']>) => {
|
||||
const page = await origNewPage(...args);
|
||||
patchContext(page.context());
|
||||
patchPage(page);
|
||||
return page;
|
||||
};
|
||||
}
|
||||
@@ -2,6 +2,13 @@
|
||||
* Shared types for cloakbrowser launch wrappers.
|
||||
*/
|
||||
|
||||
declare module 'playwright-core' {
|
||||
interface Page {
|
||||
/** Evaluate JS in a CDP isolated world — clean stack traces, invisible to main-world monkey-patches. */
|
||||
stealthEvaluate(expression: string): Promise<any>;
|
||||
}
|
||||
}
|
||||
|
||||
export interface LaunchOptions {
|
||||
/** Run in headless mode (default: true). */
|
||||
headless?: boolean;
|
||||
|
||||
+89
-8
@@ -46,14 +46,17 @@ describe("launchContext (unit)", () => {
|
||||
let mockContext: any;
|
||||
let mockBrowser: any;
|
||||
let mockChromium: any;
|
||||
let origNewContext: any;
|
||||
const origEnv = process.env.CLOAKBROWSER_BINARY_PATH;
|
||||
|
||||
beforeEach(() => {
|
||||
process.env.CLOAKBROWSER_BINARY_PATH = "/fake/chrome";
|
||||
const origClose = vi.fn();
|
||||
mockContext = { close: origClose, _origClose: origClose };
|
||||
mockContext = { close: origClose, _origClose: origClose, newPage: vi.fn(), on: vi.fn(), pages: vi.fn().mockReturnValue([]) };
|
||||
origNewContext = vi.fn().mockResolvedValue(mockContext);
|
||||
mockBrowser = {
|
||||
newContext: vi.fn().mockResolvedValue(mockContext),
|
||||
newContext: origNewContext,
|
||||
newPage: vi.fn(),
|
||||
close: vi.fn(),
|
||||
};
|
||||
mockChromium = { launch: vi.fn().mockResolvedValue(mockBrowser) };
|
||||
@@ -75,7 +78,7 @@ describe("launchContext (unit)", () => {
|
||||
const { launchContext } = await import("../src/playwright.js");
|
||||
await launchContext();
|
||||
|
||||
const ctxArgs = mockBrowser.newContext.mock.calls[0][0];
|
||||
const ctxArgs = origNewContext.mock.calls[0][0];
|
||||
expect(ctxArgs.viewport).toEqual(DEFAULT_VIEWPORT);
|
||||
});
|
||||
|
||||
@@ -84,7 +87,7 @@ describe("launchContext (unit)", () => {
|
||||
const custom = { width: 1280, height: 720 };
|
||||
await launchContext({ viewport: custom });
|
||||
|
||||
const ctxArgs = mockBrowser.newContext.mock.calls[0][0];
|
||||
const ctxArgs = origNewContext.mock.calls[0][0];
|
||||
expect(ctxArgs.viewport).toEqual(custom);
|
||||
});
|
||||
|
||||
@@ -92,7 +95,7 @@ describe("launchContext (unit)", () => {
|
||||
const { launchContext } = await import("../src/playwright.js");
|
||||
await launchContext({ userAgent: "Custom/1.0" });
|
||||
|
||||
const ctxArgs = mockBrowser.newContext.mock.calls[0][0];
|
||||
const ctxArgs = origNewContext.mock.calls[0][0];
|
||||
expect(ctxArgs.userAgent).toBe("Custom/1.0");
|
||||
});
|
||||
|
||||
@@ -108,7 +111,7 @@ describe("launchContext (unit)", () => {
|
||||
expect(hasTimezoneFlag).toBe(true);
|
||||
|
||||
// NOT in newContext() — no CDP emulation
|
||||
const ctxArgs = mockBrowser.newContext.mock.calls[0][0];
|
||||
const ctxArgs = origNewContext.mock.calls[0][0];
|
||||
expect(ctxArgs.timezoneId).toBeUndefined();
|
||||
});
|
||||
|
||||
@@ -116,7 +119,7 @@ describe("launchContext (unit)", () => {
|
||||
const { launchContext } = await import("../src/playwright.js");
|
||||
await launchContext({ colorScheme: "dark" });
|
||||
|
||||
const ctxArgs = mockBrowser.newContext.mock.calls[0][0];
|
||||
const ctxArgs = origNewContext.mock.calls[0][0];
|
||||
expect(ctxArgs.colorScheme).toBe("dark");
|
||||
});
|
||||
|
||||
@@ -132,6 +135,84 @@ describe("launchContext (unit)", () => {
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// stealth_evaluate patching unit tests
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe("stealthEvaluate patching (unit)", () => {
|
||||
const origEnv = process.env.CLOAKBROWSER_BINARY_PATH;
|
||||
let mockPage: any;
|
||||
let mockContext: any;
|
||||
let mockBrowser: any;
|
||||
|
||||
beforeEach(() => {
|
||||
process.env.CLOAKBROWSER_BINARY_PATH = "/fake/chrome";
|
||||
|
||||
// Page mock with context() returning the implicit context
|
||||
mockPage = {
|
||||
context: vi.fn(),
|
||||
};
|
||||
// Implicit context created by browser.newPage()
|
||||
mockContext = {
|
||||
pages: vi.fn().mockReturnValue([]),
|
||||
newPage: vi.fn(),
|
||||
on: vi.fn(),
|
||||
};
|
||||
mockPage.context.mockReturnValue(mockContext);
|
||||
|
||||
mockBrowser = {
|
||||
newContext: vi.fn().mockResolvedValue(mockContext),
|
||||
newPage: vi.fn().mockResolvedValue(mockPage),
|
||||
close: vi.fn(),
|
||||
};
|
||||
const mockChromium = { launch: vi.fn().mockResolvedValue(mockBrowser) };
|
||||
vi.doMock("playwright-core", () => ({ chromium: mockChromium }));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
vi.resetModules();
|
||||
if (origEnv) {
|
||||
process.env.CLOAKBROWSER_BINARY_PATH = origEnv;
|
||||
} else {
|
||||
delete process.env.CLOAKBROWSER_BINARY_PATH;
|
||||
}
|
||||
});
|
||||
|
||||
it("page.stealthEvaluate exists after launch + browser.newPage", async () => {
|
||||
const { launch } = await import("../src/playwright.js");
|
||||
const browser = await launch({ headless: true });
|
||||
const page = await browser.newPage();
|
||||
|
||||
expect(typeof (page as any).stealthEvaluate).toBe("function");
|
||||
});
|
||||
|
||||
it("implicit context from browser.newPage is patched for future pages", async () => {
|
||||
const { launch } = await import("../src/playwright.js");
|
||||
const browser = await launch({ headless: true });
|
||||
await browser.newPage();
|
||||
|
||||
// The 'page' event listener should be registered on the implicit context
|
||||
expect(mockContext.on).toHaveBeenCalledWith("page", expect.any(Function));
|
||||
// The context should be marked as patched
|
||||
expect((mockContext as any)._stealthEvalPatched).toBe(true);
|
||||
});
|
||||
|
||||
it("context from browser.newContext patches pages with stealthEvaluate", async () => {
|
||||
const { launch } = await import("../src/playwright.js");
|
||||
const browser = await launch({ headless: true });
|
||||
|
||||
const mockPage2: any = { context: vi.fn().mockReturnValue(mockContext) };
|
||||
mockContext.newPage.mockResolvedValue(mockPage2);
|
||||
mockContext.pages.mockReturnValue([]);
|
||||
|
||||
const ctx = await browser.newContext();
|
||||
const page = await ctx.newPage();
|
||||
|
||||
expect(typeof (page as any).stealthEvaluate).toBe("function");
|
||||
});
|
||||
});
|
||||
|
||||
describe("launchPersistentContext (unit)", () => {
|
||||
let mockContext: any;
|
||||
let mockChromium: any;
|
||||
@@ -139,7 +220,7 @@ describe("launchPersistentContext (unit)", () => {
|
||||
|
||||
beforeEach(() => {
|
||||
process.env.CLOAKBROWSER_BINARY_PATH = "/fake/chrome";
|
||||
mockContext = { close: vi.fn(), pages: vi.fn().mockReturnValue([]) };
|
||||
mockContext = { close: vi.fn(), pages: vi.fn().mockReturnValue([]), newPage: vi.fn(), on: vi.fn() };
|
||||
mockChromium = {
|
||||
launchPersistentContext: vi.fn().mockResolvedValue(mockContext),
|
||||
};
|
||||
|
||||
+12
-12
@@ -470,7 +470,7 @@ describe("humanType mixed text with CDP", () => {
|
||||
// patchPage stealth wiring
|
||||
// =========================================================================
|
||||
describe("patchPage stealth infrastructure", () => {
|
||||
it("page._stealth is a StealthEval instance after patching", async () => {
|
||||
it("page._stealthWorld is a StealthEval instance after patching", async () => {
|
||||
const { patchPage } = await import("../src/human/index.js");
|
||||
|
||||
const page = buildMockPage();
|
||||
@@ -478,10 +478,10 @@ describe("patchPage stealth infrastructure", () => {
|
||||
const cursor = { x: 0, y: 0, initialized: false };
|
||||
patchPage(page as any, cfg, cursor as any);
|
||||
|
||||
expect((page as any)._stealth).toBeDefined();
|
||||
expect(typeof (page as any)._stealth.evaluate).toBe("function");
|
||||
expect(typeof (page as any)._stealth.invalidate).toBe("function");
|
||||
expect(typeof (page as any)._stealth.getCdpSession).toBe("function");
|
||||
expect((page as any)._stealthWorld).toBeDefined();
|
||||
expect(typeof (page as any)._stealthWorld.evaluate).toBe("function");
|
||||
expect(typeof (page as any)._stealthWorld.invalidate).toBe("function");
|
||||
expect(typeof (page as any)._stealthWorld.getCdpSession).toBe("function");
|
||||
});
|
||||
|
||||
it("page._original and page._humanCfg are set", async () => {
|
||||
@@ -504,7 +504,7 @@ describe("patchPage stealth infrastructure", () => {
|
||||
const cursor = { x: 0, y: 0, initialized: false };
|
||||
patchPage(page as any, cfg, cursor as any);
|
||||
|
||||
const stealth = (page as any)._stealth;
|
||||
const stealth = (page as any)._stealthWorld;
|
||||
const invalidateSpy = vi.spyOn(stealth, "invalidate");
|
||||
|
||||
await page.goto("https://example.com");
|
||||
@@ -540,7 +540,7 @@ describe("StealthEval lifecycle", () => {
|
||||
const cursor = { x: 0, y: 0, initialized: false };
|
||||
patchPage(page as any, cfg, cursor as any);
|
||||
|
||||
const stealth = (page as any)._stealth;
|
||||
const stealth = (page as any)._stealthWorld;
|
||||
expect(() => stealth.invalidate()).not.toThrow();
|
||||
});
|
||||
|
||||
@@ -552,7 +552,7 @@ describe("StealthEval lifecycle", () => {
|
||||
const cursor = { x: 0, y: 0, initialized: false };
|
||||
patchPage(page as any, cfg, cursor as any);
|
||||
|
||||
const stealth = (page as any)._stealth;
|
||||
const stealth = (page as any)._stealthWorld;
|
||||
const session = await stealth.getCdpSession();
|
||||
expect(session).toBeDefined();
|
||||
expect(typeof session.send).toBe("function");
|
||||
@@ -587,7 +587,7 @@ describe("StealthEval lifecycle", () => {
|
||||
const cursor = { x: 0, y: 0, initialized: false };
|
||||
patchPage(page as any, cfg, cursor as any);
|
||||
|
||||
const stealth = (page as any)._stealth;
|
||||
const stealth = (page as any)._stealthWorld;
|
||||
const result = await stealth.evaluate("1 + 1");
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
@@ -626,7 +626,7 @@ describe("StealthEval lifecycle", () => {
|
||||
const cursor = { x: 0, y: 0, initialized: false };
|
||||
patchPage(page as any, cfg, cursor as any);
|
||||
|
||||
const stealth = (page as any)._stealth;
|
||||
const stealth = (page as any)._stealthWorld;
|
||||
const result = await stealth.evaluate("test");
|
||||
expect(result).toBe("recovered");
|
||||
});
|
||||
@@ -660,7 +660,7 @@ describe("StealthEval lifecycle", () => {
|
||||
const cursor = { x: 0, y: 0, initialized: false };
|
||||
patchPage(page as any, cfg, cursor as any);
|
||||
|
||||
const stealth = (page as any)._stealth;
|
||||
const stealth = (page as any)._stealthWorld;
|
||||
const result = await stealth.evaluate("broken");
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
@@ -975,7 +975,7 @@ describeIfSlow("stealth browser: navigation invalidation", () => {
|
||||
const browser = await launch({ headless: true, args: ['--humanize'] });
|
||||
const page = await browser.newPage();
|
||||
|
||||
expect((page as any)._stealth).toBeDefined();
|
||||
expect((page as any)._stealthWorld).toBeDefined();
|
||||
|
||||
await page.goto('https://www.wikipedia.org', { waitUntil: 'domcontentloaded' });
|
||||
await sleep(1000);
|
||||
|
||||
Reference in New Issue
Block a user