browserscale-ts 1.5.0 → 1.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +33 -0
- package/dist/browser.d.ts +4 -1
- package/dist/browser.js +5 -0
- package/dist/browserscale.browser.js +1583 -46
- package/dist/client.d.ts +318 -7
- package/dist/client.js +590 -7
- package/dist/dom-mirror.d.ts +271 -0
- package/dist/dom-mirror.js +613 -0
- package/dist/gen/wrc_pb.d.ts +1251 -87
- package/dist/gen/wrc_pb.js +214 -44
- package/dist/index.d.ts +31 -1
- package/dist/index.js +61 -0
- package/dist/internal/convert.d.ts +8 -2
- package/dist/internal/convert.js +39 -0
- package/dist/network-capture.d.ts +84 -0
- package/dist/network-capture.js +107 -0
- package/dist/scripts.d.ts +205 -0
- package/dist/scripts.js +234 -0
- package/dist/types.d.ts +157 -0
- package/dist/ws-transport.d.ts +15 -1
- package/dist/ws-transport.js +155 -10
- package/package.json +1 -1
package/dist/client.js
CHANGED
|
@@ -2,9 +2,12 @@ import { createClient } from "@connectrpc/connect";
|
|
|
2
2
|
import { create } from "@bufbuild/protobuf";
|
|
3
3
|
import { Browser,
|
|
4
4
|
// request / response schemas
|
|
5
|
-
SetProxyRequestSchema, GetPagesRequestSchema, NavigateRequestSchema, LoadHTMLRequestSchema, EvaluateRequestSchema, WaitForAnyParamsSchema, WaitConditionSchema, ClickRequestSchema, FillRequestSchema, MoveToRequestSchema, ScrollToRequestSchema, DragRequestSchema, SelectOptionRequestSchema, GetDOMRequestSchema, GetDOMHashRequestSchema, GetObservationRequestSchema, ScreenshotRequestSchema, ReadCanvasRequestSchema, SetBlockListRequestSchema, SetStaticPathsRequestSchema, WaitForAnyRequestRequestSchema, WaitForAnyResponseRequestSchema, ModifyRequestRequestSchema, GetCookiesRequestSchema, SetCookiesRequestSchema, ClearCookiesRequestSchema, GetStorageRequestSchema, SetStorageRequestSchema, ClearStorageRequestSchema, GetAuthSessionRequestSchema, SetAuthSessionRequestSchema, InspectAtPositionRequestSchema, HighlightNodeRequestSchema, InsertTextRequestSchema, TypeRequestSchema, PressKeyRequestSchema, ReleaseKeyRequestSchema, GetSelectionRequestSchema, SolveCaptchaRequestSchema, GetStreamConfigRequestSchema, StartStreamRequestSchema, StopStreamRequestSchema, AddReactionRequestSchema, RemoveReactionRequestSchema, ListReactionsRequestSchema, } from "./gen/wrc_pb.js";
|
|
5
|
+
SetProxyRequestSchema, GetPagesRequestSchema, NavigateRequestSchema, LoadHTMLRequestSchema, EvaluateRequestSchema, WaitForAnyParamsSchema, WaitConditionSchema, ClickRequestSchema, FillRequestSchema, MoveToRequestSchema, ScrollToRequestSchema, DragRequestSchema, SelectOptionRequestSchema, GetDOMRequestSchema, GetDOMHashRequestSchema, GetObservationRequestSchema, ScreenshotRequestSchema, ReadCanvasRequestSchema, SetBlockListRequestSchema, SetStaticPathsRequestSchema, WaitForAnyRequestRequestSchema, WaitForAnyResponseRequestSchema, ModifyRequestRequestSchema, GetCookiesRequestSchema, SetCookiesRequestSchema, ClearCookiesRequestSchema, GetStorageRequestSchema, SetStorageRequestSchema, ClearStorageRequestSchema, GetAuthSessionRequestSchema, SetAuthSessionRequestSchema, InspectAtPositionRequestSchema, HighlightNodeRequestSchema, InsertTextRequestSchema, TypeRequestSchema, PressKeyRequestSchema, ReleaseKeyRequestSchema, GetSelectionRequestSchema, SolveCaptchaRequestSchema, GetStreamConfigRequestSchema, StartStreamRequestSchema, StopStreamRequestSchema, AddReactionRequestSchema, RemoveReactionRequestSchema, ListReactionsRequestSchema, StartNetworkCaptureRequestSchema, StopNetworkCaptureRequestSchema, StartDomMirrorRequestSchema, StopDomMirrorRequestSchema, GetDomChildrenRequestSchema, ReleaseDomSubtreeRequestSchema, RevealDomNodeRequestSchema, GetDomRevisionRequestSchema, StreamDomEventsRequestSchema, StreamNetworkExchangesRequestSchema, RunScriptRequestSchema, StartScriptRequestSchema, StopScriptsRequestSchema, ListScriptRunsRequestSchema, StreamScriptEventsRequestSchema, } from "./gen/wrc_pb.js";
|
|
6
6
|
import { DefaultWaitTimeoutMs } from "./defaults.js";
|
|
7
7
|
import { BrowserScaleError } from "./errors.js";
|
|
8
|
+
import { NetworkCapture } from "./network-capture.js";
|
|
9
|
+
import { ScriptFollow, ScriptRun, scriptLogEntryFromProto, } from "./scripts.js";
|
|
10
|
+
import { DomMirror, } from "./dom-mirror.js";
|
|
8
11
|
import { authSessionFromProto, authSessionToProto, cookieParamFromProto, cookieParamsToProto, elementFields, headerModsToProto, headersToProto, interceptedRequestFromProto, interceptedResponseFromProto, pageInfoFromProto, rectFromProto, splitRequestPatterns, storageEntriesToProto, storageEntryFromProto, unwrapClick, unwrapDrag, unwrapFill, unwrapMove, unwrapScroll, unwrapSelect, unwrapWait, } from "./internal/convert.js";
|
|
9
12
|
/**
|
|
10
13
|
* CloudBrowser is the SDK-side handle for an active browserscale browser session.
|
|
@@ -1047,6 +1050,334 @@ export class CloudBrowser {
|
|
|
1047
1050
|
return interceptedRequestFromProto(resp.request);
|
|
1048
1051
|
}
|
|
1049
1052
|
// ──────────────────────────────────────────────────────────────────
|
|
1053
|
+
// Network capture
|
|
1054
|
+
// ──────────────────────────────────────────────────────────────────
|
|
1055
|
+
/**
|
|
1056
|
+
* Starts capturing the session's network traffic and returns a live view of
|
|
1057
|
+
* it.
|
|
1058
|
+
*
|
|
1059
|
+
* Every request matching `opts.patterns` is reported once it completes, and
|
|
1060
|
+
* "every request" is literal: capture sits in the browser process rather than
|
|
1061
|
+
* in a page, so cross-process iframes, workers and service workers are
|
|
1062
|
+
* included, the headers are the ones actually put on the wire (Cookie and
|
|
1063
|
+
* Sec-* included), and each hop of a redirect chain arrives as its own
|
|
1064
|
+
* exchange. Requests are never paused, so the page loads at full speed.
|
|
1065
|
+
*
|
|
1066
|
+
* This resolves as soon as the capture is running; onExchange then fires in
|
|
1067
|
+
* the background while you drive the browser. The capture is armed only after
|
|
1068
|
+
* the subscription exists, so nothing that happens after this resolves is
|
|
1069
|
+
* missed. Call {@link NetworkCapture.stop} when done — it disarms the capture
|
|
1070
|
+
* server-side, which an aborted transport alone does not.
|
|
1071
|
+
*
|
|
1072
|
+
* @param opts - which requests to capture and whether to keep bodies
|
|
1073
|
+
* @param onExchange - called per exchange; see {@link NetworkExchangeHandler}
|
|
1074
|
+
* for the ordering and blocking rules
|
|
1075
|
+
*
|
|
1076
|
+
* @returns NetworkCapture handle for stopping the capture and inspecting how
|
|
1077
|
+
* it ended
|
|
1078
|
+
*
|
|
1079
|
+
* @throws UNKNOWN_ERROR - the capture could not be started
|
|
1080
|
+
*
|
|
1081
|
+
* @example
|
|
1082
|
+
* const capture = await browser.captureNetwork(
|
|
1083
|
+
* { patterns: ["*\/api/*"], bodies: "text" },
|
|
1084
|
+
* (ex) => console.log(ex.statusCode, ex.method, ex.url),
|
|
1085
|
+
* );
|
|
1086
|
+
* try {
|
|
1087
|
+
* await browser.navigate("https://example.com");
|
|
1088
|
+
* } finally {
|
|
1089
|
+
* await capture.stop();
|
|
1090
|
+
* }
|
|
1091
|
+
*/
|
|
1092
|
+
async captureNetwork(opts, onExchange) {
|
|
1093
|
+
const capture = await this.subscribeNetworkExchanges(onExchange);
|
|
1094
|
+
try {
|
|
1095
|
+
await this.startNetworkCapture(opts);
|
|
1096
|
+
}
|
|
1097
|
+
catch (err) {
|
|
1098
|
+
// Not armed yet, so this only tears down the local subscription.
|
|
1099
|
+
await capture.stop();
|
|
1100
|
+
throw err;
|
|
1101
|
+
}
|
|
1102
|
+
capture.arm();
|
|
1103
|
+
return capture;
|
|
1104
|
+
}
|
|
1105
|
+
/**
|
|
1106
|
+
* Arms a capture without subscribing to it.
|
|
1107
|
+
*
|
|
1108
|
+
* Use it when the reader lives somewhere else — another tab, or a later
|
|
1109
|
+
* {@link createWebSocketBrowser} against the same session. Most callers want
|
|
1110
|
+
* {@link CloudBrowser.captureNetwork} instead, which arms and subscribes
|
|
1111
|
+
* together. Calling this again replaces the running capture.
|
|
1112
|
+
*
|
|
1113
|
+
* @param opts - which requests to capture and whether to keep bodies
|
|
1114
|
+
*
|
|
1115
|
+
* @throws UNKNOWN_ERROR - the capture could not be started
|
|
1116
|
+
*/
|
|
1117
|
+
async startNetworkCapture(opts) {
|
|
1118
|
+
await this.client.startNetworkCapture(create(StartNetworkCaptureRequestSchema, {
|
|
1119
|
+
sessionId: this.sessionId,
|
|
1120
|
+
apiKey: this.apiKey,
|
|
1121
|
+
patterns: opts.patterns ?? [],
|
|
1122
|
+
bodies: opts.bodies ?? "none",
|
|
1123
|
+
bodyPatterns: opts.bodyPatterns ?? [],
|
|
1124
|
+
}));
|
|
1125
|
+
}
|
|
1126
|
+
/**
|
|
1127
|
+
* Disarms the session's capture.
|
|
1128
|
+
*
|
|
1129
|
+
* @returns whether a capture was running
|
|
1130
|
+
*
|
|
1131
|
+
* @throws UNKNOWN_ERROR - the capture could not be stopped
|
|
1132
|
+
*/
|
|
1133
|
+
async stopNetworkCapture() {
|
|
1134
|
+
const resp = await this.client.stopNetworkCapture(create(StopNetworkCaptureRequestSchema, {
|
|
1135
|
+
sessionId: this.sessionId,
|
|
1136
|
+
apiKey: this.apiKey,
|
|
1137
|
+
}));
|
|
1138
|
+
return resp.stopped;
|
|
1139
|
+
}
|
|
1140
|
+
/**
|
|
1141
|
+
* Subscribes to the session's capture without arming one, for reading a
|
|
1142
|
+
* capture that {@link CloudBrowser.startNetworkCapture} armed elsewhere.
|
|
1143
|
+
* Several readers can watch the same capture, each with its own buffer.
|
|
1144
|
+
*
|
|
1145
|
+
* Stopping the returned view detaches this reader and leaves the capture
|
|
1146
|
+
* running, since other readers may still be attached.
|
|
1147
|
+
*
|
|
1148
|
+
* @param onExchange - called per exchange; see {@link NetworkExchangeHandler}
|
|
1149
|
+
* for the ordering and blocking rules
|
|
1150
|
+
*
|
|
1151
|
+
* @returns NetworkCapture attached to whatever capture is running; onExchange
|
|
1152
|
+
* simply never fires when none is
|
|
1153
|
+
*
|
|
1154
|
+
* @throws UNKNOWN_ERROR - the subscription could not be opened
|
|
1155
|
+
*/
|
|
1156
|
+
async streamNetworkExchanges(onExchange) {
|
|
1157
|
+
return this.subscribeNetworkExchanges(onExchange);
|
|
1158
|
+
}
|
|
1159
|
+
/**
|
|
1160
|
+
* Opens the stream and waits for the server to acknowledge the subscription
|
|
1161
|
+
* before resolving.
|
|
1162
|
+
*
|
|
1163
|
+
* Merely calling the streaming method does not wait for the server to start
|
|
1164
|
+
* handling it, so arming a capture straight after could outrun the
|
|
1165
|
+
* subscription and lose the first exchanges. The response headers arrive once
|
|
1166
|
+
* the handler is subscribed, and onHeader reports exactly that — over native
|
|
1167
|
+
* gRPC as well as over the WebSocket transport, which forwards the event as
|
|
1168
|
+
* its own frame.
|
|
1169
|
+
*/
|
|
1170
|
+
async subscribeNetworkExchanges(onExchange) {
|
|
1171
|
+
const abort = new AbortController();
|
|
1172
|
+
let subscribed;
|
|
1173
|
+
const ready = new Promise((resolve) => {
|
|
1174
|
+
subscribed = resolve;
|
|
1175
|
+
});
|
|
1176
|
+
const stream = this.client.streamNetworkExchanges(create(StreamNetworkExchangesRequestSchema, {
|
|
1177
|
+
sessionId: this.sessionId,
|
|
1178
|
+
apiKey: this.apiKey,
|
|
1179
|
+
}), { signal: abort.signal, onHeader: () => subscribed() });
|
|
1180
|
+
const capture = new NetworkCapture({
|
|
1181
|
+
stream,
|
|
1182
|
+
onExchange,
|
|
1183
|
+
abort,
|
|
1184
|
+
disarm: () => this.stopNetworkCapture(),
|
|
1185
|
+
});
|
|
1186
|
+
// A stream that dies before it is acknowledged would leave the wait above
|
|
1187
|
+
// hanging, so race the two outcomes.
|
|
1188
|
+
const ended = capture.wait().then(() => {
|
|
1189
|
+
throw new BrowserScaleError("browserscale.captureNetwork: stream closed before the subscription was established");
|
|
1190
|
+
});
|
|
1191
|
+
ended.catch(() => { }); // the loser of the race must not look unhandled
|
|
1192
|
+
await Promise.race([ready, ended]);
|
|
1193
|
+
return capture;
|
|
1194
|
+
}
|
|
1195
|
+
// ──────────────────────────────────────────────────────────────────
|
|
1196
|
+
// DOM mirror
|
|
1197
|
+
// ──────────────────────────────────────────────────────────────────
|
|
1198
|
+
/**
|
|
1199
|
+
* Starts a live copy of a frame's DOM and keeps it up to date.
|
|
1200
|
+
*
|
|
1201
|
+
* The browser sends the top of the tree once, then reports only what changed
|
|
1202
|
+
* in the part you expanded. Everything else costs a child count per batch, no
|
|
1203
|
+
* matter how much churns inside it — which is what makes this usable on a
|
|
1204
|
+
* page that rewrites a list sixty times a second, where re-fetching the
|
|
1205
|
+
* document on a timer is not.
|
|
1206
|
+
*
|
|
1207
|
+
* Expand and collapse as the user opens and closes nodes; that is what moves
|
|
1208
|
+
* the boundary of what gets reported. The returned {@link DomMirror} holds
|
|
1209
|
+
* the tree and exposes `expand`, `collapse` and `reveal`.
|
|
1210
|
+
*
|
|
1211
|
+
* The handler runs after each applied batch. `mirror.root` is a new object
|
|
1212
|
+
* whenever anything under it changed and the untouched parts keep their
|
|
1213
|
+
* identity, so rendering straight from it with memoized components is cheap.
|
|
1214
|
+
*
|
|
1215
|
+
* One mirror covers the whole page as ONE tree. An `<iframe>` is an
|
|
1216
|
+
* ordinary element whose single child is the document it hosts; expanding it
|
|
1217
|
+
* fetches that document and starts mirroring the frame, however deeply
|
|
1218
|
+
* nested and whether or not it is cross-origin. Unlike the inlining
|
|
1219
|
+
* {@link CloudBrowser.getDOM} does, these regions stay live — and frames
|
|
1220
|
+
* nobody opened cost nothing.
|
|
1221
|
+
*
|
|
1222
|
+
* ```ts
|
|
1223
|
+
* const mirror = await browser.mirrorDom({ pierce: true }, () => {
|
|
1224
|
+
* render(mirror.root);
|
|
1225
|
+
* });
|
|
1226
|
+
* await mirror.expand(bodyNode);
|
|
1227
|
+
* // ...
|
|
1228
|
+
* await mirror.stop();
|
|
1229
|
+
* ```
|
|
1230
|
+
*
|
|
1231
|
+
* @param opts initial depth and whether to pierce shadow roots
|
|
1232
|
+
* @param onChange called after every change, including the first snapshot
|
|
1233
|
+
* @param onResync called when the copy had to be rebuilt, after the new tree
|
|
1234
|
+
* is in place. Rebuilding is automatic; this is for telling the user why
|
|
1235
|
+
* their expanded nodes collapsed.
|
|
1236
|
+
* @throws UNKNOWN_ERROR - the mirror could not be started
|
|
1237
|
+
*/
|
|
1238
|
+
async mirrorDom(opts, onChange, onResync) {
|
|
1239
|
+
const abort = new AbortController();
|
|
1240
|
+
let subscribed;
|
|
1241
|
+
const ready = new Promise((resolve) => {
|
|
1242
|
+
subscribed = resolve;
|
|
1243
|
+
});
|
|
1244
|
+
const stream = this.client.streamDomEvents(create(StreamDomEventsRequestSchema, {
|
|
1245
|
+
sessionId: this.sessionId,
|
|
1246
|
+
apiKey: this.apiKey,
|
|
1247
|
+
}), { signal: abort.signal, onHeader: () => subscribed() });
|
|
1248
|
+
const mirror = new DomMirror({
|
|
1249
|
+
stream,
|
|
1250
|
+
transport: {
|
|
1251
|
+
start: (o) => this.startDomMirror(o),
|
|
1252
|
+
stop: () => this.stopDomMirror(),
|
|
1253
|
+
children: (id, frameId, depth) => this.getDomChildren(id, frameId, depth),
|
|
1254
|
+
release: (id, frameId) => this.releaseDomSubtree(id, frameId),
|
|
1255
|
+
reveal: (id, frameId) => this.revealDomNode(id, frameId),
|
|
1256
|
+
},
|
|
1257
|
+
options: opts,
|
|
1258
|
+
onChange,
|
|
1259
|
+
onResync,
|
|
1260
|
+
abort,
|
|
1261
|
+
});
|
|
1262
|
+
// A stream that dies before it is acknowledged would leave the wait below
|
|
1263
|
+
// hanging, so race the two outcomes.
|
|
1264
|
+
const ended = mirror.wait().then(() => {
|
|
1265
|
+
throw new BrowserScaleError("browserscale.mirrorDom: stream closed before the subscription was established");
|
|
1266
|
+
});
|
|
1267
|
+
ended.catch(() => { });
|
|
1268
|
+
await Promise.race([ready, ended]);
|
|
1269
|
+
// Snapshot only now: taken before the subscription exists, changes between
|
|
1270
|
+
// the two would be lost with nothing to indicate it.
|
|
1271
|
+
try {
|
|
1272
|
+
mirror.install(await this.startDomMirror(opts));
|
|
1273
|
+
}
|
|
1274
|
+
catch (err) {
|
|
1275
|
+
abort.abort();
|
|
1276
|
+
throw err;
|
|
1277
|
+
}
|
|
1278
|
+
onChange(mirror);
|
|
1279
|
+
return mirror;
|
|
1280
|
+
}
|
|
1281
|
+
/**
|
|
1282
|
+
* Starts (or restarts) the page's mirror and returns the main document,
|
|
1283
|
+
* without subscribing to changes. {@link CloudBrowser.mirrorDom} is what you
|
|
1284
|
+
* normally want; this is the raw command.
|
|
1285
|
+
*/
|
|
1286
|
+
async startDomMirror(opts = {}) {
|
|
1287
|
+
const req = create(StartDomMirrorRequestSchema, {
|
|
1288
|
+
sessionId: this.sessionId,
|
|
1289
|
+
apiKey: this.apiKey,
|
|
1290
|
+
});
|
|
1291
|
+
if (opts.depth !== undefined)
|
|
1292
|
+
req.depth = opts.depth;
|
|
1293
|
+
if (opts.pierce !== undefined)
|
|
1294
|
+
req.pierce = opts.pierce;
|
|
1295
|
+
const resp = await this.client.startDomMirror(req);
|
|
1296
|
+
return {
|
|
1297
|
+
root: resp.root,
|
|
1298
|
+
frameId: resp.frameId,
|
|
1299
|
+
seq: Number(resp.seq),
|
|
1300
|
+
};
|
|
1301
|
+
}
|
|
1302
|
+
/** Stops the page's mirror, every frame of it. Idempotent. */
|
|
1303
|
+
async stopDomMirror() {
|
|
1304
|
+
await this.client.stopDomMirror(create(StopDomMirrorRequestSchema, {
|
|
1305
|
+
sessionId: this.sessionId,
|
|
1306
|
+
apiKey: this.apiKey,
|
|
1307
|
+
}));
|
|
1308
|
+
}
|
|
1309
|
+
/**
|
|
1310
|
+
* Fetches a node's children and starts reporting changes inside them.
|
|
1311
|
+
* {@link DomMirror.expand} calls this and folds the result into the tree.
|
|
1312
|
+
*
|
|
1313
|
+
* On an `<iframe>` the one child is the document it hosts, and this call is
|
|
1314
|
+
* what starts mirroring that frame.
|
|
1315
|
+
*/
|
|
1316
|
+
async getDomChildren(backendNodeId, frameId = "", depth) {
|
|
1317
|
+
const req = create(GetDomChildrenRequestSchema, {
|
|
1318
|
+
sessionId: this.sessionId,
|
|
1319
|
+
apiKey: this.apiKey,
|
|
1320
|
+
backendNodeId,
|
|
1321
|
+
});
|
|
1322
|
+
if (frameId)
|
|
1323
|
+
req.frameId = frameId;
|
|
1324
|
+
if (depth !== undefined)
|
|
1325
|
+
req.depth = depth;
|
|
1326
|
+
const resp = await this.client.getDomChildren(req);
|
|
1327
|
+
return { children: resp.children, seq: Number(resp.seq) };
|
|
1328
|
+
}
|
|
1329
|
+
/**
|
|
1330
|
+
* Stops reporting changes inside a node, and inside any frame below it.
|
|
1331
|
+
* {@link DomMirror.collapse} calls this.
|
|
1332
|
+
*/
|
|
1333
|
+
async releaseDomSubtree(backendNodeId, frameId = "") {
|
|
1334
|
+
const req = create(ReleaseDomSubtreeRequestSchema, {
|
|
1335
|
+
sessionId: this.sessionId,
|
|
1336
|
+
apiKey: this.apiKey,
|
|
1337
|
+
backendNodeId,
|
|
1338
|
+
});
|
|
1339
|
+
if (frameId)
|
|
1340
|
+
req.frameId = frameId;
|
|
1341
|
+
await this.client.releaseDomSubtree(req);
|
|
1342
|
+
}
|
|
1343
|
+
/**
|
|
1344
|
+
* Returns the chain from the main document down to a node, each ancestor
|
|
1345
|
+
* with its own children, crossing into frames where it has to and starting
|
|
1346
|
+
* the ones it passes through. {@link DomMirror.reveal} calls this and
|
|
1347
|
+
* splices it in.
|
|
1348
|
+
*/
|
|
1349
|
+
async revealDomNode(backendNodeId, frameId = "") {
|
|
1350
|
+
const req = create(RevealDomNodeRequestSchema, {
|
|
1351
|
+
sessionId: this.sessionId,
|
|
1352
|
+
apiKey: this.apiKey,
|
|
1353
|
+
backendNodeId,
|
|
1354
|
+
});
|
|
1355
|
+
if (frameId)
|
|
1356
|
+
req.frameId = frameId;
|
|
1357
|
+
const resp = await this.client.revealDomNode(req);
|
|
1358
|
+
return { path: resp.path, seq: Number(resp.seq) };
|
|
1359
|
+
}
|
|
1360
|
+
/**
|
|
1361
|
+
* A frame's mutation counter, incremented on every change the document sees.
|
|
1362
|
+
* O(1) in the browser and the change detector to poll if you are not
|
|
1363
|
+
* consuming mirror events.
|
|
1364
|
+
*
|
|
1365
|
+
* Prefer this over {@link CloudBrowser.getDOMHash}, which serializes the
|
|
1366
|
+
* whole tree just to hash it. The two answer different questions: a hash
|
|
1367
|
+
* compares content, a revision only says whether this document moved since
|
|
1368
|
+
* you last asked.
|
|
1369
|
+
*/
|
|
1370
|
+
async getDomRevision(frameId = "") {
|
|
1371
|
+
const req = create(GetDomRevisionRequestSchema, {
|
|
1372
|
+
sessionId: this.sessionId,
|
|
1373
|
+
apiKey: this.apiKey,
|
|
1374
|
+
});
|
|
1375
|
+
if (frameId)
|
|
1376
|
+
req.frameId = frameId;
|
|
1377
|
+
const resp = await this.client.getDomRevision(req);
|
|
1378
|
+
return Number(resp.revision);
|
|
1379
|
+
}
|
|
1380
|
+
// ──────────────────────────────────────────────────────────────────
|
|
1050
1381
|
// Cookies
|
|
1051
1382
|
// ──────────────────────────────────────────────────────────────────
|
|
1052
1383
|
/**
|
|
@@ -1512,20 +1843,20 @@ export class CloudBrowser {
|
|
|
1512
1843
|
}
|
|
1513
1844
|
/**
|
|
1514
1845
|
* Answers your WebRTC SDP offer and starts streaming the page as a video
|
|
1515
|
-
* track
|
|
1516
|
-
* The browser is the answerer; you are the offerer (see
|
|
1846
|
+
* track. The browser is the answerer; you are the offerer (see
|
|
1517
1847
|
* {@link getStreamConfig} for the credentials to build the offer).
|
|
1518
1848
|
*
|
|
1519
1849
|
* @param offerSdp - your `RTCPeerConnection`'s SDP offer
|
|
1520
1850
|
*
|
|
1521
|
-
* @returns the SDP answer to apply as the remote description
|
|
1851
|
+
* @returns the SDP answer to apply as the remote description, plus the
|
|
1852
|
+
* viewport to map input coordinates into
|
|
1522
1853
|
*
|
|
1523
1854
|
* @throws UNKNOWN_ERROR - the offer was empty, TURN is unconfigured, or the
|
|
1524
1855
|
* browser could not negotiate the stream
|
|
1525
1856
|
*
|
|
1526
1857
|
* @example
|
|
1527
|
-
* const
|
|
1528
|
-
* await pc.setRemoteDescription({ type: "answer", sdp:
|
|
1858
|
+
* const { answerSdp, viewport } = await browser.startStream(offer.sdp);
|
|
1859
|
+
* await pc.setRemoteDescription({ type: "answer", sdp: answerSdp });
|
|
1529
1860
|
*/
|
|
1530
1861
|
async startStream(offerSdp) {
|
|
1531
1862
|
const resp = await this.client.startStream(create(StartStreamRequestSchema, {
|
|
@@ -1533,7 +1864,12 @@ export class CloudBrowser {
|
|
|
1533
1864
|
apiKey: this.apiKey,
|
|
1534
1865
|
offerSdp,
|
|
1535
1866
|
}));
|
|
1536
|
-
return
|
|
1867
|
+
return {
|
|
1868
|
+
answerSdp: resp.answerSdp,
|
|
1869
|
+
viewport: resp.viewport
|
|
1870
|
+
? { width: resp.viewport.width, height: resp.viewport.height }
|
|
1871
|
+
: null,
|
|
1872
|
+
};
|
|
1537
1873
|
}
|
|
1538
1874
|
/**
|
|
1539
1875
|
* Tears down the live video stream for the session's page. Safe to call even
|
|
@@ -1661,6 +1997,238 @@ export class CloudBrowser {
|
|
|
1661
1997
|
visible: r.visible,
|
|
1662
1998
|
}));
|
|
1663
1999
|
}
|
|
2000
|
+
// ──────────────────────────────────────────────────────────────────
|
|
2001
|
+
// Scripts
|
|
2002
|
+
// ──────────────────────────────────────────────────────────────────
|
|
2003
|
+
/**
|
|
2004
|
+
* Runs `source` in the session's browser and waits for it to finish.
|
|
2005
|
+
*
|
|
2006
|
+
* The script runs beside the browser, in a V8 isolate of its own rather than
|
|
2007
|
+
* in the page, and reaches the document through the engine: a cross-origin
|
|
2008
|
+
* `<iframe>` is read as plain `contentDocument` with no frame ids anywhere,
|
|
2009
|
+
* values come back as live objects it can assign to rather than snapshots, an
|
|
2010
|
+
* element can be handed straight to `browser.click`, and the page sees nothing
|
|
2011
|
+
* injected. Steps cost microseconds rather than network round trips, so work
|
|
2012
|
+
* that is chatty by nature — polling for a selector, walking a list,
|
|
2013
|
+
* following pagination — is affordable there. A guide for it is still to come.
|
|
2014
|
+
*
|
|
2015
|
+
* This waits for as long as the script runs, and cannot be bounded: the run
|
|
2016
|
+
* id needed to cancel only arrives with the reply. Use
|
|
2017
|
+
* {@link CloudBrowser.startScript} when the script may outlive the caller's
|
|
2018
|
+
* patience, or {@link CloudBrowser.stopScripts} to abandon what this session
|
|
2019
|
+
* is running.
|
|
2020
|
+
*
|
|
2021
|
+
* @param source - JavaScript to execute; its return value comes back as JSON
|
|
2022
|
+
*
|
|
2023
|
+
* @returns the return value and the script's whole console output. A script
|
|
2024
|
+
* that threw is reported as `success: false`, not as a rejection
|
|
2025
|
+
*
|
|
2026
|
+
* @throws UNKNOWN_ERROR - the script could not be delivered to the browser
|
|
2027
|
+
*
|
|
2028
|
+
* @example
|
|
2029
|
+
* const result = await browser.runScript(`
|
|
2030
|
+
* await browser.navigate("https://example.com");
|
|
2031
|
+
* const items = [];
|
|
2032
|
+
* for (const el of await browser.getDOM().querySelectorAll("h1")) {
|
|
2033
|
+
* items.push(el.textContent);
|
|
2034
|
+
* }
|
|
2035
|
+
* return items;
|
|
2036
|
+
* `);
|
|
2037
|
+
* console.log(result.success, result.result);
|
|
2038
|
+
*/
|
|
2039
|
+
async runScript(source) {
|
|
2040
|
+
const resp = await this.client.runScript(create(RunScriptRequestSchema, {
|
|
2041
|
+
sessionId: this.sessionId,
|
|
2042
|
+
apiKey: this.apiKey,
|
|
2043
|
+
source,
|
|
2044
|
+
}));
|
|
2045
|
+
return {
|
|
2046
|
+
success: resp.success,
|
|
2047
|
+
result: resp.result,
|
|
2048
|
+
runId: resp.runId,
|
|
2049
|
+
log: resp.log.map(scriptLogEntryFromProto),
|
|
2050
|
+
truncated: resp.truncated,
|
|
2051
|
+
};
|
|
2052
|
+
}
|
|
2053
|
+
/**
|
|
2054
|
+
* Launches `source` in the session's browser and resolves as soon as the run
|
|
2055
|
+
* is under way.
|
|
2056
|
+
*
|
|
2057
|
+
* The counterpart to {@link CloudBrowser.runScript}, for scripts that are not
|
|
2058
|
+
* worth waiting on: a watcher that runs for the life of the session, work
|
|
2059
|
+
* that should survive this page. Output arrives at `onEvent` while the caller
|
|
2060
|
+
* gets on with something else, and {@link ScriptRun.wait} collects the
|
|
2061
|
+
* outcome if it is wanted.
|
|
2062
|
+
*
|
|
2063
|
+
* Subscribing has to happen before the launch, because a detached run's
|
|
2064
|
+
* output is not kept anywhere — the browser rejects a start with nobody
|
|
2065
|
+
* listening rather than discard the script's log and result. This call does
|
|
2066
|
+
* both in that order, so nothing the script prints is missed.
|
|
2067
|
+
*
|
|
2068
|
+
* @param source - JavaScript to execute
|
|
2069
|
+
* @param onEvent - called per log line and once for the outcome; see
|
|
2070
|
+
* {@link ScriptEventHandler} for the ordering and blocking rules
|
|
2071
|
+
*
|
|
2072
|
+
* @returns ScriptRun handle for awaiting or cancelling the run
|
|
2073
|
+
*
|
|
2074
|
+
* @throws UNKNOWN_ERROR - the run could not be started
|
|
2075
|
+
*
|
|
2076
|
+
* @example
|
|
2077
|
+
* const run = await browser.startScript(source, (ev) => {
|
|
2078
|
+
* if (ev.log) console.log(ev.log.level, ev.log.message);
|
|
2079
|
+
* });
|
|
2080
|
+
* const outcome = await run.wait();
|
|
2081
|
+
*/
|
|
2082
|
+
async startScript(source, onEvent) {
|
|
2083
|
+
// Subscribe unfiltered: the run id this handle filters on does not exist
|
|
2084
|
+
// yet. Events for it pile up in the server's per-reader buffer between the
|
|
2085
|
+
// subscription and the launch, which is what that buffer is for.
|
|
2086
|
+
const abort = new AbortController();
|
|
2087
|
+
let subscribed;
|
|
2088
|
+
const ready = new Promise((resolve) => {
|
|
2089
|
+
subscribed = resolve;
|
|
2090
|
+
});
|
|
2091
|
+
const stream = this.client.streamScriptEvents(create(StreamScriptEventsRequestSchema, {
|
|
2092
|
+
sessionId: this.sessionId,
|
|
2093
|
+
apiKey: this.apiKey,
|
|
2094
|
+
}), { signal: abort.signal, onHeader: () => subscribed() });
|
|
2095
|
+
// Opening a stream does not wait for the server to start handling it. For a
|
|
2096
|
+
// capture that would only cost the first few events; here it would fail the
|
|
2097
|
+
// launch outright, since the browser refuses to start a run before a
|
|
2098
|
+
// subscription exists. The headers arrive once it is subscribed.
|
|
2099
|
+
const iterator = stream[Symbol.asyncIterator]();
|
|
2100
|
+
const first = iterator.next();
|
|
2101
|
+
// A stream that fails or closes before it is acknowledged would leave the
|
|
2102
|
+
// wait below hanging, so race the two outcomes. An event arriving ahead of
|
|
2103
|
+
// the headers is not a failure — it proves the subscription exists.
|
|
2104
|
+
const ended = first.then((result) => {
|
|
2105
|
+
if (result.done) {
|
|
2106
|
+
throw new BrowserScaleError("browserscale.startScript: stream closed before the subscription was established");
|
|
2107
|
+
}
|
|
2108
|
+
});
|
|
2109
|
+
ended.catch(() => { }); // the loser of the race must not look unhandled
|
|
2110
|
+
try {
|
|
2111
|
+
await Promise.race([ready, ended]);
|
|
2112
|
+
}
|
|
2113
|
+
catch (err) {
|
|
2114
|
+
abort.abort();
|
|
2115
|
+
throw err;
|
|
2116
|
+
}
|
|
2117
|
+
let runId;
|
|
2118
|
+
try {
|
|
2119
|
+
const resp = await this.client.startScript(create(StartScriptRequestSchema, {
|
|
2120
|
+
sessionId: this.sessionId,
|
|
2121
|
+
apiKey: this.apiKey,
|
|
2122
|
+
source,
|
|
2123
|
+
}));
|
|
2124
|
+
runId = resp.runId;
|
|
2125
|
+
}
|
|
2126
|
+
catch (err) {
|
|
2127
|
+
abort.abort();
|
|
2128
|
+
throw err;
|
|
2129
|
+
}
|
|
2130
|
+
// Resume from the pending read rather than iterating the stream again: the
|
|
2131
|
+
// first next() is already in flight and its value would otherwise be lost.
|
|
2132
|
+
const resumed = resumeStream(iterator, first);
|
|
2133
|
+
return new ScriptRun({
|
|
2134
|
+
runId,
|
|
2135
|
+
stream: resumed,
|
|
2136
|
+
onEvent,
|
|
2137
|
+
abort,
|
|
2138
|
+
cancel: (id) => this.stopScripts(id),
|
|
2139
|
+
});
|
|
2140
|
+
}
|
|
2141
|
+
/**
|
|
2142
|
+
* Watches script output in this session without starting anything.
|
|
2143
|
+
*
|
|
2144
|
+
* For the case {@link CloudBrowser.startScript} cannot cover: a run somebody
|
|
2145
|
+
* else launched, or one this page started before it reloaded. Several readers
|
|
2146
|
+
* can watch the same session, each with its own buffer.
|
|
2147
|
+
*
|
|
2148
|
+
* Only output produced from now on arrives — lines printed before the
|
|
2149
|
+
* subscription existed are not kept. A run that has already finished is
|
|
2150
|
+
* therefore invisible here; {@link CloudBrowser.listScriptRuns} is how you
|
|
2151
|
+
* tell that apart from a run that is merely quiet.
|
|
2152
|
+
*
|
|
2153
|
+
* @param runId - run to follow, or `""` to follow every run in the session
|
|
2154
|
+
* @param onEvent - called per event; see {@link ScriptEventHandler} for the
|
|
2155
|
+
* ordering and blocking rules
|
|
2156
|
+
*
|
|
2157
|
+
* @returns ScriptFollow handle for stopping the subscription
|
|
2158
|
+
*
|
|
2159
|
+
* @throws UNKNOWN_ERROR - the subscription could not be opened
|
|
2160
|
+
*
|
|
2161
|
+
* @example
|
|
2162
|
+
* const follow = await browser.followScript(runId, (ev) => {
|
|
2163
|
+
* if (ev.log) console.log(ev.log.message);
|
|
2164
|
+
* });
|
|
2165
|
+
* try {
|
|
2166
|
+
* await follow.wait();
|
|
2167
|
+
* } finally {
|
|
2168
|
+
* await follow.stop();
|
|
2169
|
+
* }
|
|
2170
|
+
*/
|
|
2171
|
+
async followScript(runId, onEvent) {
|
|
2172
|
+
const abort = new AbortController();
|
|
2173
|
+
const stream = this.client.streamScriptEvents(create(StreamScriptEventsRequestSchema, {
|
|
2174
|
+
sessionId: this.sessionId,
|
|
2175
|
+
apiKey: this.apiKey,
|
|
2176
|
+
runId,
|
|
2177
|
+
}), { signal: abort.signal });
|
|
2178
|
+
return new ScriptFollow({ stream, onEvent, abort });
|
|
2179
|
+
}
|
|
2180
|
+
/**
|
|
2181
|
+
* Cancels runs in this session and reports how many it ended.
|
|
2182
|
+
*
|
|
2183
|
+
* An empty `runId` cancels every run in the session, which is the only form
|
|
2184
|
+
* available to a caller that never learned an id — notably one abandoning a
|
|
2185
|
+
* {@link CloudBrowser.runScript}.
|
|
2186
|
+
*
|
|
2187
|
+
* @param runId - run to cancel, or `""` for all of them
|
|
2188
|
+
*
|
|
2189
|
+
* @returns how many runs were cancelled; 0 when the id named nothing in
|
|
2190
|
+
* flight
|
|
2191
|
+
*
|
|
2192
|
+
* @throws UNKNOWN_ERROR - the cancel could not be delivered
|
|
2193
|
+
*
|
|
2194
|
+
* @example
|
|
2195
|
+
* await browser.stopScripts(""); // abandon everything running
|
|
2196
|
+
*/
|
|
2197
|
+
async stopScripts(runId) {
|
|
2198
|
+
const resp = await this.client.stopScripts(create(StopScriptsRequestSchema, {
|
|
2199
|
+
sessionId: this.sessionId,
|
|
2200
|
+
apiKey: this.apiKey,
|
|
2201
|
+
runId,
|
|
2202
|
+
}));
|
|
2203
|
+
return resp.stopped;
|
|
2204
|
+
}
|
|
2205
|
+
/**
|
|
2206
|
+
* Reports the scripts still running in this session.
|
|
2207
|
+
*
|
|
2208
|
+
* Only runs in flight — a finished run is reported once on the event stream
|
|
2209
|
+
* and then forgotten, so this is not a history. Its use is finding work this
|
|
2210
|
+
* caller did not start: a script a previous page left behind, which
|
|
2211
|
+
* {@link CloudBrowser.stopScripts} needs an id to name.
|
|
2212
|
+
*
|
|
2213
|
+
* @returns one entry per run still executing
|
|
2214
|
+
*
|
|
2215
|
+
* @throws UNKNOWN_ERROR - the session could not be queried
|
|
2216
|
+
*
|
|
2217
|
+
* @example
|
|
2218
|
+
* for (const run of await browser.listScriptRuns()) {
|
|
2219
|
+
* console.log(run.runId, run.runningMs);
|
|
2220
|
+
* }
|
|
2221
|
+
*/
|
|
2222
|
+
async listScriptRuns() {
|
|
2223
|
+
const resp = await this.client.listScriptRuns(create(ListScriptRunsRequestSchema, {
|
|
2224
|
+
sessionId: this.sessionId,
|
|
2225
|
+
apiKey: this.apiKey,
|
|
2226
|
+
}));
|
|
2227
|
+
return resp.runs.map((run) => ({
|
|
2228
|
+
runId: run.runId,
|
|
2229
|
+
runningMs: Number(run.runningMs),
|
|
2230
|
+
}));
|
|
2231
|
+
}
|
|
1664
2232
|
/**
|
|
1665
2233
|
* @internal — a reaction match/action must be a css()/js() locator: node()
|
|
1666
2234
|
* and at() are rejected (a reaction watches for a condition, like a wait).
|
|
@@ -1674,3 +2242,18 @@ export class CloudBrowser {
|
|
|
1674
2242
|
}
|
|
1675
2243
|
}
|
|
1676
2244
|
}
|
|
2245
|
+
/**
|
|
2246
|
+
* Continues an async iterator whose first read is already in flight.
|
|
2247
|
+
*
|
|
2248
|
+
* startScript has to know the subscription exists before it launches anything,
|
|
2249
|
+
* and the only way to make the client send the request is to start reading. That
|
|
2250
|
+
* read cannot be discarded — it may already hold the run's first log line — so
|
|
2251
|
+
* the reader is handed a stream that replays it before continuing.
|
|
2252
|
+
*/
|
|
2253
|
+
async function* resumeStream(iterator, first) {
|
|
2254
|
+
let result = await first;
|
|
2255
|
+
while (!result.done) {
|
|
2256
|
+
yield result.value;
|
|
2257
|
+
result = await iterator.next();
|
|
2258
|
+
}
|
|
2259
|
+
}
|