Skip to content

Commit

Permalink
chore: expose injectedScript.extend (#3784)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman authored Sep 7, 2020
1 parent d6cd022 commit fea3ceb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
9 changes: 1 addition & 8 deletions src/debug/debugController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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) {
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/debug/injected/debugScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
7 changes: 7 additions & 0 deletions src/server/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
11 changes: 10 additions & 1 deletion src/server/injected/injectedScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.');
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit fea3ceb

Please sign in to comment.