@trawlme/skills 1.4.12 → 1.4.13

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trawlme/skills",
3
- "version": "1.4.12",
3
+ "version": "1.4.13",
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": {
@@ -172,6 +172,10 @@ Full detail in `references/anti-patterns.md`. Summary:
172
172
 
173
173
  Some pages hide data behind real UI interactions — scroll, hover, click, form fill. These are about **functional correctness**, not bot evasion. See `references/interaction-patterns.md` for patterns: lazy-load scroll, hover-to-reveal, click-to-expand, `page.type` form fill, state-transition waits, "next" pagination.
174
174
 
175
+ ## Script helpers (`fetch`, `cursor`, `captcha`)
176
+
177
+ Besides `page` and `returnData`, a script on the worker can call `fetch` (a request from the script, outside the page: no page cookies), `cursor` (mouse moves along a path, for hover menus and controls that need a real pointer) and `captcha` (a token for a captcha inside a form the script submits). See `references/script-helpers.md` for each helper's arguments, return shape and an example, and the full list of globals a script can use.
178
+
175
179
  ## Canonical Trawl docs
176
180
 
177
181
  - *Trawl Scraping docs (in-app help center)* — selector basics, `returnData`, scrap lifecycle.
@@ -0,0 +1,122 @@
1
+ # Script Helpers — Trawl Scrap Scripts
2
+
3
+ > *Besides `page`, `browser`, `TRAWL` and `returnData`, a script on the worker can call four helpers: `fetch`, `cursor`, `captcha` and `trawl.waitForHydration`. This page lists what each one takes and returns. They exist on the worker only: the local harness (`trawl-scrap-local-test`) does not inject `cursor`, `captcha` or `trawl`, so a script that uses them fails locally with a `ReferenceError`.*
4
+
5
+ The script runs in a sandbox, not in Node. Only these globals exist at script level: `page`, `browser`, `cursor`, `captcha`, `TRAWL`, `account`, `trawl`, `saveSession`, `returnData`, `fetch`, `console`, the timers (`setTimeout`, `setInterval`, `clearTimeout`, `clearInterval`), `JSON`, `Math`, `Date`, `Array`, `Object`, `String`, `Number`, `Boolean`, `RegExp`, `Map`, `Set`, `Promise`, `Error`, `URL`, `URLSearchParams`, `Buffer`. Anything else (`AbortSignal`, `AbortController`, `Headers`, `FormData`, `TextDecoder`, `require`, `process`) is undefined at script level; it is still there inside `page.evaluate`, which runs in the browser.
6
+
7
+ ---
8
+
9
+ ## `fetch` — a request from the script, not from the page
10
+
11
+ `fetch(url, options?)` → `Promise<Response>`. The standard `fetch`: same arguments, same `Response` (`res.ok`, `res.status`, `await res.json()`, `await res.text()`).
12
+
13
+ It runs on the worker, **outside the page**: it does not carry the page's cookies, session or headers, and does not go the way the page's own requests go. So:
14
+
15
+ - **For the target site, keep using `fetch` inside `page.evaluate`** (see "Run budget" in `SKILL.md`): same origin, same cookies, same session as the page.
16
+ - **Use the script-level `fetch` for a public endpoint that needs no page**: a sitemap, a public JSON feed, a file you then parse in the script.
17
+
18
+ `AbortSignal` does not exist at script level, so bound the wait with `Promise.race`:
19
+
20
+ ```js
21
+ // one request, at most 20 s — the request itself is not cancelled, the script just stops waiting
22
+ const withTimeout = (p, ms) => Promise.race([
23
+ p,
24
+ new Promise((_, reject) => setTimeout(() => reject(new Error(`timeout after ${ms} ms`)), ms)),
25
+ ]);
26
+
27
+ let urls = [];
28
+ try {
29
+ // bound the headers AND the body read: a server can send headers, then stall
30
+ const xml = await withTimeout(
31
+ fetch('https://example.com/sitemap.xml').then((res) => (res.ok ? res.text() : '')),
32
+ 20000,
33
+ );
34
+ urls = [...xml.matchAll(/<loc>([^<]+)<\/loc>/g)].map((m) => m[1]);
35
+ } catch (err) {
36
+ console.error('sitemap fetch failed:', err.message);
37
+ }
38
+ ```
39
+
40
+ ---
41
+
42
+ ## `cursor` — a mouse that moves along a path
43
+
44
+ `cursor` drives the mouse of the main `page` along a curved path instead of jumping to the target. Use it for pages that react to real pointer movement: a menu that opens on hover, a control that ignores `element.click()`. It is bound to the main `page` only; a tab you open with `browser.newPage()` has no cursor. `page.click()` and `page.hover()` do not use it.
45
+
46
+ Every method returns `Promise<void>`. `target` is a CSS selector, an XPath, or an element handle.
47
+
48
+ | Method | What it does |
49
+ |---|---|
50
+ | `cursor.click(target?, options?)` | Moves to `target` (scrolling it into view), then clicks. No `target` = click where the mouse is. Options: `button` (`'left'`), `clickCount` (`1`), `hesitate` (ms before the click), `waitForClick` (ms between press and release), `moveDelay` (pause after the move, random from 0 up to this, default 2000 ms). |
51
+ | `cursor.move(target, options?)` | Moves to `target` (hover), scrolling it into view first. Options: `moveDelay`, `moveSpeed`, `paddingPercentage` (how far inside the element to aim). |
52
+ | `cursor.moveTo({ x, y }, options?)` | Moves to a point in the viewport. |
53
+ | `cursor.scrollIntoView(target, options?)` | Scrolls until `target` is visible; no scroll if it already is. |
54
+ | `cursor.scroll({ x?, y? }, options?)` | Scrolls by that many pixels. |
55
+ | `cursor.getLocation()` | Returns the current `{ x, y }` (not a promise). |
56
+
57
+ ```js
58
+ // open a hover menu, then read the links it reveals
59
+ const MENU_SEL = 'nav [aria-haspopup="true"]'; // the menu trigger
60
+ const ITEM_SEL = 'nav [role="menu"] a'; // links shown once open
61
+ await page.waitForSelector(MENU_SEL);
62
+ await cursor.move(MENU_SEL);
63
+ await page.waitForSelector(ITEM_SEL, { visible: true });
64
+ const links = await page.$$eval(ITEM_SEL, (els) => els.map((a) => a.href));
65
+ ```
66
+
67
+ ---
68
+
69
+ ## `trawl.waitForHydration` — wait until the page has finished loading its data
70
+
71
+ Waits on the last page the script navigated. Use it after `page.goto` on a site that renders its content with JavaScript, before you read the DOM.
72
+
73
+ | Option | Default | Meaning |
74
+ |---|---|---|
75
+ | `strategy` | `'networkIdle'` | `'networkIdle'`: no network request for 500 ms. `'selector'`: wait for `selector` to appear. |
76
+ | `selector` | — | Required with `strategy: 'selector'` (throws without it). |
77
+ | `timeout` | `30000` | Milliseconds before it throws. |
78
+
79
+ Returns nothing; throws on timeout.
80
+
81
+ ```js
82
+ await page.goto(TRAWL.url, { waitUntil: 'domcontentloaded' });
83
+ await trawl.waitForHydration({ strategy: 'selector', selector: '.product-card', timeout: 45000 });
84
+ const items = await page.$$eval('.product-card', (cards) => cards.map((c) => ({ title: c.textContent.trim() })));
85
+ return returnData(items);
86
+ ```
87
+
88
+ ## `captcha` — get a token for a captcha the script must submit
89
+
90
+ The worker already handles the common challenge pages by itself, after your script runs (see `anti-patterns.md`: never detect or fight them in your script). `captcha` is for one case only: a captcha that is **part of a form the script fills and submits** (a search or lookup form that asks for a token before it answers).
91
+
92
+ Each solver takes the widget's site key and the page URL and returns `Promise<string>`, the token. It **throws** when solving is not available on the worker, fails, or takes too long. A solve can take from seconds to a few minutes, and that time counts against the run budget (`SKILL.md`, "Run budget"): always `try`/`catch` it and keep the deadline guard.
93
+
94
+ | Helper | Arguments | Returns |
95
+ |---|---|---|
96
+ | `captcha.solveRecaptchaV2(siteKey, pageUrl)` | site key (`data-sitekey` on the widget), page URL | `Promise<string>` token |
97
+ | `captcha.solveRecaptchaV3(siteKey, pageUrl, action?, minScore?)` | site key, page URL, the page's action name (default `'verify'`), minimum score (default `0.7`) | `Promise<string>` token |
98
+ | `captcha.solveHcaptcha(siteKey, pageUrl)` | site key, page URL | `Promise<string>` token |
99
+ | `captcha.solveTurnstile(siteKey, pageUrl)` | site key, page URL | `Promise<string>` token |
100
+ | `captcha.injectRecaptchaSolution(page, token)` | the page, a reCAPTCHA token | `Promise<void>`: writes the token into `#g-recaptcha-response` and calls the widget's callbacks |
101
+
102
+ Only reCAPTCHA has an inject helper. For the others, write the token into the widget's response field yourself (`[name="h-captcha-response"]` for hCaptcha, `[name="cf-turnstile-response"]` for Turnstile), then submit the form.
103
+
104
+ ```js
105
+ // a lookup form protected by a reCAPTCHA v2 checkbox
106
+ const FORM_SEL = 'form#lookup';
107
+ const WIDGET_SEL = '.g-recaptcha[data-sitekey]';
108
+ await page.type(`${FORM_SEL} input[name="q"]`, TRAWL.query);
109
+ try {
110
+ const siteKey = await page.$eval(WIDGET_SEL, (el) => el.getAttribute('data-sitekey'));
111
+ const token = await captcha.solveRecaptchaV2(siteKey, page.url());
112
+ await captcha.injectRecaptchaSolution(page, token);
113
+ } catch (err) {
114
+ console.error('captcha step failed:', err.message);
115
+ returnData([]); // nothing to read without the form's answer
116
+ return;
117
+ }
118
+ await Promise.all([
119
+ page.waitForNavigation({ waitUntil: 'domcontentloaded' }),
120
+ page.click(`${FORM_SEL} [type="submit"]`),
121
+ ]);
122
+ ```