mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
chore: expose injectedScript.extend (#3784)
This commit is contained in:
parent
d6cd0224ab
commit
fea3ceb36c
@ -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<DebugScript>;
|
||||
const injectedScript = await mainContext.injectedScript();
|
||||
await debugScript.evaluate((debugScript, injectedScript) => {
|
||||
debugScript.initialize(injectedScript, { console: true });
|
||||
}, injectedScript);
|
||||
await mainContext.extendInjectedScript(debugScriptSource.source);
|
||||
} catch (e) {
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
constructor(injectedScript: InjectedScript) {
|
||||
this.consoleAPI = new ConsoleAPI(injectedScript);
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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<T> = (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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user