Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/examples/detach.ts
1028 views
1
import agent, { LocationStatus } from 'secret-agent';
2
3
/**
4
* The first run of this will result in a script taking 60+ seconds.
5
*
6
* Subsequent runs will "learn" the queries that ran against the frozenTab and run significantly faster.
7
*/
8
9
async function run() {
10
console.time('Detach');
11
await agent.goto('https://chromium.googlesource.com/chromium/src/+refs');
12
await agent.activeTab.waitForLoad(LocationStatus.DomContentLoaded);
13
await agent.waitForPaintingStable();
14
15
console.timeLog('Detach', 'got sync result');
16
const frozenTab = await agent.detach(agent.activeTab);
17
console.timeLog('Detach', 'detached');
18
const { document } = frozenTab;
19
console.log(document);
20
21
const wrapperElements = await document.querySelectorAll('.RefList');
22
console.log(wrapperElements);
23
console.timeLog('Detach', 'wrapped list');
24
const versions = agent.output;
25
for (const elem of wrapperElements) {
26
console.log(elem);
27
const innerText = await elem.querySelector('.RefList-title').innerText;
28
if (innerText === 'Tags') {
29
console.timeLog('Detach', 'found tags');
30
const aElems = await elem.querySelectorAll('ul.RefList-items li a');
31
console.timeLog('Detach', 'loaded tag elems');
32
33
for (const aElem of aElems) {
34
const version = await aElem.innerText;
35
versions.push(version);
36
}
37
38
console.timeLog('Detach', 'looped through versions');
39
break;
40
}
41
}
42
console.log(versions.length);
43
console.timeEnd('Detach');
44
await agent.close();
45
}
46
47
run().catch(error => console.log(error));
48
49