diff --git a/src/debug/debugController.ts b/src/debug/debugController.ts index 1a8286a206ab6..156bd6bb922f3 100644 --- a/src/debug/debugController.ts +++ b/src/debug/debugController.ts @@ -16,10 +16,8 @@ import { BrowserContext } from '../server/browserContext'; import * as frames from '../server/frames'; -import * as js from '../server/javascript'; import { Page } from '../server/page'; import { InstrumentingAgent } from '../server/instrumentation'; -import type DebugScript from './injected/debugScript'; import { Progress } from '../server/progress'; import { isDebugMode } from '../utils/utils'; import * as debugScriptSource from '../generated/debugScriptSource'; @@ -33,12 +31,7 @@ export class DebugController implements InstrumentingAgent { if ((mainContext as any)[debugScriptSymbol]) return; (mainContext as any)[debugScriptSymbol] = true; - const objectId = await mainContext._delegate.rawEvaluate(`new (${debugScriptSource.source})()`); - const debugScript = new js.JSHandle(mainContext, 'object', objectId) as js.JSHandle; - const injectedScript = await mainContext.injectedScript(); - await debugScript.evaluate((debugScript, injectedScript) => { - debugScript.initialize(injectedScript, { console: true }); - }, injectedScript); + await mainContext.extendInjectedScript(debugScriptSource.source); } catch (e) { } } diff --git a/src/debug/injected/debugScript.ts b/src/debug/injected/debugScript.ts index d0aaf899b3efd..52aac81fa04c1 100644 --- a/src/debug/injected/debugScript.ts +++ b/src/debug/injected/debugScript.ts @@ -19,9 +19,7 @@ import type InjectedScript from '../../server/injected/injectedScript'; export default class DebugScript { consoleAPI: ConsoleAPI | undefined; - - initialize(injectedScript: InjectedScript, options: { console?: boolean }) { - if (options.console) - this.consoleAPI = new ConsoleAPI(injectedScript); + constructor(injectedScript: InjectedScript) { + this.consoleAPI = new ConsoleAPI(injectedScript); } } diff --git a/src/server/dom.ts b/src/server/dom.ts index 766ebdddd46b6..5f2b55c33a7e8 100644 --- a/src/server/dom.ts +++ b/src/server/dom.ts @@ -90,6 +90,13 @@ export class FrameExecutionContext extends js.ExecutionContext { return this._injectedScriptPromise; } + async extendInjectedScript(source: string, params?: any) { + const injectedScriptHandle = await this.injectedScript(); + return injectedScriptHandle.evaluate((injectedScript, {source, params}) => { + return injectedScript.extend(source, params); + }, { source, params }); + } + async doSlowMo() { return this.frame._page._doSlowMo(); } diff --git a/src/server/injected/injectedScript.ts b/src/server/injected/injectedScript.ts index e26d690b78c6d..df10789f6a168 100644 --- a/src/server/injected/injectedScript.ts +++ b/src/server/injected/injectedScript.ts @@ -19,7 +19,7 @@ import { createCSSEngine } from './cssSelectorEngine'; import { SelectorEngine, SelectorRoot } from './selectorEngine'; import { createTextSelector } from './textSelectorEngine'; import { XPathEngine } from './xpathSelectorEngine'; -import { ParsedSelector } from '../common/selectorParser'; +import { ParsedSelector, parseSelector } from '../common/selectorParser'; import { FatalDOMError } from '../common/domErrors'; type Predicate = (progress: InjectedScriptProgress, continuePolling: symbol) => T | symbol; @@ -63,6 +63,10 @@ export class InjectedScript { this.engines.set(name, engine); } + parseSelector(selector: string): ParsedSelector { + return parseSelector(selector); + } + querySelector(selector: ParsedSelector, root: Node): Element | undefined { if (!(root as any)['querySelector']) throw new Error('Node is not queryable.'); @@ -108,6 +112,11 @@ export class InjectedScript { return candidates.filter(e => !!this._querySelectorRecursively(e, partial, 0)); } + extend(source: string, params: any): any { + const constrFunction = global.eval(source); + return new constrFunction(this, params); + } + isVisible(element: Element): boolean { // Note: this logic should be similar to waitForDisplayedAtStablePosition() to avoid surprises. if (!element.ownerDocument || !element.ownerDocument.defaultView)