Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/client/lib/CoreFrameEnvironment.ts
1028 views
1
import { IInteractionGroups } from '@secret-agent/interfaces/IInteractions';
2
import ISessionMeta from '@secret-agent/interfaces/ISessionMeta';
3
import { ILocationStatus, ILocationTrigger } from '@secret-agent/interfaces/Location';
4
import { IJsPath } from 'awaited-dom/base/AwaitedPath';
5
import { ICookie } from '@secret-agent/interfaces/ICookie';
6
import IWaitForElementOptions from '@secret-agent/interfaces/IWaitForElementOptions';
7
import IExecJsPathResult from '@secret-agent/interfaces/IExecJsPathResult';
8
import { IRequestInit } from 'awaited-dom/base/interfaces/official';
9
import INodePointer from 'awaited-dom/base/INodePointer';
10
import ISetCookieOptions from '@secret-agent/interfaces/ISetCookieOptions';
11
import IWaitForOptions from '@secret-agent/interfaces/IWaitForOptions';
12
import IFrameMeta from '@secret-agent/interfaces/IFrameMeta';
13
import IResourceMeta from '@secret-agent/interfaces/IResourceMeta';
14
import CoreCommandQueue from './CoreCommandQueue';
15
16
export default class CoreFrameEnvironment {
17
public tabId: number;
18
public frameId: number;
19
public sessionId: string;
20
public commandQueue: CoreCommandQueue;
21
public parentFrameId: number;
22
23
constructor(
24
meta: ISessionMeta & { sessionName: string },
25
parentFrameId: number,
26
commandQueue: CoreCommandQueue,
27
) {
28
const { tabId, sessionId, frameId, sessionName } = meta;
29
this.tabId = tabId;
30
this.sessionId = sessionId;
31
this.frameId = frameId;
32
this.parentFrameId = parentFrameId;
33
const queueMeta = {
34
sessionId,
35
tabId,
36
sessionName,
37
frameId,
38
};
39
this.commandQueue = commandQueue.createSharedQueue(queueMeta);
40
}
41
42
public async getFrameMeta(): Promise<IFrameMeta> {
43
return await this.commandQueue.run('FrameEnvironment.meta');
44
}
45
46
public async getChildFrameEnvironment(jsPath: IJsPath): Promise<IFrameMeta> {
47
return await this.commandQueue.run('FrameEnvironment.getChildFrameEnvironment', jsPath);
48
}
49
50
public async execJsPath<T = any>(jsPath: IJsPath): Promise<IExecJsPathResult<T>> {
51
return await this.commandQueue.run('FrameEnvironment.execJsPath', jsPath);
52
}
53
54
public recordDetachedJsPath(index: number, startDate: Date, endDate: Date): void {
55
this.commandQueue.record({
56
commandId: this.commandQueue.nextCommandId,
57
command: 'FrameEnvironment.recordDetachedJsPath',
58
args: [index, startDate.getTime(), endDate.getTime()],
59
});
60
}
61
62
public async getJsValue<T>(expression: string): Promise<T> {
63
return await this.commandQueue.run('FrameEnvironment.getJsValue', expression);
64
}
65
66
public async fetch(request: string | number, init?: IRequestInit): Promise<INodePointer> {
67
return await this.commandQueue.run('FrameEnvironment.fetch', request, init);
68
}
69
70
public async createRequest(input: string | number, init?: IRequestInit): Promise<INodePointer> {
71
return await this.commandQueue.run('FrameEnvironment.createRequest', input, init);
72
}
73
74
public async getUrl(): Promise<string> {
75
return await this.commandQueue.run('FrameEnvironment.getLocationHref');
76
}
77
78
public async interact(interactionGroups: IInteractionGroups): Promise<void> {
79
await this.commandQueue.run('FrameEnvironment.interact', ...interactionGroups);
80
}
81
82
public async getCookies(): Promise<ICookie[]> {
83
return await this.commandQueue.run('FrameEnvironment.getCookies');
84
}
85
86
public async setCookie(
87
name: string,
88
value: string,
89
options?: ISetCookieOptions,
90
): Promise<boolean> {
91
return await this.commandQueue.run('FrameEnvironment.setCookie', name, value, options);
92
}
93
94
public async removeCookie(name: string): Promise<boolean> {
95
return await this.commandQueue.run('FrameEnvironment.removeCookie', name);
96
}
97
98
public async setFileInputFiles(
99
jsPath: IJsPath,
100
files: { name: string; data: Buffer }[],
101
): Promise<void> {
102
return await this.commandQueue.run('FrameEnvironment.setFileInputFiles', jsPath, files);
103
}
104
105
public async waitForElement(jsPath: IJsPath, opts: IWaitForElementOptions): Promise<void> {
106
await this.commandQueue.run('FrameEnvironment.waitForElement', jsPath, opts);
107
}
108
109
public async waitForLoad(status: ILocationStatus, opts: IWaitForOptions): Promise<void> {
110
await this.commandQueue.run('FrameEnvironment.waitForLoad', status, opts);
111
}
112
113
public async waitForLocation(
114
trigger: ILocationTrigger,
115
opts: IWaitForOptions,
116
): Promise<IResourceMeta> {
117
return await this.commandQueue.run('FrameEnvironment.waitForLocation', trigger, opts);
118
}
119
}
120
121