nothing-browser 0.0.21 → 0.1.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/LICENSE +1 -1
- package/dist/human/index.js +25 -1
- package/dist/piggy/captcha/index.d.ts +39 -0
- package/dist/piggy/captcha/index.d.ts.map +1 -0
- package/dist/piggy/capture/index.d.ts +48 -0
- package/dist/piggy/capture/index.d.ts.map +1 -0
- package/dist/piggy/dialog/index.d.ts +28 -0
- package/dist/piggy/dialog/index.d.ts.map +1 -0
- package/dist/piggy/export/index.d.ts +62 -0
- package/dist/piggy/export/index.d.ts.map +1 -0
- package/dist/piggy/find/index.d.ts +52 -0
- package/dist/piggy/find/index.d.ts.map +1 -0
- package/dist/piggy/http/index.d.ts +14 -0
- package/dist/piggy/http/index.d.ts.map +1 -0
- package/dist/piggy/human/index.d.ts +36 -4
- package/dist/piggy/human/index.d.ts.map +1 -1
- package/dist/piggy/iframe/index.d.ts +53 -0
- package/dist/piggy/iframe/index.d.ts.map +1 -0
- package/dist/piggy/interactions/index.d.ts +24 -0
- package/dist/piggy/interactions/index.d.ts.map +1 -0
- package/dist/piggy/media/index.d.ts +11 -0
- package/dist/piggy/media/index.d.ts.map +1 -0
- package/dist/piggy/navigation/index.d.ts +16 -0
- package/dist/piggy/navigation/index.d.ts.map +1 -0
- package/dist/piggy/provide/index.d.ts +98 -0
- package/dist/piggy/provide/index.d.ts.map +1 -0
- package/dist/piggy/proxy/index.d.ts +94 -0
- package/dist/piggy/proxy/index.d.ts.map +1 -0
- package/dist/piggy/register/index.d.ts.map +1 -1
- package/dist/piggy/router/index.d.ts +38 -0
- package/dist/piggy/router/index.d.ts.map +1 -0
- package/dist/piggy/session/index.d.ts +27 -0
- package/dist/piggy/session/index.d.ts.map +1 -0
- package/dist/piggy/tabs/index.d.ts +10 -0
- package/dist/piggy/tabs/index.d.ts.map +1 -0
- package/dist/piggy/wait/index.d.ts +28 -0
- package/dist/piggy/wait/index.d.ts.map +1 -0
- package/dist/piggy.d.ts.map +1 -1
- package/dist/piggy.js +898 -181
- package/dist/register/index.js +39 -86
- package/package.json +1 -1
- package/piggy/captcha/index.d.ts +35 -0
- package/piggy/captcha/index.ts +93 -0
- package/piggy/capture/index.ts +76 -0
- package/piggy/dialog/index.d.ts +29 -0
- package/piggy/dialog/index.ts +85 -0
- package/piggy/export/index.d.ts +117 -0
- package/piggy/export/index.ts +147 -0
- package/piggy/find/index.ts +118 -0
- package/piggy/http/index.ts +65 -0
- package/piggy/human/index.ts +115 -53
- package/piggy/iframe/index.ts +79 -0
- package/piggy/interactions/index.ts +79 -0
- package/piggy/media/index.ts +46 -0
- package/piggy/navigation/index.ts +52 -0
- package/piggy/provide/index.ts +170 -0
- package/piggy/proxy/index.ts +154 -0
- package/piggy/register/index.ts +41 -59
- package/piggy/router/index.ts +69 -0
- package/piggy/session/index.ts +76 -0
- package/piggy/tabs/index.ts +22 -0
- package/piggy/wait/index.ts +90 -0
- package/piggy.ts +94 -39
- package/dist/piggy/open/index.d.ts +0 -4
- package/dist/piggy/open/index.d.ts.map +0 -1
- package/piggy/open/index.d.ts +0 -4
- package/piggy/open/index.d.ts.map +0 -1
- package/piggy/open/index.ts +0 -5
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// piggy/media/index.ts
|
|
2
|
+
import { PiggyClient } from "../client";
|
|
3
|
+
import { writeFileSync, mkdirSync } from "fs";
|
|
4
|
+
import { dirname } from "path";
|
|
5
|
+
|
|
6
|
+
export class MediaClient {
|
|
7
|
+
constructor(private client: PiggyClient) {}
|
|
8
|
+
|
|
9
|
+
// ── Screenshot ────────────────────────────────────────────────────────────
|
|
10
|
+
|
|
11
|
+
async screenshot(filePath?: string, tabId = "default"): Promise<string> {
|
|
12
|
+
const b64 = await this.client.send<string>("screenshot", { tabId });
|
|
13
|
+
if (filePath) {
|
|
14
|
+
mkdirSync(dirname(filePath), { recursive: true });
|
|
15
|
+
writeFileSync(filePath, Buffer.from(b64, "base64"));
|
|
16
|
+
return filePath;
|
|
17
|
+
}
|
|
18
|
+
return b64;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// ── PDF ───────────────────────────────────────────────────────────────────
|
|
22
|
+
|
|
23
|
+
async pdf(filePath?: string, tabId = "default"): Promise<string> {
|
|
24
|
+
const b64 = await this.client.send<string>("pdf", { tabId });
|
|
25
|
+
if (filePath) {
|
|
26
|
+
mkdirSync(dirname(filePath), { recursive: true });
|
|
27
|
+
writeFileSync(filePath, Buffer.from(b64, "base64"));
|
|
28
|
+
return filePath;
|
|
29
|
+
}
|
|
30
|
+
return b64;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// ── Image blocking ────────────────────────────────────────────────────────
|
|
34
|
+
|
|
35
|
+
blockImages(tabId = "default"): Promise<void> {
|
|
36
|
+
return this.client.send("intercept.block.images", { tabId });
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
unblockImages(tabId = "default"): Promise<void> {
|
|
40
|
+
return this.client.send("intercept.unblock.images", { tabId });
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function createMediaAPI(client: PiggyClient): MediaClient {
|
|
45
|
+
return new MediaClient(client);
|
|
46
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// piggy/navigation/index.ts
|
|
2
|
+
import { PiggyClient } from "../client";
|
|
3
|
+
|
|
4
|
+
export class NavigationClient {
|
|
5
|
+
constructor(private client: PiggyClient) {}
|
|
6
|
+
|
|
7
|
+
// ── Navigation ────────────────────────────────────────────────────────────
|
|
8
|
+
|
|
9
|
+
navigate(url: string, tabId = "default"): Promise<void> {
|
|
10
|
+
return this.client.send("navigate", { url, tabId });
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
reload(tabId = "default"): Promise<void> {
|
|
14
|
+
return this.client.send("reload", { tabId });
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
goBack(tabId = "default"): Promise<void> {
|
|
18
|
+
return this.client.send("go.back", { tabId });
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
goForward(tabId = "default"): Promise<void> {
|
|
22
|
+
return this.client.send("go.forward", { tabId });
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// ── Page info ─────────────────────────────────────────────────────────────
|
|
26
|
+
|
|
27
|
+
url(tabId = "default"): Promise<string> {
|
|
28
|
+
return this.client.send("page.url", { tabId });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
title(tabId = "default"): Promise<string> {
|
|
32
|
+
return this.client.send("page.title", { tabId });
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
content(tabId = "default"): Promise<string> {
|
|
36
|
+
return this.client.send("page.content", { tabId });
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// ── Wait ──────────────────────────────────────────────────────────────────
|
|
40
|
+
|
|
41
|
+
waitForNavigation(tabId = "default"): Promise<void> {
|
|
42
|
+
return this.client.send("wait.navigation", { tabId });
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
waitForSelector(selector: string, timeout = 10000, tabId = "default"): Promise<void> {
|
|
46
|
+
return this.client.send("wait.selector", { selector, timeout, tabId });
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function createNavigationAPI(client: PiggyClient): NavigationClient {
|
|
51
|
+
return new NavigationClient(client);
|
|
52
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
// piggy/provide/index.ts
|
|
2
|
+
import { PiggyClient } from "../client";
|
|
3
|
+
|
|
4
|
+
// ─── Shared base option ───────────────────────────────────────────────────────
|
|
5
|
+
// Every provide method targets a selector, and optionally scopes under a parent.
|
|
6
|
+
|
|
7
|
+
export interface ProvideOptions {
|
|
8
|
+
/** CSS selector for the target element(s). */
|
|
9
|
+
selector: string;
|
|
10
|
+
/**
|
|
11
|
+
* Optional parent selector to scope the query under.
|
|
12
|
+
* Equivalent to: parent.querySelector(selector)
|
|
13
|
+
*/
|
|
14
|
+
parent?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface ProvideAttrOptions extends ProvideOptions {
|
|
18
|
+
/** The attribute name to extract. */
|
|
19
|
+
attr: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface ProvideListOptions extends ProvideOptions {
|
|
23
|
+
/** Optional child selector to scope list items. */
|
|
24
|
+
itemSel?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// ─── Return types ─────────────────────────────────────────────────────────────
|
|
28
|
+
|
|
29
|
+
export interface ProvideTable {
|
|
30
|
+
headers: string[];
|
|
31
|
+
rows: string[][];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface ProvideLink {
|
|
35
|
+
text: string;
|
|
36
|
+
href: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface ProvideImage {
|
|
40
|
+
alt: string;
|
|
41
|
+
src: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface ProvideForm {
|
|
45
|
+
[name: string]: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface ProvidePage {
|
|
49
|
+
title: string;
|
|
50
|
+
url: string;
|
|
51
|
+
html: string;
|
|
52
|
+
text: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface ProvideDiv {
|
|
56
|
+
tag: string;
|
|
57
|
+
id: string;
|
|
58
|
+
cls: string;
|
|
59
|
+
text: string;
|
|
60
|
+
html: string;
|
|
61
|
+
children: ProvideDiv[];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface ProvideMeta {
|
|
65
|
+
[name: string]: string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface ProvideSelectOption {
|
|
69
|
+
text: string;
|
|
70
|
+
value: string;
|
|
71
|
+
selected: boolean;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface ProvideSelect {
|
|
75
|
+
value: string;
|
|
76
|
+
options: ProvideSelectOption[];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// ─── ProvideClient ────────────────────────────────────────────────────────────
|
|
80
|
+
// provide answers ONE question: "give me the actual value from this element."
|
|
81
|
+
// All methods take an options object { selector, parent? } so scoping is consistent.
|
|
82
|
+
// If you just want to know if something exists, use find instead.
|
|
83
|
+
|
|
84
|
+
export class ProvideClient {
|
|
85
|
+
constructor(private client: PiggyClient) {}
|
|
86
|
+
|
|
87
|
+
/** innerText of the first matched element. */
|
|
88
|
+
text(opts: ProvideOptions, tabId = "default"): Promise<string> {
|
|
89
|
+
return this.client.send("provide.text", { ...opts, tabId });
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** innerText of all matched elements. */
|
|
93
|
+
textAll(opts: ProvideOptions, tabId = "default"): Promise<string[]> {
|
|
94
|
+
return this.client.send("provide.textAll", { ...opts, tabId });
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Single attribute value from the first matched element. */
|
|
98
|
+
attr(opts: ProvideAttrOptions, tabId = "default"): Promise<string> {
|
|
99
|
+
return this.client.send("provide.attr", { ...opts, tabId });
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Attribute value from all matched elements. */
|
|
103
|
+
attrAll(opts: ProvideAttrOptions, tabId = "default"): Promise<string[]> {
|
|
104
|
+
return this.client.send("provide.attrAll", { ...opts, tabId });
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** innerHTML of the first matched element. */
|
|
108
|
+
html(opts: ProvideOptions, tabId = "default"): Promise<string> {
|
|
109
|
+
return this.client.send("provide.html", { ...opts, tabId });
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Extract a table into headers + rows. */
|
|
113
|
+
table(opts: ProvideOptions, tabId = "default"): Promise<ProvideTable> {
|
|
114
|
+
return this.client.send("provide.table", { ...opts, tabId });
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Extract a list of text items, optionally scoped to child items. */
|
|
118
|
+
list(opts: ProvideListOptions, tabId = "default"): Promise<string[]> {
|
|
119
|
+
return this.client.send("provide.list", { ...opts, tabId });
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** All links inside the matched selector. */
|
|
123
|
+
links(opts: ProvideOptions, tabId = "default"): Promise<ProvideLink[]> {
|
|
124
|
+
return this.client.send("provide.links", { ...opts, tabId });
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** All images inside the matched selector. */
|
|
128
|
+
images(opts: ProvideOptions, tabId = "default"): Promise<ProvideImage[]> {
|
|
129
|
+
return this.client.send("provide.images", { ...opts, tabId });
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Form field name→value map. */
|
|
133
|
+
form(opts: ProvideOptions, tabId = "default"): Promise<ProvideForm> {
|
|
134
|
+
return this.client.send("provide.form", { ...opts, tabId });
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Full page info: title, url, html, text. No selector needed. */
|
|
138
|
+
page(tabId = "default"): Promise<ProvidePage> {
|
|
139
|
+
return this.client.send("provide.page", { tabId });
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** Structured div tree: tag, id, cls, text, html, children[]. */
|
|
143
|
+
div(opts: ProvideOptions, tabId = "default"): Promise<ProvideDiv> {
|
|
144
|
+
return this.client.send("provide.div", { ...opts, tabId });
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** All <meta> name→content pairs. No selector needed. */
|
|
148
|
+
meta(tabId = "default"): Promise<ProvideMeta> {
|
|
149
|
+
return this.client.send("provide.meta", { tabId });
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** <select> current value + all options. */
|
|
153
|
+
select(opts: ProvideOptions, tabId = "default"): Promise<ProvideSelect> {
|
|
154
|
+
return this.client.send("provide.select", { ...opts, tabId });
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Parse JSON from element innerText or script[type=application/json].
|
|
159
|
+
* selector is optional — defaults to the first matching JSON script tag.
|
|
160
|
+
*/
|
|
161
|
+
json(opts?: Partial<ProvideOptions>, tabId = "default"): Promise<unknown> {
|
|
162
|
+
return this.client.send("provide.json", { ...opts, tabId });
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// ─── Factory helper ───────────────────────────────────────────────────────────
|
|
167
|
+
|
|
168
|
+
export function createProvideAPI(client: PiggyClient): ProvideClient {
|
|
169
|
+
return new ProvideClient(client);
|
|
170
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// piggy/proxy/index.ts
|
|
2
|
+
import { PiggyClient } from "../client";
|
|
3
|
+
|
|
4
|
+
// ─── Types ────────────────────────────────────────────────────────────────────
|
|
5
|
+
|
|
6
|
+
export type ProxyType = "http" | "https" | "socks5";
|
|
7
|
+
export type ProxyHealth = "alive" | "dead" | "checking" | "unchecked";
|
|
8
|
+
export type RotationMode = "none" | "timed" | "perrequest";
|
|
9
|
+
|
|
10
|
+
export interface ProxyEntry {
|
|
11
|
+
index: number;
|
|
12
|
+
host: string;
|
|
13
|
+
port: number;
|
|
14
|
+
type: ProxyType;
|
|
15
|
+
user: string;
|
|
16
|
+
proxy: string; // full string e.g. "socks5://user:pass@host:port"
|
|
17
|
+
latency: number;
|
|
18
|
+
health: ProxyHealth;
|
|
19
|
+
current: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface ProxyStats {
|
|
23
|
+
total: number;
|
|
24
|
+
alive: number;
|
|
25
|
+
dead: number;
|
|
26
|
+
index: number;
|
|
27
|
+
active: boolean;
|
|
28
|
+
checking: boolean;
|
|
29
|
+
skipDead: boolean;
|
|
30
|
+
autoCheck: boolean;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface ProxyListResult {
|
|
34
|
+
proxies: ProxyEntry[];
|
|
35
|
+
total: number;
|
|
36
|
+
shown: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface ProxyCurrent {
|
|
40
|
+
active: boolean;
|
|
41
|
+
host?: string;
|
|
42
|
+
port?: number;
|
|
43
|
+
type?: ProxyType;
|
|
44
|
+
user?: string;
|
|
45
|
+
proxy?: string;
|
|
46
|
+
latency?: number;
|
|
47
|
+
health?: ProxyHealth;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface ProxyConfig {
|
|
51
|
+
skipDead: boolean;
|
|
52
|
+
autoCheck: boolean;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// ─── Inline set options ───────────────────────────────────────────────────────
|
|
56
|
+
|
|
57
|
+
export type ProxySetOptions =
|
|
58
|
+
| { proxy: string }
|
|
59
|
+
| { host: string; port: number; type?: ProxyType; user?: string; pass?: string };
|
|
60
|
+
|
|
61
|
+
// ─── ProxyClient ──────────────────────────────────────────────────────────────
|
|
62
|
+
|
|
63
|
+
export class ProxyClient {
|
|
64
|
+
constructor(private client: PiggyClient) {}
|
|
65
|
+
|
|
66
|
+
/** Load proxies from a local file path. */
|
|
67
|
+
load(path: string): Promise<void> {
|
|
68
|
+
return this.client.send("proxy.load", { path });
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Fetch proxies from a URL. Result comes via proxy:loaded / proxy:fetch:failed events. */
|
|
72
|
+
fetch(url: string): Promise<void> {
|
|
73
|
+
return this.client.send("proxy.fetch", { url });
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Load an .ovpn config file. */
|
|
77
|
+
ovpn(path: string): Promise<void> {
|
|
78
|
+
return this.client.send("proxy.ovpn", { path });
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Set a single proxy inline. */
|
|
82
|
+
set(opts: ProxySetOptions): Promise<void> {
|
|
83
|
+
return this.client.send("proxy.set", opts);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Health-check all loaded proxies. Results come via events. */
|
|
87
|
+
test(): Promise<void> {
|
|
88
|
+
return this.client.send("proxy.test", {});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Abort an in-progress health check. */
|
|
92
|
+
testStop(): Promise<void> {
|
|
93
|
+
return this.client.send("proxy.test.stop", {});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Rotate to the next proxy in the pool. */
|
|
97
|
+
next(): Promise<string> {
|
|
98
|
+
return this.client.send("proxy.next", {});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Alias for next(). */
|
|
102
|
+
rotate(): Promise<string> {
|
|
103
|
+
return this.client.send("proxy.rotate", {});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Disable the proxy — use the real IP. */
|
|
107
|
+
disable(): Promise<void> {
|
|
108
|
+
return this.client.send("proxy.disable", {});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Re-enable the current proxy. */
|
|
112
|
+
enable(): Promise<void> {
|
|
113
|
+
return this.client.send("proxy.enable", {});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Get the current active proxy details. */
|
|
117
|
+
current(): Promise<ProxyCurrent> {
|
|
118
|
+
return this.client.send("proxy.current", {});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Get pool stats: total, alive, dead, index, active, checking. */
|
|
122
|
+
stats(): Promise<ProxyStats> {
|
|
123
|
+
return this.client.send("proxy.stats", {});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** List all proxies with health info. limit defaults to 500. */
|
|
127
|
+
list(limit?: number): Promise<ProxyListResult> {
|
|
128
|
+
return this.client.send("proxy.list", { limit });
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Set rotation mode and interval (seconds, only used for "timed"). */
|
|
132
|
+
rotation(mode: RotationMode, interval?: number): Promise<void> {
|
|
133
|
+
return this.client.send("proxy.rotation", { mode, interval });
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Set skipDead / autoCheck flags. */
|
|
137
|
+
config(opts: Partial<ProxyConfig>): Promise<ProxyConfig> {
|
|
138
|
+
return this.client.send("proxy.config", opts);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Save the current proxy list to a file.
|
|
143
|
+
* filter: "all" | "alive" | "dead" — defaults to "all".
|
|
144
|
+
*/
|
|
145
|
+
save(path: string, filter?: "all" | "alive" | "dead"): Promise<void> {
|
|
146
|
+
return this.client.send("proxy.save", { path, filter });
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// ─── Factory helper ───────────────────────────────────────────────────────────
|
|
151
|
+
|
|
152
|
+
export function createProxyAPI(client: PiggyClient): ProxyClient {
|
|
153
|
+
return new ProxyClient(client);
|
|
154
|
+
}
|
package/piggy/register/index.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// piggy/register/index.ts
|
|
2
2
|
import { PiggyClient } from "../client";
|
|
3
|
+
import { HumanClient } from "../human";
|
|
3
4
|
import logger from "../logger";
|
|
4
5
|
import { routeRegistry, keepAliveSites, type RouteHandler, type BeforeMiddleware, type RouteDetail } from "../server";
|
|
5
|
-
import { randomDelay, humanTypeSequence } from "../human";
|
|
6
6
|
import { buildRespondScript, buildModifyResponseScript } from "../intercept/scripts";
|
|
7
7
|
import { storeRecord } from "../store";
|
|
8
8
|
import { TabPool } from "../pool";
|
|
@@ -37,8 +37,6 @@ export function createSiteObject(
|
|
|
37
37
|
let _currentUrl: string = registeredUrl;
|
|
38
38
|
let _modifyRuleCounter = 0;
|
|
39
39
|
|
|
40
|
-
// ── helpers ────────────────────────────────────────────────────────────────
|
|
41
|
-
// If pool exists, run fn with a pool tab. Otherwise use the fixed tabId.
|
|
42
40
|
function withTab<T>(fn: (t: string) => Promise<T>): Promise<T> {
|
|
43
41
|
return pool ? pool.withTab(fn) : fn(tabId);
|
|
44
42
|
}
|
|
@@ -69,10 +67,8 @@ export function createSiteObject(
|
|
|
69
67
|
_tabId: tabId,
|
|
70
68
|
_pool: pool ?? null,
|
|
71
69
|
|
|
72
|
-
// ── Pool stats ────────────────────────────────────────────────────────────
|
|
73
70
|
poolStats: () => pool?.stats ?? null,
|
|
74
71
|
|
|
75
|
-
// ── Navigation ────────────────────────────────────────────────────────────
|
|
76
72
|
navigate: (url?: string, opts?: { retries?: number }) => {
|
|
77
73
|
const target = url ?? registeredUrl;
|
|
78
74
|
return withTab(t =>
|
|
@@ -115,7 +111,6 @@ export function createSiteObject(
|
|
|
115
111
|
waitForResponse: (pattern: string, timeout = 30000) =>
|
|
116
112
|
withTab(t => client.waitForResponse(pattern, timeout, t)),
|
|
117
113
|
|
|
118
|
-
// ── Init Script ───────────────────────────────────────────────────────────
|
|
119
114
|
addInitScript: async (js: string | (() => void)) => {
|
|
120
115
|
const code = typeof js === "function" ? `(${js.toString()})();` : js;
|
|
121
116
|
await withTab(t => client.addInitScript(code, t));
|
|
@@ -123,7 +118,6 @@ export function createSiteObject(
|
|
|
123
118
|
return site;
|
|
124
119
|
},
|
|
125
120
|
|
|
126
|
-
// ── Events ────────────────────────────────────────────────────────────────
|
|
127
121
|
on: (event: string, handler: (data: any) => void): (() => void) => {
|
|
128
122
|
if (!_eventListeners.has(event)) _eventListeners.set(event, new Set());
|
|
129
123
|
_eventListeners.get(event)!.add(handler);
|
|
@@ -138,12 +132,11 @@ export function createSiteObject(
|
|
|
138
132
|
_eventListeners.get(event)?.delete(handler);
|
|
139
133
|
},
|
|
140
134
|
|
|
141
|
-
// ── Interactions ──────────────────────────────────────────────────────────
|
|
142
135
|
click: (selector: string, opts?: { retries?: number; timeout?: number }) =>
|
|
143
136
|
withErrScreen(() =>
|
|
144
137
|
withTab(t =>
|
|
145
138
|
retry(name, async () => {
|
|
146
|
-
if (humanMode) await
|
|
139
|
+
if (humanMode) await new Promise(r => setTimeout(r, 80 + Math.random() * 140));
|
|
147
140
|
await client.waitForSelector(selector, opts?.timeout ?? 15000, t);
|
|
148
141
|
const ok = await client.click(selector, t);
|
|
149
142
|
if (!ok) throw new Error(`click failed: ${selector}`);
|
|
@@ -157,7 +150,7 @@ export function createSiteObject(
|
|
|
157
150
|
doubleClick: (selector: string) =>
|
|
158
151
|
withErrScreen(() =>
|
|
159
152
|
withTab(async t => {
|
|
160
|
-
if (humanMode) await
|
|
153
|
+
if (humanMode) await new Promise(r => setTimeout(r, 80 + Math.random() * 120));
|
|
161
154
|
return client.doubleClick(selector, t);
|
|
162
155
|
}),
|
|
163
156
|
`dblclick(${selector})`
|
|
@@ -166,52 +159,50 @@ export function createSiteObject(
|
|
|
166
159
|
hover: (selector: string) =>
|
|
167
160
|
withErrScreen(() =>
|
|
168
161
|
withTab(async t => {
|
|
169
|
-
if (humanMode) await
|
|
162
|
+
if (humanMode) await new Promise(r => setTimeout(r, 50 + Math.random() * 100));
|
|
170
163
|
return client.hover(selector, t);
|
|
171
164
|
}),
|
|
172
165
|
`hover(${selector})`
|
|
173
166
|
),
|
|
174
167
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
if (
|
|
184
|
-
|
|
168
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
169
|
+
// type – uses HumanClient when humanMode is true, otherwise basic client.type
|
|
170
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
171
|
+
type: (selector: string, text: string, opts?: { delay?: number; retries?: number; clear?: boolean; speed?: number }) =>
|
|
172
|
+
withErrScreen(() =>
|
|
173
|
+
withTab(async t => {
|
|
174
|
+
await client.waitForSelector(selector, 30000, t);
|
|
175
|
+
|
|
176
|
+
if (humanMode) {
|
|
177
|
+
const human = new HumanClient(client);
|
|
178
|
+
await human.type({
|
|
179
|
+
selector,
|
|
180
|
+
text,
|
|
181
|
+
clear: opts?.clear ?? false,
|
|
182
|
+
speed: opts?.speed
|
|
183
|
+
}, t);
|
|
184
|
+
} else {
|
|
185
|
+
// Fallback to basic type (with optional clear)
|
|
186
|
+
if (opts?.clear) {
|
|
187
|
+
await client.evaluate(`
|
|
188
|
+
const el = document.querySelector('${selector.replace(/'/g, "\\'")}');
|
|
189
|
+
if (el) { el.value = ''; el.dispatchEvent(new Event('input', { bubbles: true })); }
|
|
190
|
+
`, t);
|
|
191
|
+
}
|
|
192
|
+
await client.type(selector, text, t);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Fire blur to trigger reactive frameworks
|
|
185
196
|
await client.evaluate(`
|
|
186
|
-
(
|
|
187
|
-
const el = document.querySelector('${selector}');
|
|
188
|
-
const nativeSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value').set;
|
|
189
|
-
nativeSetter.call(el, '${current.replace(/'/g, "\\'")}');
|
|
190
|
-
el.dispatchEvent(new Event('input', { bubbles: true }));
|
|
191
|
-
el.dispatchEvent(new Event('change', { bubbles: true }));
|
|
192
|
-
})()
|
|
197
|
+
document.querySelector('${selector.replace(/'/g, "\\'")}')?.dispatchEvent(new Event('blur', { bubbles: true }));
|
|
193
198
|
`, t);
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
await new Promise(r => setTimeout(r, opts.delay));
|
|
202
|
-
}
|
|
203
|
-
} else {
|
|
204
|
-
await client.type(selector, text, t);
|
|
205
|
-
}
|
|
206
|
-
// fire blur so Phoenix/Angular/React pick up the final value
|
|
207
|
-
await client.evaluate(`
|
|
208
|
-
document.querySelector('${selector}').dispatchEvent(new Event('blur', { bubbles: true }))
|
|
209
|
-
`, t);
|
|
210
|
-
logger.success(`[${name}] typed into: ${selector}`);
|
|
211
|
-
return true;
|
|
212
|
-
}),
|
|
213
|
-
`type(${selector})`
|
|
214
|
-
),
|
|
199
|
+
|
|
200
|
+
logger.success(`[${name}] typed into: ${selector}`);
|
|
201
|
+
return true;
|
|
202
|
+
}),
|
|
203
|
+
`type(${selector})`
|
|
204
|
+
),
|
|
205
|
+
|
|
215
206
|
select: (selector: string, value: string) => withTab(t => client.select(selector, value, t)),
|
|
216
207
|
|
|
217
208
|
evaluate: (js: string | (() => any), ...args: any[]) => {
|
|
@@ -240,7 +231,7 @@ export function createSiteObject(
|
|
|
240
231
|
const chunk = px / steps;
|
|
241
232
|
for (let i = 0; i < steps; i++) {
|
|
242
233
|
await client.scrollBy(chunk, t);
|
|
243
|
-
await
|
|
234
|
+
await new Promise(r => setTimeout(r, 30 + Math.random() * 50));
|
|
244
235
|
}
|
|
245
236
|
} else {
|
|
246
237
|
await client.scrollBy(px, t);
|
|
@@ -248,7 +239,6 @@ export function createSiteObject(
|
|
|
248
239
|
}) as Promise<void>,
|
|
249
240
|
},
|
|
250
241
|
|
|
251
|
-
// ── Fetch ─────────────────────────────────────────────────────────────────
|
|
252
242
|
fetchText: (selector: string) => withTab(t => client.fetchText(selector, t)),
|
|
253
243
|
|
|
254
244
|
fetchLinks: async (selector: string) => {
|
|
@@ -268,7 +258,6 @@ export function createSiteObject(
|
|
|
268
258
|
id: (query: string) => withTab(t => client.searchId(query, t)),
|
|
269
259
|
},
|
|
270
260
|
|
|
271
|
-
// ── Screenshot / PDF ──────────────────────────────────────────────────────
|
|
272
261
|
screenshot: async (filePath?: string) => {
|
|
273
262
|
const r = await withTab(t => client.screenshot(filePath, t));
|
|
274
263
|
logger.success(`[${name}] screenshot → ${filePath ?? "base64"}`);
|
|
@@ -284,7 +273,6 @@ export function createSiteObject(
|
|
|
284
273
|
blockImages: () => withTab(async t => { await client.blockImages(t); logger.info(`[${name}] images blocked`); }),
|
|
285
274
|
unblockImages: () => withTab(async t => { await client.unblockImages(t); logger.info(`[${name}] images unblocked`); }),
|
|
286
275
|
|
|
287
|
-
// ── Cookies ───────────────────────────────────────────────────────────────
|
|
288
276
|
cookies: {
|
|
289
277
|
set: async (cookieName: string, value: string, domain: string, path = "/") => {
|
|
290
278
|
await withTab(t => client.setCookie(cookieName, value, domain, path, t));
|
|
@@ -298,7 +286,6 @@ export function createSiteObject(
|
|
|
298
286
|
list: () => withTab(t => client.listCookies(t)),
|
|
299
287
|
},
|
|
300
288
|
|
|
301
|
-
// ── Interception ──────────────────────────────────────────────────────────
|
|
302
289
|
intercept: {
|
|
303
290
|
block: async (pattern: string) => {
|
|
304
291
|
await withTab(t => client.addInterceptRule("block", pattern, {}, t));
|
|
@@ -399,7 +386,6 @@ export function createSiteObject(
|
|
|
399
386
|
},
|
|
400
387
|
},
|
|
401
388
|
|
|
402
|
-
// ── Network capture ───────────────────────────────────────────────────────
|
|
403
389
|
capture: {
|
|
404
390
|
start: () => withTab(async t => { await client.captureStart(t); logger.info(`[${name}] capture started`); }),
|
|
405
391
|
stop: () => withTab(async t => { await client.captureStop(t); logger.info(`[${name}] capture stopped`); }),
|
|
@@ -410,7 +396,6 @@ export function createSiteObject(
|
|
|
410
396
|
clear: () => withTab(async t => { await client.captureClear(t); logger.info(`[${name}] capture cleared`); }),
|
|
411
397
|
},
|
|
412
398
|
|
|
413
|
-
// ── Session ───────────────────────────────────────────────────────────────
|
|
414
399
|
session: {
|
|
415
400
|
export: async () => {
|
|
416
401
|
const data = await withTab(t => client.sessionExport(t));
|
|
@@ -423,7 +408,6 @@ export function createSiteObject(
|
|
|
423
408
|
},
|
|
424
409
|
},
|
|
425
410
|
|
|
426
|
-
// ── Expose Function ───────────────────────────────────────────────────────
|
|
427
411
|
exposeFunction: async (fnName: string, handler: (data: any) => Promise<any> | any) => {
|
|
428
412
|
await client.exposeFunction(fnName, handler, tabId);
|
|
429
413
|
logger.success(`[${name}] exposed function: ${fnName}`);
|
|
@@ -447,7 +431,6 @@ export function createSiteObject(
|
|
|
447
431
|
return site;
|
|
448
432
|
},
|
|
449
433
|
|
|
450
|
-
// ── Store ─────────────────────────────────────────────────────────────────
|
|
451
434
|
store: async (
|
|
452
435
|
data: Record<string, any> | Record<string, any>[],
|
|
453
436
|
schemaName?: string
|
|
@@ -458,7 +441,6 @@ export function createSiteObject(
|
|
|
458
441
|
return result;
|
|
459
442
|
},
|
|
460
443
|
|
|
461
|
-
// ── Elysia API ────────────────────────────────────────────────────────────
|
|
462
444
|
api: (
|
|
463
445
|
path: string,
|
|
464
446
|
handler: RouteHandler,
|