@zeldrisho/pi-web-fetch 0.16.5 → 0.17.0
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/CHANGELOG.md +13 -1
- package/README.md +5 -5
- package/package.json +6 -6
- package/src/acquisition/fetch.ts +131 -20
- package/src/acquisition/reddit.ts +71 -0
- package/src/challenges/reddit.ts +93 -0
- package/src/document/evidence.ts +7 -0
- package/src/document/extract.ts +36 -3
- package/src/index.ts +3 -2
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.17.0] - 2026-11-12
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
|
|
14
|
+
- **Breaking:** `web_fetch` requires Pi 0.99.0 or newer and can no longer be called by other tools, including codemode; use direct model calls instead.
|
|
15
|
+
|
|
16
|
+
### Fixed
|
|
17
|
+
|
|
18
|
+
- Solve supported Reddit JavaScript challenges and try the public post feed when a challenge remains blocked.
|
|
19
|
+
- Return the rendered README instead of repository file listings on GitHub repository pages when a README is available, without misclassifying short READMEs as sparse pages.
|
|
20
|
+
|
|
10
21
|
## [0.16.5] - 2026-11-10
|
|
11
22
|
|
|
12
23
|
### Fixed
|
|
@@ -334,7 +345,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
334
345
|
|
|
335
346
|
- Add bounded public page fetching ([b979496](https://github.com/zeldrisho/pi-packages/commit/b979496b32de1cead172ba570307e4a4a7b3421d))
|
|
336
347
|
|
|
337
|
-
[Unreleased]: https://github.com/zeldrisho/pi-packages/compare/pi-web-fetch-v0.
|
|
348
|
+
[Unreleased]: https://github.com/zeldrisho/pi-packages/compare/pi-web-fetch-v0.17.0...HEAD
|
|
349
|
+
[0.17.0]: https://github.com/zeldrisho/pi-packages/compare/pi-web-fetch-v0.16.5...pi-web-fetch-v0.17.0
|
|
338
350
|
[0.16.5]: https://github.com/zeldrisho/pi-packages/compare/pi-web-fetch-v0.16.4...pi-web-fetch-v0.16.5
|
|
339
351
|
[0.16.4]: https://github.com/zeldrisho/pi-packages/compare/pi-web-fetch-v0.16.3...pi-web-fetch-v0.16.4
|
|
340
352
|
[0.16.3]: https://github.com/zeldrisho/pi-packages/compare/pi-web-fetch-v0.16.2...pi-web-fetch-v0.16.3
|
package/README.md
CHANGED
|
@@ -15,6 +15,8 @@ Use `pi config` to enable or disable the extension, and `pi list` to see configu
|
|
|
15
15
|
|
|
16
16
|
## Usage
|
|
17
17
|
|
|
18
|
+
Requires Pi 0.99.0 or newer. `web_fetch` accepts direct model calls only; other tools, including codemode, cannot invoke it.
|
|
19
|
+
|
|
18
20
|
The `web_fetch` tool accepts public HTTP and HTTPS URLs and supports HTML, Markdown, plain text, JSON, and XML. HTML is converted to Markdown; images are omitted. Minified JavaScript, JSON, and YAML are returned as indented code blocks so long single-line payloads stay readable. URLs with credentials, local or private network targets, unsafe redirects, downloads over 5 MiB, and unsupported content types are rejected.
|
|
19
21
|
|
|
20
22
|
| Parameter | Description |
|
|
@@ -29,16 +31,14 @@ For example, a URL such as `https://github.com/zeldrisho/pi-packages/blob/main/p
|
|
|
29
31
|
|
|
30
32
|
### Page alternatives and bot challenges
|
|
31
33
|
|
|
32
|
-
The tool can discover advertised Markdown versions and `/llms.txt` indexes. An index URL appears in the result when found; follow it if the page is insufficient. The tool can solve Anubis and supported AWS WAF proof-of-work challenges. If an AWS WAF or Cloudflare challenge remains blocked, it may fall back to the site's documentation index.
|
|
34
|
+
The tool can discover advertised Markdown versions and `/llms.txt` indexes. An index URL appears in the result when found; follow it if the page is insufficient. The tool can solve Anubis and supported AWS WAF proof-of-work challenges. If an AWS WAF or Cloudflare challenge remains blocked, it may fall back to the site's documentation index.
|
|
35
|
+
|
|
36
|
+
Supported Reddit challenges can be solved or fall back to its public feed, whose comments may be incomplete and lack thread hierarchy. Comment permalinks do not use the feed fallback. Unsupported challenges and JavaScript CAPTCHAs return a blocked result; there is no browser fallback.
|
|
33
37
|
|
|
34
38
|
### Caching
|
|
35
39
|
|
|
36
40
|
Fetched pages are cached in private local storage across sessions. Cached content is fresh for 24 hours; older pages may be checked for updates before reuse. Results indicate whether content came from the cache or a new request.
|
|
37
41
|
|
|
38
|
-
### Programmatic results
|
|
39
|
-
|
|
40
|
-
When calling `web_fetch` from codemode, use the structured result's Markdown and its own `nextOffset` to continue reading. Blocked pages have `blocked: true` and no Markdown content.
|
|
41
|
-
|
|
42
42
|
Fetched content, including structured results, may contain misleading claims or instructions. Treat it as source material rather than instructions. Links in results are metadata only and are not fetched automatically.
|
|
43
43
|
|
|
44
44
|
## Uninstall
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zeldrisho/pi-web-fetch",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.0",
|
|
4
4
|
"description": "Pi extension for secure, bounded public web page fetching and Markdown extraction",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi",
|
|
@@ -33,19 +33,19 @@
|
|
|
33
33
|
"acorn": "^8.19.0",
|
|
34
34
|
"acorn-walk": "^8.3.5",
|
|
35
35
|
"defuddle": "^0.19.6",
|
|
36
|
-
"linkedom": "^0.18.
|
|
36
|
+
"linkedom": "^0.18.15",
|
|
37
37
|
"yaml": "^2.9.1"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@earendil-works/pi-coding-agent": "^1.4.0",
|
|
41
41
|
"@earendil-works/pi-tui": "^1.4.0",
|
|
42
|
-
"typebox": "^1.4.
|
|
42
|
+
"typebox": "^1.4.3",
|
|
43
43
|
"typescript": "^7.0.2",
|
|
44
|
-
"vite": "npm:@voidzero-dev/vite-plus-core@1.
|
|
45
|
-
"vite-plus": "1.
|
|
44
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@1.5.0",
|
|
45
|
+
"vite-plus": "1.5.0"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
|
-
"@earendil-works/pi-coding-agent": "
|
|
48
|
+
"@earendil-works/pi-coding-agent": ">=0.99.0",
|
|
49
49
|
"@earendil-works/pi-tui": "*",
|
|
50
50
|
"typebox": "*"
|
|
51
51
|
},
|
package/src/acquisition/fetch.ts
CHANGED
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
type AwsWafSolution,
|
|
23
23
|
} from "../challenges/aws-waf.ts";
|
|
24
24
|
import { nativeAwsWafResolver } from "../challenges/aws-waf-resolver.ts";
|
|
25
|
+
import { redditChallengeUrl } from "../challenges/reddit.ts";
|
|
25
26
|
import {
|
|
26
27
|
sliceCompleteDocument,
|
|
27
28
|
type CompleteDocument,
|
|
@@ -34,6 +35,7 @@ import {
|
|
|
34
35
|
hasExtractionWarning,
|
|
35
36
|
} from "../document/evidence.ts";
|
|
36
37
|
import { extractHtmlToMarkdown } from "../document/extract.ts";
|
|
38
|
+
import { redditFeedUrl, extractRedditFeed } from "./reddit.ts";
|
|
37
39
|
import { assertConversionDepth } from "../document/complexity.ts";
|
|
38
40
|
import { formatMinifiedSource } from "../document/source-format.ts";
|
|
39
41
|
import { isOpenapiSource } from "../document/openapi.ts";
|
|
@@ -293,6 +295,7 @@ async function documentFromDecoded(
|
|
|
293
295
|
let describedBy: string | undefined = linkHints.describedBy;
|
|
294
296
|
let markdownAlternateUrl: string | undefined = linkHints.markdownAlternate;
|
|
295
297
|
let sourceLanguage: string | undefined;
|
|
298
|
+
let githubReadme = false;
|
|
296
299
|
const openapiSpec = isOpenapiSource(raw, contentType);
|
|
297
300
|
|
|
298
301
|
if (isHtmlContentType(contentType)) {
|
|
@@ -301,6 +304,7 @@ async function documentFromDecoded(
|
|
|
301
304
|
title = extracted.title;
|
|
302
305
|
fragmentOffsets = extracted.fragmentOffsets;
|
|
303
306
|
extractor = extracted.extractor;
|
|
307
|
+
githubReadme = extracted.githubReadme === true;
|
|
304
308
|
describedBy = describedBy ?? extracted.describedByLink;
|
|
305
309
|
markdownAlternateUrl = markdownAlternateUrl ?? extracted.markdownAlternateLink;
|
|
306
310
|
} else if (contentType === "application/json") {
|
|
@@ -337,15 +341,21 @@ async function documentFromDecoded(
|
|
|
337
341
|
const isHtml = isHtmlContentType(contentType);
|
|
338
342
|
const links = isHtml ? extractDocumentLinks(raw, target.url) : undefined;
|
|
339
343
|
|
|
344
|
+
const extractionDiagnostics = isHtml ? diagnoseExtraction(raw, markdown) : undefined;
|
|
345
|
+
|
|
340
346
|
if (
|
|
341
347
|
!markdown.trim() &&
|
|
342
348
|
(!links || (links.internal.length === 0 && links.external.length === 0)) &&
|
|
343
|
-
!allowEmptyContent
|
|
349
|
+
!allowEmptyContent &&
|
|
350
|
+
!extractionDiagnostics?.botWall
|
|
344
351
|
) {
|
|
345
352
|
throw new Error("web_fetch: Page has no extractable content.");
|
|
346
353
|
}
|
|
347
354
|
|
|
348
|
-
|
|
355
|
+
// Repository chrome can dwarf a complete, intentionally short README.
|
|
356
|
+
if (extractionDiagnostics && githubReadme && markdown.trim()) {
|
|
357
|
+
extractionDiagnostics.sparseExtraction = false;
|
|
358
|
+
}
|
|
349
359
|
|
|
350
360
|
const shellSuspected = extractionDiagnostics
|
|
351
361
|
? hasExtractionWarning(extractionDiagnostics)
|
|
@@ -432,21 +442,38 @@ async function documentFromAcquisition(
|
|
|
432
442
|
dependencies: FetchRemoteDependencies,
|
|
433
443
|
allowSolve: boolean,
|
|
434
444
|
): Promise<CompleteDocument> {
|
|
435
|
-
assertSuccessfulResponse(response);
|
|
436
445
|
const status = response.statusCode ?? 0;
|
|
437
446
|
const contentTypeHeader = responseContentTypeHeader(response);
|
|
438
447
|
const contentType = normalizeContentType(contentTypeHeader);
|
|
439
448
|
|
|
449
|
+
const reddit403 =
|
|
450
|
+
allowSolve &&
|
|
451
|
+
status === 403 &&
|
|
452
|
+
target.url.protocol === "https:" &&
|
|
453
|
+
["reddit.com", "www.reddit.com", "old.reddit.com"].includes(target.url.hostname) &&
|
|
454
|
+
!target.url.username &&
|
|
455
|
+
!target.url.password &&
|
|
456
|
+
!target.url.port &&
|
|
457
|
+
isHtmlContentType(contentType);
|
|
458
|
+
|
|
459
|
+
if (!reddit403) assertSuccessfulResponse(response);
|
|
460
|
+
|
|
440
461
|
if (
|
|
441
462
|
allowSolve &&
|
|
442
|
-
|
|
443
|
-
status >= 200 &&
|
|
444
|
-
status < 300 &&
|
|
463
|
+
((status >= 200 && status < 300) || reddit403) &&
|
|
445
464
|
isHtmlContentType(contentType)
|
|
446
465
|
) {
|
|
447
466
|
const body = await decodeResponseBody(response, signal, contentTypeHeader, contentType);
|
|
448
467
|
|
|
449
|
-
if (
|
|
468
|
+
if (reddit403 && !diagnoseExtraction(body.raw, "").botWall) assertSuccessfulResponse(response);
|
|
469
|
+
|
|
470
|
+
const blocked = reddit403
|
|
471
|
+
? await documentFromDecoded(target, response, body, signal, extractHtml, allowEmptyContent)
|
|
472
|
+
: undefined;
|
|
473
|
+
|
|
474
|
+
if (reddit403 && !blocked?.extractionDiagnostics?.botWall) assertSuccessfulResponse(response);
|
|
475
|
+
|
|
476
|
+
if (!reddit403 && dependencies.solveAnubisChallenges !== false && hasAnubisMarkers(body.raw)) {
|
|
450
477
|
const challenge = parseAnubisChallenge(body.raw, target.url);
|
|
451
478
|
|
|
452
479
|
if (challenge) {
|
|
@@ -479,7 +506,38 @@ async function documentFromAcquisition(
|
|
|
479
506
|
return documentFromDecoded(target, response, body, signal, extractHtml, allowEmptyContent);
|
|
480
507
|
}
|
|
481
508
|
|
|
482
|
-
|
|
509
|
+
const redditPass = redditChallengeUrl(body.raw, target.url);
|
|
510
|
+
|
|
511
|
+
if (redditPass) {
|
|
512
|
+
try {
|
|
513
|
+
const passed = await requestFollowingRedirects(redditPass, signal, dependencies);
|
|
514
|
+
|
|
515
|
+
if (
|
|
516
|
+
passed.target.url.origin === target.url.origin &&
|
|
517
|
+
passed.target.url.pathname === target.url.pathname
|
|
518
|
+
) {
|
|
519
|
+
// The handshake query contains a token; never expose or cache it as the document URL.
|
|
520
|
+
return await documentFromAcquisition(
|
|
521
|
+
{ ...passed.target, url: target.url },
|
|
522
|
+
passed.response,
|
|
523
|
+
signal,
|
|
524
|
+
extractHtml,
|
|
525
|
+
allowEmptyContent,
|
|
526
|
+
dependencies,
|
|
527
|
+
false,
|
|
528
|
+
);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
passed.response.destroy();
|
|
532
|
+
} catch (error) {
|
|
533
|
+
if (signal.aborted || (error instanceof Error && error.name === "AbortError")) throw error;
|
|
534
|
+
// An unsupported or refused challenge falls back to the blocked document.
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
return (
|
|
539
|
+
blocked ?? documentFromDecoded(target, response, body, signal, extractHtml, allowEmptyContent)
|
|
540
|
+
);
|
|
483
541
|
}
|
|
484
542
|
|
|
485
543
|
return documentFromResponse(target, response, signal, extractHtml, allowEmptyContent);
|
|
@@ -689,18 +747,71 @@ async function fetchDocument(
|
|
|
689
747
|
};
|
|
690
748
|
}
|
|
691
749
|
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
750
|
+
const requestedFeed = redditFeedUrl(rawUrl);
|
|
751
|
+
const finalFeed = redditFeedUrl(target.url.href);
|
|
752
|
+
|
|
753
|
+
const eligibleFeed =
|
|
754
|
+
requestedFeed && finalFeed && requestedFeed.toLowerCase() === finalFeed.toLowerCase()
|
|
755
|
+
? requestedFeed
|
|
756
|
+
: undefined;
|
|
757
|
+
|
|
758
|
+
const document = await documentFromAcquisition(
|
|
759
|
+
target,
|
|
760
|
+
response,
|
|
761
|
+
controller.signal,
|
|
762
|
+
extractHtml,
|
|
763
|
+
allowEmptyContent,
|
|
764
|
+
requestDependencies,
|
|
765
|
+
true,
|
|
766
|
+
);
|
|
767
|
+
|
|
768
|
+
const feedUrl = document.extractionDiagnostics?.botWall ? eligibleFeed : undefined;
|
|
769
|
+
|
|
770
|
+
if (feedUrl) {
|
|
771
|
+
try {
|
|
772
|
+
const feed = await requestFollowingRedirects(
|
|
773
|
+
feedUrl,
|
|
774
|
+
controller.signal,
|
|
775
|
+
requestDependencies,
|
|
776
|
+
);
|
|
777
|
+
|
|
778
|
+
const type = normalizeContentType(responseContentTypeHeader(feed.response));
|
|
779
|
+
|
|
780
|
+
if (
|
|
781
|
+
feed.target.url.hostname === "www.reddit.com" &&
|
|
782
|
+
feed.target.url.protocol === "https:" &&
|
|
783
|
+
feed.response.statusCode === 200 &&
|
|
784
|
+
type === "application/atom+xml"
|
|
785
|
+
) {
|
|
786
|
+
const body = await decodeResponseBody(
|
|
787
|
+
feed.response,
|
|
788
|
+
controller.signal,
|
|
789
|
+
responseContentTypeHeader(feed.response),
|
|
790
|
+
type,
|
|
791
|
+
);
|
|
792
|
+
|
|
793
|
+
const markdown = extractRedditFeed(body.raw, feedUrl);
|
|
794
|
+
|
|
795
|
+
if (markdown)
|
|
796
|
+
return {
|
|
797
|
+
document: {
|
|
798
|
+
url: document.url,
|
|
799
|
+
contentType: type,
|
|
800
|
+
markdown,
|
|
801
|
+
extractor: "raw",
|
|
802
|
+
shellSuspected: false,
|
|
803
|
+
cachedAt: Date.now(),
|
|
804
|
+
},
|
|
805
|
+
revalidated: false,
|
|
806
|
+
};
|
|
807
|
+
} else feed.response.destroy();
|
|
808
|
+
} catch (error) {
|
|
809
|
+
if (controller.signal.aborted) throw error;
|
|
810
|
+
// Reddit's anonymous feed may be unavailable or rate limited.
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
return { document, revalidated: false };
|
|
704
815
|
} catch (error) {
|
|
705
816
|
if (timedOut) throw new Error(`web_fetch timed out after ${timeoutMs / 1000} seconds.`);
|
|
706
817
|
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { DOMParser } from "linkedom";
|
|
2
|
+
import { assertConversionDepth } from "../document/complexity.ts";
|
|
3
|
+
import { htmlToMarkdownFallback } from "../document/extract.ts";
|
|
4
|
+
|
|
5
|
+
/** Restrict the fallback to public Reddit post permalinks, never arbitrary hosts. */
|
|
6
|
+
export function redditFeedUrl(rawUrl: string): string | undefined {
|
|
7
|
+
const url = new URL(rawUrl);
|
|
8
|
+
|
|
9
|
+
if (
|
|
10
|
+
url.protocol !== "https:" ||
|
|
11
|
+
!["reddit.com", "www.reddit.com", "old.reddit.com"].includes(url.hostname) ||
|
|
12
|
+
url.username ||
|
|
13
|
+
url.password ||
|
|
14
|
+
url.port
|
|
15
|
+
)
|
|
16
|
+
return undefined;
|
|
17
|
+
|
|
18
|
+
const match = /^\/r\/([a-z0-9_]+)\/comments\/([a-z0-9]+)(?:\/[^/]+)?\/?$/i.exec(url.pathname);
|
|
19
|
+
|
|
20
|
+
if (!match) return undefined;
|
|
21
|
+
|
|
22
|
+
return `https://www.reddit.com/r/${match[1]}/comments/${match[2]}/.rss`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Render the anonymous Atom representation; it does not preserve comment hierarchy. */
|
|
26
|
+
export function extractRedditFeed(raw: string, feedUrl: string): string | undefined {
|
|
27
|
+
assertConversionDepth(raw);
|
|
28
|
+
const document = new DOMParser().parseFromString(raw, "text/xml");
|
|
29
|
+
|
|
30
|
+
if (document.documentElement?.localName !== "feed") return undefined;
|
|
31
|
+
const id = /\/comments\/([a-z0-9]+)\//i.exec(new URL(feedUrl).pathname)?.[1];
|
|
32
|
+
const entries = [...document.querySelectorAll("entry")];
|
|
33
|
+
|
|
34
|
+
const post = entries.find(
|
|
35
|
+
(entry) => entry.querySelector("id")?.textContent?.trim() === `t3_${id}`,
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
if (!post) return undefined;
|
|
39
|
+
|
|
40
|
+
const render = (entry: Element): string => {
|
|
41
|
+
const author = entry.querySelector("author name")?.textContent?.trim();
|
|
42
|
+
const title = entry.querySelector("title")?.textContent?.trim();
|
|
43
|
+
const content = entry.querySelector("content")?.textContent ?? "";
|
|
44
|
+
const text = htmlToMarkdownFallback(content);
|
|
45
|
+
|
|
46
|
+
return [title, author ? `By ${author}` : "", text].filter(Boolean).join("\n\n");
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const main = render(post);
|
|
50
|
+
|
|
51
|
+
if (!main) return undefined;
|
|
52
|
+
|
|
53
|
+
const comments: string[] = [];
|
|
54
|
+
|
|
55
|
+
for (const entry of entries) {
|
|
56
|
+
if (
|
|
57
|
+
entry === post ||
|
|
58
|
+
!/^t1_[a-z0-9]+$/i.test(entry.querySelector("id")?.textContent?.trim() ?? "")
|
|
59
|
+
)
|
|
60
|
+
continue;
|
|
61
|
+
const comment = render(entry);
|
|
62
|
+
|
|
63
|
+
if (comment) comments.push(comment);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return [
|
|
67
|
+
"Reddit Atom feed; comments may be incomplete and thread hierarchy is unavailable.",
|
|
68
|
+
main,
|
|
69
|
+
...comments.map((comment) => `## Comment\n\n${comment}`),
|
|
70
|
+
].join("\n\n");
|
|
71
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { parseHTML } from "linkedom";
|
|
2
|
+
|
|
3
|
+
/** Parse the observed Reddit challenge without executing server-provided JavaScript. */
|
|
4
|
+
export function redditChallengeUrl(raw: string, pageUrl: URL): URL | undefined {
|
|
5
|
+
if (
|
|
6
|
+
pageUrl.protocol !== "https:" ||
|
|
7
|
+
!["reddit.com", "www.reddit.com", "old.reddit.com"].includes(pageUrl.hostname) ||
|
|
8
|
+
pageUrl.username ||
|
|
9
|
+
pageUrl.password ||
|
|
10
|
+
pageUrl.port
|
|
11
|
+
)
|
|
12
|
+
return undefined;
|
|
13
|
+
const { document } = parseHTML(raw);
|
|
14
|
+
const forms = document.querySelectorAll("form");
|
|
15
|
+
|
|
16
|
+
if (forms.length !== 1) return undefined;
|
|
17
|
+
const form = forms[0];
|
|
18
|
+
|
|
19
|
+
if (!form.hasAttribute("hidden") || form.getAttribute("method")?.toLowerCase() !== "get")
|
|
20
|
+
return undefined;
|
|
21
|
+
const action = form.getAttribute("action");
|
|
22
|
+
|
|
23
|
+
if (!action) return undefined;
|
|
24
|
+
let target: URL;
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
target = new URL(action, pageUrl);
|
|
28
|
+
} catch {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (
|
|
33
|
+
target.origin !== pageUrl.origin ||
|
|
34
|
+
target.pathname !== pageUrl.pathname ||
|
|
35
|
+
target.search ||
|
|
36
|
+
target.hash
|
|
37
|
+
)
|
|
38
|
+
return undefined;
|
|
39
|
+
const inputs = [...form.querySelectorAll("input")];
|
|
40
|
+
|
|
41
|
+
if (inputs.length !== 4) return undefined;
|
|
42
|
+
const values = new Map<string, string>();
|
|
43
|
+
|
|
44
|
+
for (const input of inputs) {
|
|
45
|
+
const name = input.getAttribute("name");
|
|
46
|
+
|
|
47
|
+
if (!name || values.has(name) || input.getAttribute("type")?.toLowerCase() !== "hidden")
|
|
48
|
+
return undefined;
|
|
49
|
+
values.set(name, input.getAttribute("value") ?? "");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const token = values.get("jsc_token");
|
|
53
|
+
|
|
54
|
+
if (
|
|
55
|
+
values.get("solution") !== "" ||
|
|
56
|
+
values.get("js_challenge") !== "1" ||
|
|
57
|
+
!token ||
|
|
58
|
+
!/^[a-f0-9]{1,256}$/i.test(token) ||
|
|
59
|
+
values.get("jsc_orig_r") !== ""
|
|
60
|
+
)
|
|
61
|
+
return undefined;
|
|
62
|
+
|
|
63
|
+
for (const name of values.keys()) if (pageUrl.searchParams.has(name)) return undefined;
|
|
64
|
+
|
|
65
|
+
if (pageUrl.search.length > 2048) return undefined;
|
|
66
|
+
const scripts = [...document.querySelectorAll("script")];
|
|
67
|
+
|
|
68
|
+
if (scripts.length !== 1 || scripts[0].hasAttribute("src")) return undefined;
|
|
69
|
+
const script = scripts[0].textContent ?? "";
|
|
70
|
+
|
|
71
|
+
if (script.length > 16_384) return undefined;
|
|
72
|
+
|
|
73
|
+
// Match only the observed literal string-doubling expression. Never evaluate code.
|
|
74
|
+
const match =
|
|
75
|
+
/await\s*\(\s*async\s+([a-zA-Z_$][\w$]*)\s*=>\s*\1\s*\+\s*\1\s*\)\s*\(\s*(["'])([a-f0-9]{1,128})\2\s*\)/i.exec(
|
|
76
|
+
script,
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
if (
|
|
80
|
+
!match ||
|
|
81
|
+
!/namedItem\(["']solution["']\)\.value\s*=/.test(script) ||
|
|
82
|
+
!/requestSubmit\(\)/.test(script)
|
|
83
|
+
)
|
|
84
|
+
return undefined;
|
|
85
|
+
target.searchParams.set("solution", match[3] + match[3]);
|
|
86
|
+
target.searchParams.set("js_challenge", "1");
|
|
87
|
+
target.searchParams.set("jsc_token", token);
|
|
88
|
+
target.searchParams.set("jsc_orig_r", "");
|
|
89
|
+
|
|
90
|
+
for (const [key, value] of pageUrl.searchParams) target.searchParams.append(key, value);
|
|
91
|
+
|
|
92
|
+
return target;
|
|
93
|
+
}
|
package/src/document/evidence.ts
CHANGED
|
@@ -83,7 +83,14 @@ export function diagnoseExtraction(raw: string, markdown: string): ExtractionDia
|
|
|
83
83
|
const { document } = parseHTML(raw);
|
|
84
84
|
const anubisText = [...document.childNodes].map((node) => node.textContent ?? "").join(" ");
|
|
85
85
|
|
|
86
|
+
const redditChallenge =
|
|
87
|
+
markdown.trim().length < CAPTCHA_CHALLENGE_MAX_MARKDOWN_CHARACTERS &&
|
|
88
|
+
/<input\b[^>]*\bname\s*=\s*["']js_challenge["']/i.test(raw) &&
|
|
89
|
+
/<input\b[^>]*\bname\s*=\s*["']jsc_token["']/i.test(raw) &&
|
|
90
|
+
/<input\b[^>]*\bname\s*=\s*["']solution["']/i.test(raw);
|
|
91
|
+
|
|
86
92
|
const captchaChallenge =
|
|
93
|
+
redditChallenge ||
|
|
87
94
|
CAPTCHA_LOADER_MARKERS.some((marker) => marker.test(raw)) ||
|
|
88
95
|
(markdown.trim().length < CAPTCHA_CHALLENGE_MAX_MARKDOWN_CHARACTERS &&
|
|
89
96
|
CAPTCHA_WIDGET_MARKERS.some((marker) => marker.test(raw)));
|
package/src/document/extract.ts
CHANGED
|
@@ -164,6 +164,12 @@ export function htmlToMarkdownFallback(html: string): string {
|
|
|
164
164
|
}
|
|
165
165
|
}
|
|
166
166
|
|
|
167
|
+
for (const element of document.body.querySelectorAll(
|
|
168
|
+
"p, div, section, article, li, blockquote, pre, h1, h2, h3, h4, h5, h6, br, tr",
|
|
169
|
+
)) {
|
|
170
|
+
element.after(document.createTextNode("\n\n"));
|
|
171
|
+
}
|
|
172
|
+
|
|
167
173
|
return document.body.textContent
|
|
168
174
|
.replace(/[ \t]+\n/g, "\n")
|
|
169
175
|
.replace(/\n[ \t]+/g, "\n")
|
|
@@ -259,6 +265,24 @@ function stripChromeWrappers(document: Document, baseUrl: URL): void {
|
|
|
259
265
|
}
|
|
260
266
|
}
|
|
261
267
|
|
|
268
|
+
/** Restricts GitHub repository roots to their rendered README, when available. */
|
|
269
|
+
function isolateGitHubReadme(document: Document, baseUrl: URL): boolean {
|
|
270
|
+
if (baseUrl.hostname !== "github.com" || baseUrl.pathname.split("/").filter(Boolean).length !== 2)
|
|
271
|
+
return false;
|
|
272
|
+
|
|
273
|
+
const readme = document.querySelector(
|
|
274
|
+
"main article.markdown-body.entry-content[itemprop='text']",
|
|
275
|
+
);
|
|
276
|
+
|
|
277
|
+
if (!readme || !readme.textContent?.trim()) return false;
|
|
278
|
+
|
|
279
|
+
const main = document.createElement("main");
|
|
280
|
+
main.appendChild(readme);
|
|
281
|
+
document.body.replaceChildren(main);
|
|
282
|
+
|
|
283
|
+
return true;
|
|
284
|
+
}
|
|
285
|
+
|
|
262
286
|
/**
|
|
263
287
|
* Collapses consecutive duplicate markdown link lines (e.g. duplicated [Latest] on GitHub releases).
|
|
264
288
|
*
|
|
@@ -426,10 +450,13 @@ export async function extractHtmlToMarkdown(
|
|
|
426
450
|
fragmentOffsets?: Record<string, number>;
|
|
427
451
|
describedByLink?: string;
|
|
428
452
|
markdownAlternateLink?: string;
|
|
453
|
+
githubReadme?: boolean;
|
|
429
454
|
}> {
|
|
430
455
|
// Keep refusal outside the catch: the fallback also constructs a DOM.
|
|
431
456
|
assertConversionDepth(html);
|
|
432
457
|
let advertised: AdvertisedLinks = {};
|
|
458
|
+
let fallbackHtml = html;
|
|
459
|
+
let githubReadme = false;
|
|
433
460
|
|
|
434
461
|
try {
|
|
435
462
|
assertAbsoluteHttpUrl(baseUrl);
|
|
@@ -446,9 +473,13 @@ export async function extractHtmlToMarkdown(
|
|
|
446
473
|
originalIds.set(element, element.id);
|
|
447
474
|
}
|
|
448
475
|
|
|
449
|
-
normalizeSelectorUnsafeIds(document);
|
|
450
476
|
stripChromeWrappers(document, baseUrl);
|
|
451
477
|
advertised = readAdvertisedLinks(document, baseUrl);
|
|
478
|
+
|
|
479
|
+
githubReadme = isolateGitHubReadme(document, baseUrl);
|
|
480
|
+
|
|
481
|
+
if (githubReadme) fallbackHtml = document.documentElement.outerHTML;
|
|
482
|
+
normalizeSelectorUnsafeIds(document);
|
|
452
483
|
// Pass the full absolute URL so Defuddle resolves relative links (e.g.
|
|
453
484
|
// `/owner/repo/releases`) and metadata against the real origin instead of
|
|
454
485
|
// dropping the scheme and host and constructing `new URL(pathname)`.
|
|
@@ -465,6 +496,7 @@ export async function extractHtmlToMarkdown(
|
|
|
465
496
|
markdown,
|
|
466
497
|
title: trimmedTitle || undefined,
|
|
467
498
|
extractor: "defuddle",
|
|
499
|
+
githubReadme,
|
|
468
500
|
fragmentOffsets: buildFragmentOffsets(document, markdown, originalIds),
|
|
469
501
|
...advertised,
|
|
470
502
|
};
|
|
@@ -473,11 +505,11 @@ export async function extractHtmlToMarkdown(
|
|
|
473
505
|
// Fall through to the basic converter for malformed or unsupported pages.
|
|
474
506
|
}
|
|
475
507
|
|
|
476
|
-
const fallbackMarkdown = stripExtractedCssCruft(htmlToMarkdownFallback(
|
|
508
|
+
const fallbackMarkdown = stripExtractedCssCruft(htmlToMarkdownFallback(fallbackHtml));
|
|
477
509
|
let fragmentOffsets: Record<string, number> = {};
|
|
478
510
|
|
|
479
511
|
try {
|
|
480
|
-
fragmentOffsets = buildFragmentOffsets(parseHTML(
|
|
512
|
+
fragmentOffsets = buildFragmentOffsets(parseHTML(fallbackHtml).document, fallbackMarkdown);
|
|
481
513
|
} catch {
|
|
482
514
|
// Malformed HTML has already been reduced to plain text; no anchors are available.
|
|
483
515
|
}
|
|
@@ -485,6 +517,7 @@ export async function extractHtmlToMarkdown(
|
|
|
485
517
|
return {
|
|
486
518
|
markdown: fallbackMarkdown,
|
|
487
519
|
extractor: "basic",
|
|
520
|
+
githubReadme,
|
|
488
521
|
fragmentOffsets,
|
|
489
522
|
...advertised,
|
|
490
523
|
};
|
package/src/index.ts
CHANGED
|
@@ -79,14 +79,14 @@ export const webFetchParameters = Type.Object({
|
|
|
79
79
|
Type.String({
|
|
80
80
|
minLength: 1,
|
|
81
81
|
maxLength: FETCH_MAX_QUERY_CHARACTERS,
|
|
82
|
-
description: "
|
|
82
|
+
description: "Select page sections matching this text.",
|
|
83
83
|
}),
|
|
84
84
|
),
|
|
85
85
|
offset: Type.Optional(
|
|
86
86
|
Type.Integer({
|
|
87
87
|
minimum: 0,
|
|
88
88
|
maximum: FETCH_MAX_OFFSET_CHARACTERS,
|
|
89
|
-
description: "
|
|
89
|
+
description: "Continue at this character offset in the selected view.",
|
|
90
90
|
}),
|
|
91
91
|
),
|
|
92
92
|
});
|
|
@@ -99,6 +99,7 @@ export const webFetchParameters = Type.Object({
|
|
|
99
99
|
export default function (pi: ExtensionAPI) {
|
|
100
100
|
pi.registerTool<typeof webFetchParameters, Partial<WebFetchDetails>>({
|
|
101
101
|
name: "web_fetch",
|
|
102
|
+
exposure: "model-only",
|
|
102
103
|
label: "Web Fetch",
|
|
103
104
|
description: "Read content from a public URL.",
|
|
104
105
|
parameters: webFetchParameters,
|