Skip to content

Commit

Permalink
feat(puppet): ability to disable storage
Browse files Browse the repository at this point in the history
  • Loading branch information
blakebyrnes committed Jan 25, 2022
1 parent 1154d4a commit 77c28fa
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 16 deletions.
8 changes: 5 additions & 3 deletions core/lib/InjectedScripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,14 @@ export default class InjectedScripts {
]);
}

public static installInteractionScript(puppetPage: IPuppetPage): Promise<void> {
public static installInteractionScript(puppetPage: IPuppetPage): Promise<{ identifier: string }> {
return puppetPage.addNewDocumentScript(showInteractionScript, true);
}

public static async installDomStorageRestore(puppetPage: IPuppetPage): Promise<void> {
await puppetPage.addNewDocumentScript(
public static async installDomStorageRestore(
puppetPage: IPuppetPage,
): Promise<{ identifier: string }> {
return await puppetPage.addNewDocumentScript(
`(function restoreDomStorage() {
const exports = {}; // workaround for ts adding an exports variable
${stringifiedTypeSerializerClass};
Expand Down
1 change: 1 addition & 0 deletions interfaces/IPuppetContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default interface IPuppetContext extends ITypedEventEmitter<IPuppetContex

export interface IPuppetPageOptions {
runPageScripts: boolean;
enableDomStorageTracker?: boolean;
triggerPopupOnPageId?: string;
}

Expand Down
6 changes: 5 additions & 1 deletion interfaces/IPuppetPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ export interface IPuppetPage extends ITypedEventEmitter<IPuppetPageEvents> {
setJavaScriptEnabled(enabled: boolean): Promise<void>;

evaluate<T>(expression: string): Promise<T>;
addNewDocumentScript(script: string, isolateFromWebPageEnvironment: boolean): Promise<void>;
addNewDocumentScript(
script: string,
isolateFromWebPageEnvironment: boolean,
): Promise<{ identifier: string }>;
removeDocumentScript(identifier: string): Promise<void>;
addPageCallback(
name: string,
onCallback?: (payload: any, frameId: string) => any,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@commitlint/config-conventional": "^12.0.1",
"@types/jest": "^26.0.20",
"@types/node": "^13.13.52",
"@typescript-eslint/eslint-plugin": "^5.1.0",
"@typescript-eslint/eslint-plugin": "^5.10.1",
"@typescript-eslint/parser": "^5.1.0",
"awaited-dom": "^1.3.1",
"copyfiles": "^2.4.1",
Expand Down
25 changes: 21 additions & 4 deletions puppet-chrome/lib/BrowserContext.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { assert } from '@ulixee/commons/lib/utils';
import IPuppetContext, { IPuppetContextEvents, IPuppetPageOptions } from '@ulixee/hero-interfaces/IPuppetContext';
import IPuppetContext, {
IPuppetContextEvents,
IPuppetPageOptions,
} from '@ulixee/hero-interfaces/IPuppetContext';
import { ICookie } from '@ulixee/hero-interfaces/ICookie';
import { URL } from 'url';
import Protocol from 'devtools-protocol';
import { addTypedEventListener, removeEventListeners, TypedEventEmitter } from '@ulixee/commons/lib/eventUtils';
import {
addTypedEventListener,
removeEventListeners,
TypedEventEmitter,
} from '@ulixee/commons/lib/eventUtils';
import { IBoundLog } from '@ulixee/commons/interfaces/ILog';
import IRegisteredEventListener from '@ulixee/commons/interfaces/IRegisteredEventListener';
import { CanceledPromiseError } from '@ulixee/commons/interfaces/IPendingWaitEvent';
Expand All @@ -13,7 +20,10 @@ import ICorePlugins from '@ulixee/hero-interfaces/ICorePlugins';
import { IPuppetPage } from '@ulixee/hero-interfaces/IPuppetPage';
import IProxyConnectionOptions from '@ulixee/hero-interfaces/IProxyConnectionOptions';
import Resolvable from '@ulixee/commons/lib/Resolvable';
import { IDevtoolsEventMessage, IDevtoolsResponseMessage } from '@ulixee/hero-interfaces/IDevtoolsSession';
import {
IDevtoolsEventMessage,
IDevtoolsResponseMessage,
} from '@ulixee/hero-interfaces/IDevtoolsSession';
import { Page } from './Page';
import { Browser } from './Browser';
import { DevtoolsSession } from './DevtoolsSession';
Expand Down Expand Up @@ -143,7 +153,14 @@ export class BrowserContext
opener = this.pagesById.values().next().value;
}

const page = new Page(devtoolsSession, targetInfo.targetId, this, this.logger, opener);
const page = new Page(
devtoolsSession,
targetInfo.targetId,
this,
this.logger,
opener,
pageOptions,
);
this.pagesById.set(page.targetId, page);
this.waitForPageAttachedById.get(page.targetId)?.resolve(page);
await page.isReady;
Expand Down
4 changes: 4 additions & 0 deletions puppet-chrome/lib/DomStorageTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,17 @@ export class DomStorageTracker
private readonly indexedDBContentUpdatingOrigins = new Set<string>();

private trackedOrigins = new Set<string>();
private isEnabled = false;

constructor(
page: Page,
storageByOrigin: IDomStorage,
networkManager: NetworkManager,
logger: IBoundLog,
isEnabled: boolean,
) {
super();
this.isEnabled = isEnabled;
this.page = page;
this.devtoolsSession = page.devtoolsSession;
this.networkManager = networkManager;
Expand Down Expand Up @@ -79,6 +82,7 @@ export class DomStorageTracker
}

public initialize(): Promise<void> {
if (!this.isEnabled) return Promise.resolve();
return Promise.all([
this.devtoolsSession.send('DOMStorage.enable'),
this.devtoolsSession.send('IndexedDB.enable'),
Expand Down
17 changes: 12 additions & 5 deletions puppet-chrome/lib/FramesManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,24 @@ export default class FramesManager extends TypedEventEmitter<IPuppetFrameManager
);
}

public async addNewDocumentScript(script: string, installInIsolatedScope = true): Promise<void> {
await this.devtoolsSession.send('Page.addScriptToEvaluateOnNewDocument', {
source: script,
worldName: installInIsolatedScope ? ISOLATED_WORLD : undefined,
});
public async addNewDocumentScript(
script: string,
installInIsolatedScope = true,
): Promise<{ identifier: string }> {
const installedScript = await this.devtoolsSession.send(
'Page.addScriptToEvaluateOnNewDocument',
{
source: script,
worldName: installInIsolatedScope ? ISOLATED_WORLD : undefined,
},
);

// sometimes we get a new anchor link that already has an initiated frame. If that's the case, newDocumentScripts won't trigger.
// NOTE: we DON'T want this to trigger for internal pages (':', 'about:blank')
if (this.main.url?.startsWith('http')) {
await this.main.evaluate(script, installInIsolatedScope, { retriesWaitingForLoad: 1 });
}
return installedScript;
}

/////// EXECUTION CONTEXT ////////////////////////////////////////////////////
Expand Down
12 changes: 11 additions & 1 deletion puppet-chrome/lib/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import Size = Protocol.SystemInfo.Size;
import Rect = Protocol.DOM.Rect;
import SetDeviceMetricsOverrideRequest = Protocol.Emulation.SetDeviceMetricsOverrideRequest;
import Viewport = Protocol.Page.Viewport;
import { IPuppetPageOptions } from '@ulixee/hero-interfaces/IPuppetContext';

export class Page extends TypedEventEmitter<IPuppetPageEvents> implements IPuppetPage {
public keyboard: Keyboard;
Expand Down Expand Up @@ -94,6 +95,7 @@ export class Page extends TypedEventEmitter<IPuppetPageEvents> implements IPuppe
browserContext: BrowserContext,
logger: IBoundLog,
opener: Page | null,
pageOptions?: IPuppetPageOptions,
) {
super();

Expand All @@ -117,6 +119,7 @@ export class Page extends TypedEventEmitter<IPuppetPageEvents> implements IPuppe
browserContext.domStorage,
this.networkManager,
this.logger,
pageOptions?.enableDomStorageTracker ?? true,
);
this.framesManager = new FramesManager(
devtoolsSession,
Expand Down Expand Up @@ -175,10 +178,17 @@ export class Page extends TypedEventEmitter<IPuppetPageEvents> implements IPuppe
return await this.networkManager.setNetworkInterceptor(networkRequestsFn, true);
}

addNewDocumentScript(script: string, isolatedEnvironment: boolean): Promise<void> {
addNewDocumentScript(
script: string,
isolatedEnvironment: boolean,
): Promise<{ identifier: string }> {
return this.framesManager.addNewDocumentScript(script, isolatedEnvironment);
}

removeDocumentScript(identifier: string): Promise<void> {
return this.devtoolsSession.send('Page.removeScriptToEvaluateOnNewDocument', { identifier });
}

addPageCallback(
name: string,
onCallback?: (payload: any, frameId: string) => any,
Expand Down
2 changes: 1 addition & 1 deletion timetravel/lib/MirrorPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default class MirrorPage extends TypedEventEmitter<{
const ready = new Resolvable<void>();
this.isReady = ready.promise;
try {
this.page = await context.newPage({ runPageScripts: false });
this.page = await context.newPage({ runPageScripts: false, enableDomStorageTracker: false });
this.page.once('close', this.close.bind(this));
if (this.debugLogging) {
this.page.on('console', msg => {
Expand Down

0 comments on commit 77c28fa

Please sign in to comment.