@trawlme/skills 1.2.2 → 1.2.4
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/package.json +3 -2
- package/skills/trawl-cli/SKILL.md +9 -2
- package/skills/trawl-scrap-account/SKILL.md +23 -15
- package/skills/trawl-scrap-account/references/cookie-injection.md +1 -1
- package/skills/trawl-scrap-account/references/session-flow.md +9 -9
- package/skills/trawl-scrap-design/SKILL.md +15 -3
- package/skills/trawl-scrap-design/references/anti-patterns.md +6 -0
- package/skills/trawl-scrap-design/references/interaction-patterns.md +23 -0
- package/skills/trawl-scrap-local-test/SKILL.md +26 -4
- package/skills/trawl-scrap-local-test/scripts/build-context.mjs +44 -0
- package/skills/trawl-scrap-local-test/scripts/run-local.mjs +6 -20
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trawlme/skills",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.4",
|
|
4
4
|
"description": "Claude Code skills for the Trawl CLI — teaches Claude how to manage scraps via @trawlme/cli",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
"skills/**/*.md",
|
|
11
11
|
"skills/**/references/**",
|
|
12
12
|
"skills/**/run-local.mjs",
|
|
13
|
+
"skills/**/build-context.mjs",
|
|
13
14
|
"bin/install.js",
|
|
14
15
|
"README.md",
|
|
15
16
|
"LICENSE"
|
|
@@ -27,7 +28,7 @@
|
|
|
27
28
|
},
|
|
28
29
|
"scripts": {
|
|
29
30
|
"install-skill": "node bin/install.js",
|
|
30
|
-
"test": "node bin/install.test.mjs && node skills/trawl-scrap-local-test/scripts/run-local.test.mjs"
|
|
31
|
+
"test": "node bin/install.test.mjs && node skills/trawl-scrap-local-test/scripts/run-local.test.mjs && node skills/trawl-scrap-local-test/scripts/build-context.test.mjs"
|
|
31
32
|
},
|
|
32
33
|
"keywords": [
|
|
33
34
|
"trawl",
|
|
@@ -118,9 +118,16 @@ trawl scraps data "$ID" --json
|
|
|
118
118
|
|
|
119
119
|
**"Why did this scrap fail?"**
|
|
120
120
|
```bash
|
|
121
|
-
trawl scraps
|
|
122
|
-
trawl scraps
|
|
121
|
+
trawl scraps doctor <id> # last-run diagnosis: status/statusDetail, error message, failed selector, block status, empty-context, proxy Tier (0-4), regression, autofix summary
|
|
122
|
+
trawl scraps doctor <id> --autofix # same + full autofix diff/dry-run/knowledge inline
|
|
123
|
+
trawl scraps doctor <id> --json # raw run + autofix JSON
|
|
124
|
+
trawl scraps autofix <id> # full last auto-fix attempt: decision, unified fix diff, dry-run X/Y, knowledge fingerprint + confidence%
|
|
125
|
+
trawl scraps autofix <id> --json # raw autofix JSON
|
|
126
|
+
trawl scraps data <id> --errors # failure diagnostics inline with the data command
|
|
127
|
+
trawl scraps snapshot <id> --error -o /tmp/error.html # download the error-path HTML
|
|
128
|
+
trawl scraps snapshot <id> -o /tmp/page.html # download the captured page HTML
|
|
123
129
|
```
|
|
130
|
+
Note: the CLI surfaces the abstract proxy Tier (0–4) + diagnostics + autofix, but **not** cost or proxy nature/IP/vendor — those are admin-only by design.
|
|
124
131
|
|
|
125
132
|
**"Create a job that runs every morning"**
|
|
126
133
|
```bash
|
|
@@ -15,33 +15,35 @@ Injecting cookies does not change the worker's fingerprint policy.
|
|
|
15
15
|
|
|
16
16
|
## Decision tree
|
|
17
17
|
|
|
18
|
-
1. **Plain username/password login, no MFA** → use **Trawl-managed credentials** (section below). Trawl stores credentials encrypted; the script reads `account.username` / `account.password` at runtime and calls `saveSession()` after a successful login to reuse the session on subsequent runs.
|
|
18
|
+
1. **Plain username/password login, no MFA** → use **Trawl-managed credentials** (section below). Trawl stores credentials encrypted; the script reads `TRAWL.account.username` / `TRAWL.account.password` at runtime and calls `saveSession()` after a successful login to reuse the session on subsequent runs.
|
|
19
19
|
|
|
20
20
|
2. **One-shot test or short-lived experiment, or you want to verify your cookies work before setting up persistence** → use **Flavour A — Embedded BYO-cookies**. Hard-code the cookie array directly in the script body. Accepts the trade-off that cookies are in plain text in the script source.
|
|
21
21
|
|
|
22
|
-
3. **MFA, SSO, OAuth, or you don't want to hand credentials to Trawl** → use **Flavour B — Persisted BYO-cookies**. Export cookies from a logged-in browser session, push them into `account.session.cookies` via
|
|
22
|
+
3. **MFA, SSO, OAuth, or you don't want to hand credentials to Trawl** → use **Flavour B — Persisted BYO-cookies**. Export cookies from a logged-in browser session, push them into `TRAWL.account.session.cookies` via UI, CLI, or API, and the script reads them back before navigation. Cookies are encrypted at rest.
|
|
23
23
|
|
|
24
24
|
## Trawl-managed credentials (legacy flow)
|
|
25
25
|
|
|
26
|
-
The
|
|
26
|
+
The worker injects the account credentials under `TRAWL.account`:
|
|
27
27
|
|
|
28
|
-
- `account.username` — the username stored for this scrap account.
|
|
29
|
-
- `account.password` — the password stored for this scrap account.
|
|
28
|
+
- `TRAWL.account.username` — the username stored for this scrap account (alias: `account.username`).
|
|
29
|
+
- `TRAWL.account.password` — the password stored for this scrap account (alias: `account.password`).
|
|
30
30
|
|
|
31
|
-
After a successful login, call `saveSession(await page.cookies())` immediately. The worker persists the cookie array encrypted at rest and exposes it as `account.session.cookies` on the next run.
|
|
31
|
+
After a successful login, call `saveSession(await page.cookies())` immediately. The worker persists the cookie array encrypted at rest and exposes it as `TRAWL.account.session.cookies` on the next run.
|
|
32
|
+
|
|
33
|
+
> Legacy bare `account.*` (e.g. `account.username`) still works as an alias.
|
|
32
34
|
|
|
33
35
|
```js
|
|
34
36
|
const page = await browser.newPage();
|
|
35
37
|
|
|
36
|
-
if (account
|
|
38
|
+
if (TRAWL.account?.session?.cookies) {
|
|
37
39
|
// Saved session exists — restore cookies and skip the login flow.
|
|
38
|
-
await page.setCookie(...account.session.cookies);
|
|
40
|
+
await page.setCookie(...TRAWL.account.session.cookies);
|
|
39
41
|
await page.goto('https://example.com/dashboard', { waitUntil: 'domcontentloaded' });
|
|
40
42
|
} else {
|
|
41
43
|
// First run (or cleared session) — perform full login.
|
|
42
44
|
await page.goto('https://example.com/login', { waitUntil: 'domcontentloaded' });
|
|
43
|
-
await page.type('#username', account.username);
|
|
44
|
-
await page.type('#password', account.password);
|
|
45
|
+
await page.type('#username', TRAWL.account.username);
|
|
46
|
+
await page.type('#password', TRAWL.account.password);
|
|
45
47
|
await Promise.all([
|
|
46
48
|
page.click('button[type=submit]'),
|
|
47
49
|
page.waitForNavigation({ waitUntil: 'networkidle2' }),
|
|
@@ -78,19 +80,25 @@ await page.goto('https://example.com/dashboard', { waitUntil: 'domcontentloaded'
|
|
|
78
80
|
|
|
79
81
|
## Flavour B — Persisted BYO-cookies
|
|
80
82
|
|
|
81
|
-
Export cookies from a logged-in browser session
|
|
83
|
+
Export cookies from a logged-in browser session. Three write paths exist for `TRAWL.account.session.cookies` (any one is fine; the scrap source reads them back identically):
|
|
84
|
+
|
|
85
|
+
- **UI** (one-off / interactive): scrap settings drawer → Account section → **Upload session cookies** → paste a Puppeteer cookie JSON array → Save.
|
|
86
|
+
- **CLI** (scripted / batch): `trawl scraps account session set <scrap-id> --cookies <file>` (npm `@trawlme/cli` ≥ 1.14.0).
|
|
87
|
+
- **API** (custom tooling): `PUT /api/scraps/:scrapId/account/session` with body `{ cookies: [...] }`.
|
|
88
|
+
|
|
89
|
+
The worker encrypts them at rest; the script reads the same global as the managed flow:
|
|
82
90
|
|
|
83
91
|
```js
|
|
84
|
-
if (!account
|
|
85
|
-
throw new Error('Flavour B requires account.session.cookies — push them via
|
|
92
|
+
if (!TRAWL.account?.session?.cookies) {
|
|
93
|
+
throw new Error('Flavour B requires TRAWL.account.session.cookies — push them via UI / CLI / API first.');
|
|
86
94
|
}
|
|
87
95
|
|
|
88
96
|
const page = await browser.newPage();
|
|
89
|
-
await page.setCookie(...account.session.cookies);
|
|
97
|
+
await page.setCookie(...TRAWL.account.session.cookies);
|
|
90
98
|
await page.goto('https://example.com/dashboard', { waitUntil: 'domcontentloaded' });
|
|
91
99
|
```
|
|
92
100
|
|
|
93
|
-
|
|
101
|
+
See `references/cookie-injection.md` for domain matching, HttpOnly export, localStorage, and expiry handling.
|
|
94
102
|
|
|
95
103
|
## Anti-patterns to avoid
|
|
96
104
|
|
|
@@ -68,7 +68,7 @@ await page.reload({ waitUntil: 'domcontentloaded' });
|
|
|
68
68
|
|
|
69
69
|
The worker starts a fresh browser context on each run. Persistence requires:
|
|
70
70
|
|
|
71
|
-
- `saveSession(await page.cookies())` — stores encrypted, exposed as `account.session.cookies` next run.
|
|
71
|
+
- `saveSession(await page.cookies())` — stores encrypted, exposed as `TRAWL.account.session.cookies` next run (alias: `account.session.cookies`).
|
|
72
72
|
- Flavour B persistence endpoint — cookies pushed externally.
|
|
73
73
|
|
|
74
74
|
Flavour A re-injects cookies from the script source on every run.
|
|
@@ -10,22 +10,22 @@ Async helper injected by the worker. Call after confirming login succeeded — n
|
|
|
10
10
|
await saveSession(await page.cookies());
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
The worker persists the cookie array encrypted. On the next run, `account.session.cookies` is populated and the login flow is skipped. Call once per run only.
|
|
13
|
+
The worker persists the cookie array encrypted. On the next run, `TRAWL.account.session.cookies` is populated and the login flow is skipped. Call once per run only.
|
|
14
14
|
|
|
15
15
|
### Reuse pattern (full code)
|
|
16
16
|
|
|
17
17
|
```js
|
|
18
18
|
const page = await browser.newPage();
|
|
19
19
|
|
|
20
|
-
if (account
|
|
20
|
+
if (TRAWL.account?.session?.cookies) {
|
|
21
21
|
// Saved session exists — restore cookies and skip the login flow.
|
|
22
|
-
await page.setCookie(...account.session.cookies);
|
|
22
|
+
await page.setCookie(...TRAWL.account.session.cookies);
|
|
23
23
|
await page.goto('https://example.com/dashboard', { waitUntil: 'domcontentloaded' });
|
|
24
24
|
} else {
|
|
25
25
|
// First run (or cleared session) — perform full login.
|
|
26
26
|
await page.goto('https://example.com/login', { waitUntil: 'domcontentloaded' });
|
|
27
|
-
await page.type('#username', account.username);
|
|
28
|
-
await page.type('#password', account.password);
|
|
27
|
+
await page.type('#username', TRAWL.account.username);
|
|
28
|
+
await page.type('#password', TRAWL.account.password);
|
|
29
29
|
await Promise.all([
|
|
30
30
|
page.click('button[type=submit]'),
|
|
31
31
|
page.waitForNavigation({ waitUntil: 'networkidle2' }),
|
|
@@ -35,7 +35,7 @@ if (account.session?.cookies) {
|
|
|
35
35
|
}
|
|
36
36
|
```
|
|
37
37
|
|
|
38
|
-
`account.session` is `undefined` on the first run — hence the optional chain. Both branches must reach the same post-login URL before scraping starts.
|
|
38
|
+
`TRAWL.account.session` is `undefined` on the first run — hence the optional chain. Both branches must reach the same post-login URL before scraping starts.
|
|
39
39
|
|
|
40
40
|
### Forced re-login
|
|
41
41
|
|
|
@@ -43,7 +43,7 @@ if (account.session?.cookies) {
|
|
|
43
43
|
trawl scraps account clear-session <scrap-id>
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
-
The next run finds `account.session` undefined, falls into the login branch, and re-runs `saveSession` on success.
|
|
46
|
+
The next run finds `TRAWL.account.session` undefined, falls into the login branch, and re-runs `saveSession` on success.
|
|
47
47
|
|
|
48
48
|
### When the login flow itself breaks
|
|
49
49
|
|
|
@@ -58,8 +58,8 @@ const page = await browser.newPage();
|
|
|
58
58
|
|
|
59
59
|
try {
|
|
60
60
|
await page.goto('https://example.com/login', { waitUntil: 'domcontentloaded' });
|
|
61
|
-
await page.type('#username', account.username);
|
|
62
|
-
await page.type('#password', account.password);
|
|
61
|
+
await page.type('#username', TRAWL.account.username);
|
|
62
|
+
await page.type('#password', TRAWL.account.password);
|
|
63
63
|
await Promise.all([
|
|
64
64
|
page.click('button[type=submit]'),
|
|
65
65
|
page.waitForNavigation({ waitUntil: 'networkidle2', timeout: 10000 }),
|
|
@@ -33,6 +33,12 @@ const ADD_TO_CART_SEL = '[role="button"][aria-label="Add to cart"]';
|
|
|
33
33
|
const price = await page.$eval(PRICE_SEL, (el) => el.textContent.trim());
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
+
**Avoid obfuscated hash classes** (`css-1a2b3c`, styled-components like `fPSBzf`) — they rotate every deploy. When the only classes are hashes:
|
|
37
|
+
- prefer `data-*` / `aria-*` / `role` / semantic tags (`<article>`, `<address>`) / `time[datetime]`
|
|
38
|
+
- module-CSS (stable prefix + hash suffix) → match the prefix: `[class*="ListingCard_price"]`
|
|
39
|
+
- `aria-label` often encodes the value (`[aria-label*="Current price"]`) — more stable than any class
|
|
40
|
+
- **JSON beats DOM**: `window.__NEXT_DATA__` (Next.js SSR) and JSON-LD (`script[type="application/ld+json"]`, schema.org) carry the data pre-render — use as primary, DOM as fallback.
|
|
41
|
+
|
|
36
42
|
## Waits
|
|
37
43
|
|
|
38
44
|
- **`page.waitForSelector(selector)`** — most targeted; use as the default.
|
|
@@ -88,6 +94,8 @@ if (!category) throw new Error('Missing required param: category (set in Setting
|
|
|
88
94
|
await page.goto(url, { waitUntil: 'domcontentloaded' });
|
|
89
95
|
```
|
|
90
96
|
|
|
97
|
+
**URL hygiene**: a page can return HTTP 200 with a soft-404 ("page not found") body — verify *content*, not status. Prefer search/category URLs over guessed deep-link IDs (product/hotel IDs go stale). Pre-wire locale in the URL (`/en/`, `intl=nosplash`, country path) to skip region splashes.
|
|
98
|
+
|
|
91
99
|
## Resilience
|
|
92
100
|
|
|
93
101
|
Wrap each logical field in its own `try/catch` and return what you have. Throw only for structural failures that make the entire run meaningless.
|
|
@@ -101,9 +109,8 @@ try {
|
|
|
101
109
|
// continue — return partial data with price: null
|
|
102
110
|
}
|
|
103
111
|
|
|
104
|
-
// Structural failure:
|
|
105
|
-
|
|
106
|
-
if (bodyText.includes('Access Denied')) throw new Error('Access denied — escalate proxy tier');
|
|
112
|
+
// Structural failure: required param missing — throw immediately
|
|
113
|
+
// Do NOT sniff for bot-challenge markers ("Access Denied", "Just a moment") — see anti-patterns.md
|
|
107
114
|
```
|
|
108
115
|
|
|
109
116
|
**Throw only for:**
|
|
@@ -111,6 +118,10 @@ if (bodyText.includes('Access Denied')) throw new Error('Access denied — escal
|
|
|
111
118
|
- Page never loaded or returned a fatal error
|
|
112
119
|
- Required param missing
|
|
113
120
|
|
|
121
|
+
## Hard anti-bot sites
|
|
122
|
+
|
|
123
|
+
The platform auto-escalates proxy tiers when a run comes back empty — you usually don't manage tiers at all. For a site you *know* is heavily protected, you can pin a higher tier from the start so the first run doesn't waste a cycle; the top tier passes most protections. That's the whole knob. Don't try to outsmart protection in your script — return what you can and let the platform escalate.
|
|
124
|
+
|
|
114
125
|
## Anti-patterns (don't do this)
|
|
115
126
|
|
|
116
127
|
Full detail in `references/anti-patterns.md`. Summary:
|
|
@@ -120,6 +131,7 @@ Full detail in `references/anti-patterns.md`. Summary:
|
|
|
120
131
|
- No `waitForTimeout(N)` — deprecated and flaky.
|
|
121
132
|
- No deep CSS chains (`div > div > div > span`) — break on every redesign.
|
|
122
133
|
- No random jitter as anti-bot defence — detection happens at fingerprint level before timing matters.
|
|
134
|
+
- No bot-challenge detection or vendor-string sniffing (e.g. `if (html.includes('px-captcha'))`) — false-throws on pages that actually loaded. Throw only on structural failures.
|
|
123
135
|
- No silent `.catch(() => {})` around data extraction.
|
|
124
136
|
|
|
125
137
|
## Interaction patterns (when the page needs them)
|
|
@@ -56,6 +56,12 @@ Source: [rebrowser.net/blog/xpath-vs-css-selectors-a-comprehensive-guide-for-web
|
|
|
56
56
|
|
|
57
57
|
---
|
|
58
58
|
|
|
59
|
+
## Don't detect bot-challenges in your script
|
|
60
|
+
|
|
61
|
+
Do NOT sniff for challenge markers to throw — e.g. `if (html.includes('px-captcha')) throw …` or checking for "Just a moment" / "Access Denied". Two reasons: (1) it **false-throws on pages that actually loaded** (the marker can be present in benign markup, or the challenge already cleared), killing good runs; (2) it isn't your job — the platform handles bot-detection and auto-escalates on empty results. **Throw only on structural failure** (required param missing, page genuinely never loaded). If the content selector times out, return what you have (often `[]`) and let the platform escalate.
|
|
62
|
+
|
|
63
|
+
---
|
|
64
|
+
|
|
59
65
|
## No silent catch on data writes
|
|
60
66
|
|
|
61
67
|
Never swallow extraction errors silently:
|
|
@@ -4,6 +4,29 @@
|
|
|
4
4
|
|
|
5
5
|
---
|
|
6
6
|
|
|
7
|
+
## Dismiss first-visit interstitials (before the content wait)
|
|
8
|
+
|
|
9
|
+
Consent (GDPR) modals and language/region splashes block the content `waitForSelector`. Dismiss FIRST, then wait for cards.
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
// click the consent/region button by text, then let the page settle
|
|
13
|
+
await page.evaluate(() => {
|
|
14
|
+
const rx = /^(accept all|allow all|agree|tout accepter|united states|continue)/i;
|
|
15
|
+
const b = [...document.querySelectorAll('button,[role="button"],a')]
|
|
16
|
+
.find(el => rx.test((el.textContent||'').trim()) && el.offsetParent);
|
|
17
|
+
if (b) b.click();
|
|
18
|
+
});
|
|
19
|
+
await page.waitForNetworkIdle({ idleTime: 800, timeout: 8000 }).catch(() => {});
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## waitUntil: 'load' for redirect-heavy pages
|
|
25
|
+
|
|
26
|
+
`domcontentloaded` can fire before a post-load redirect settles → `page.evaluate` throws "execution context destroyed". Use `waitUntil:'load'` when the URL changes after navigation (auth/locale redirects).
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
7
30
|
## Scroll to trigger lazy-load
|
|
8
31
|
|
|
9
32
|
Use `window.scrollBy` in a loop; wait for new content, stop when the item count stabilises.
|
|
@@ -38,7 +38,8 @@ The script body runs in a closure. The harness injects these globals, mirroring
|
|
|
38
38
|
|
|
39
39
|
- `browser` — the Puppeteer browser instance (headed, with DevTools open).
|
|
40
40
|
- `TRAWL` — object with `url` (from `--url`) and any `<custom>` keys from `--params`.
|
|
41
|
-
- `account` —
|
|
41
|
+
- `TRAWL.account` — sub-namespace with `username`, `password`, and optional `session.cookies` (from `--account-username` / `--account-password` / `--account-cookies`). `null` when no credentials are provided (mirrors prod worker).
|
|
42
|
+
- `account` — legacy alias for `TRAWL.account` (still works; both are injected).
|
|
42
43
|
- `returnData(arr)` — stub that prints the array as JSON to stdout.
|
|
43
44
|
- `saveSession(cookies)` — stub that prints the cookie array to stdout (does not persist).
|
|
44
45
|
|
|
@@ -48,9 +49,9 @@ No changes to the script body are needed to run it locally.
|
|
|
48
49
|
|
|
49
50
|
- `--url=<url>` — sets `TRAWL.url` (use a real value if your script navigates to it).
|
|
50
51
|
- `--params=k=v[,k=v]*` — sets `TRAWL.<custom>` keys (comma-separated `key=value` pairs).
|
|
51
|
-
- `--account-username=<u>` — sets `account.username
|
|
52
|
-
- `--account-password=<p>` — sets `account.password
|
|
53
|
-
- `--account-cookies=<path>` — loads `account.session.cookies` from a JSON array file.
|
|
52
|
+
- `--account-username=<u>` — sets `TRAWL.account.username` (alias: `account.username`).
|
|
53
|
+
- `--account-password=<p>` — sets `TRAWL.account.password` (alias: `account.password`).
|
|
54
|
+
- `--account-cookies=<path>` — loads `TRAWL.account.session.cookies` from a JSON array file (alias: `account.session.cookies`).
|
|
54
55
|
- `--remote` — connects to an existing Chrome at `http://127.0.0.1:9222` (instead of launching a fresh one). Useful for MFA flows — see `references/headed-debug.md`.
|
|
55
56
|
- `--headless` — launches Chrome headlessly (no window). Default is headed (`headless: false`) for interactive debugging.
|
|
56
57
|
- `--chrome=<path>` — path to a Chrome executable. Auto-detected on macOS and Linux when omitted; pass explicitly if Chrome lives in a non-standard location or you want to use Chromium.
|
|
@@ -62,11 +63,32 @@ No changes to the script body are needed to run it locally.
|
|
|
62
63
|
2. `trawl scraps update <id> -r "$(cat scrap.js)"` (see the `trawl-cli` skill for full CLI usage).
|
|
63
64
|
3. `trawl scraps run <id> --watch` to confirm the worker behaves the same.
|
|
64
65
|
|
|
66
|
+
## Headless vs headed-remote — which to trust
|
|
67
|
+
|
|
68
|
+
- `--headless` tests script wiring fast, but its automation fingerprint trips bot-walls that a real browser never sees → **false `len=0`**. Do not conclude "this site is walled" from a headless empty result.
|
|
69
|
+
- To validate **selectors + returnData logic** under a real fingerprint, run `--remote` against a manually-launched Chrome (no `--enable-automation` flag): `--remote-debugging-port=9222 --user-data-dir=/tmp/chrome-trawl`, then `run-local.mjs scrap.js --remote`. This is the truth signal for *script logic* (not the worker network surface).
|
|
70
|
+
|
|
71
|
+
## `len=0` is ambiguous — classify, don't assume "wall"
|
|
72
|
+
|
|
73
|
+
An empty result has 5 causes: real bot-wall · first-visit interstitial (consent/region) · wrong/stale selector on a page that renders fine (most common) · dead/wrong URL (soft-404 — `curl 200` ≠ content exists) · auth wall. **Capture the page title + a screenshot + visible body text and read it** before deciding — the title usually reveals which. `page.screenshot()` + `document.title` + `body.innerText.slice(0,600)`.
|
|
74
|
+
|
|
65
75
|
## Limitations (intentional)
|
|
66
76
|
|
|
67
77
|
- Local runs use the user's IP — no proxy tier, no fingerprint policy.
|
|
68
78
|
- Local pass does not guarantee worker pass — script logic is tested, not the network surface.
|
|
69
79
|
|
|
80
|
+
## Diagnosing prod failures
|
|
81
|
+
|
|
82
|
+
To diagnose a *prod* failure (vs debugging locally), use the CLI instead of re-running blind:
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
trawl scraps doctor <id> # last-run error + failed selector + block status + autofix summary
|
|
86
|
+
trawl scraps autofix <id> # full auto-fix detail: decision, diff, dry-run, knowledge consulted
|
|
87
|
+
trawl scraps snapshot <id> --error -o /tmp/err.html # download the error-path page HTML
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
These commands surface what the worker captured (same info as the web frontend's "Last run" panel) without exposing cost or proxy-vendor internals.
|
|
91
|
+
|
|
70
92
|
## What this skill does NOT cover
|
|
71
93
|
|
|
72
94
|
- Script body → `trawl-scrap-design`
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Build the script-VM context (TRAWL + account) from parsed CLI flags, mirroring
|
|
6
|
+
* the worker runtime contract (#786): credentials are exposed BOTH under the
|
|
7
|
+
* forward `TRAWL.account.*` sub-namespace AND the legacy bare `account` global
|
|
8
|
+
* (a retained alias). `readFile` is injected so the builder stays pure/testable.
|
|
9
|
+
* @param {Record<string, string|true>} flags - Parsed CLI flags.
|
|
10
|
+
* @param {(path: string) => string} [readFile] - File reader (injected in tests).
|
|
11
|
+
* @returns {{ TRAWL: object, account: object }}
|
|
12
|
+
*/
|
|
13
|
+
export function buildContext(flags, readFile = (p) => readFileSync(resolve(p), 'utf8')) {
|
|
14
|
+
// A flag passed without `=value` parses to boolean `true` (see run-local.mjs's
|
|
15
|
+
// parser). Coerce any non-string flag to null so a value-less flag can't leak a
|
|
16
|
+
// boolean into the context (e.g. `TRAWL.account.username === true`) or crash
|
|
17
|
+
// `readFile(true)`.
|
|
18
|
+
const str = (v) => (typeof v === 'string' ? v : null);
|
|
19
|
+
|
|
20
|
+
const TRAWL = { url: str(flags.url) };
|
|
21
|
+
if (typeof flags.params === 'string') {
|
|
22
|
+
for (const pair of flags.params.split(',')) {
|
|
23
|
+
const eqIdx = pair.indexOf('=');
|
|
24
|
+
if (eqIdx <= 0) continue;
|
|
25
|
+
TRAWL[pair.slice(0, eqIdx)] = pair.slice(eqIdx + 1);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const account = {
|
|
30
|
+
username: str(flags['account-username']),
|
|
31
|
+
password: str(flags['account-password']),
|
|
32
|
+
};
|
|
33
|
+
const cookiesPath = str(flags['account-cookies']);
|
|
34
|
+
if (cookiesPath) {
|
|
35
|
+
account.session = { cookies: JSON.parse(readFile(cookiesPath)) };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Parity with the worker (trawl_node#786): also expose creds under TRAWL.account.*.
|
|
39
|
+
// null when nothing was provided, mirroring buildTrawlAccount's null-when-empty contract.
|
|
40
|
+
const hasAccount = !!(account.username || account.password || account.session);
|
|
41
|
+
TRAWL.account = hasAccount ? account : null;
|
|
42
|
+
|
|
43
|
+
return { TRAWL, account };
|
|
44
|
+
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// Local Trawl scrap harness — mirrors the worker entry contract.
|
|
3
|
-
// Globals injected: browser, TRAWL, account, returnData, saveSession.
|
|
3
|
+
// Globals injected: browser, TRAWL (incl. TRAWL.account.*), account (legacy alias), returnData, saveSession.
|
|
4
4
|
import { readFileSync, existsSync } from 'node:fs';
|
|
5
5
|
import { resolve } from 'node:path';
|
|
6
|
+
import { buildContext } from './build-context.mjs';
|
|
6
7
|
|
|
7
8
|
const args = process.argv.slice(2);
|
|
8
9
|
|
|
@@ -12,9 +13,9 @@ function help() {
|
|
|
12
13
|
Flags:
|
|
13
14
|
--url=<url> TRAWL.url
|
|
14
15
|
--params=k=v[,k=v]* TRAWL.<custom> keys
|
|
15
|
-
--account-username=<user> account.username
|
|
16
|
-
--account-password=<pwd> account.password
|
|
17
|
-
--account-cookies=<path> account.session.cookies (JSON array)
|
|
16
|
+
--account-username=<user> TRAWL.account.username (alias: account.username)
|
|
17
|
+
--account-password=<pwd> TRAWL.account.password (alias: account.password)
|
|
18
|
+
--account-cookies=<path> TRAWL.account.session.cookies (alias: account.session.cookies; JSON array)
|
|
18
19
|
--remote connect to existing Chrome at 127.0.0.1:9222
|
|
19
20
|
--headless launch Chrome in headless mode (no window)
|
|
20
21
|
--chrome=<path> path to Chrome executable (auto-detected if omitted)
|
|
@@ -46,22 +47,7 @@ if (!scrapPath) {
|
|
|
46
47
|
|
|
47
48
|
const scrapBody = readFileSync(resolve(scrapPath), 'utf8');
|
|
48
49
|
|
|
49
|
-
const TRAWL
|
|
50
|
-
if (typeof flags.params === 'string') {
|
|
51
|
-
for (const pair of flags.params.split(',')) {
|
|
52
|
-
const eqIdx = pair.indexOf('=');
|
|
53
|
-
if (eqIdx <= 0) continue;
|
|
54
|
-
const k = pair.slice(0, eqIdx);
|
|
55
|
-
const v = pair.slice(eqIdx + 1);
|
|
56
|
-
TRAWL[k] = v;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const account = { username: flags['account-username'] || null, password: flags['account-password'] || null };
|
|
61
|
-
if (flags['account-cookies']) {
|
|
62
|
-
const cookies = JSON.parse(readFileSync(resolve(flags['account-cookies']), 'utf8'));
|
|
63
|
-
account.session = { cookies };
|
|
64
|
-
}
|
|
50
|
+
const { TRAWL, account } = buildContext(flags);
|
|
65
51
|
|
|
66
52
|
let puppeteer;
|
|
67
53
|
try {
|