@jayyuen66/dsh-scrapling 0.0.0-stage → 0.0.1-rc.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/client.js ADDED
@@ -0,0 +1,1015 @@
1
+ // GENERATED by build-client.ts — edit src/client-entry.ts, then run `node build-client.ts`.
2
+ window.__ModuleLoader__.load({
3
+ id: '@jayyuen66/dsh-scrapling',
4
+ factory: (require) => {
5
+ var module = { exports: {} };
6
+ var exports = module.exports;
7
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
8
+ let react = require("react");
9
+ //#region node_modules/.pnpm/@jayyuen66+dsh-plugin-shared@0.1.3_b27ef5d0d5133ce558f2812ee20c5ef8/node_modules/@jayyuen66/dsh-plugin-shared/dist/card-apply.js
10
+ function e$1(e, t, n) {
11
+ let r = globalThis;
12
+ if (r[t] === !0) return !1;
13
+ r[t] = !0;
14
+ try {
15
+ e.effect(() => () => {
16
+ r[t] = void 0;
17
+ }, n);
18
+ } catch (e) {
19
+ throw r[t] = void 0, e;
20
+ }
21
+ return !0;
22
+ }
23
+ //#endregion
24
+ //#region node_modules/.pnpm/@jayyuen66+dsh-plugin-shared@0.1.3_b27ef5d0d5133ce558f2812ee20c5ef8/node_modules/@jayyuen66/dsh-plugin-shared/dist/record.js
25
+ function e(e) {
26
+ return typeof e == `object` && !!e && !Array.isArray(e);
27
+ }
28
+ //#endregion
29
+ //#region src/settings-fields.ts
30
+ /** 本包在插件管理器里的包名,也就是 `plugins.bundle.config` 这张 keyed slot 的键。 */
31
+ const BUNDLE_PACKAGE_NAME = "@jayyuen66/dsh-scrapling";
32
+ /** 字段表。顺序即卡上的呈现顺序,按「先让用户认出东西、再调细节」排。 */
33
+ const SETTINGS_GROUPS = [
34
+ {
35
+ id: "runtime",
36
+ title: {
37
+ zh: "运行环境",
38
+ en: "Runtime"
39
+ },
40
+ note: {
41
+ zh: "Python 与数据目录。装完 scrapling 之后这里一般不用动。",
42
+ en: "Python and the data directory. Usually untouched once scrapling is installed."
43
+ },
44
+ fields: [
45
+ {
46
+ key: "pythonBin",
47
+ kind: "text",
48
+ label: {
49
+ zh: "Python 解释器",
50
+ en: "Python interpreter"
51
+ },
52
+ hint: {
53
+ zh: "留空用 PATH 上的 python3;装了 pyenv/conda 时填绝对路径。",
54
+ en: "Leave empty to use python3 from PATH; give an absolute path under pyenv/conda."
55
+ },
56
+ fallback: ""
57
+ },
58
+ {
59
+ key: "failFastOnMissingPython",
60
+ kind: "boolean",
61
+ label: {
62
+ zh: "Python 缺失时拒绝加载",
63
+ en: "Fail fast without Python"
64
+ },
65
+ hint: {
66
+ zh: "开:Python 起不来就不注册任何工具并报错。关:只警告,工具全部消失。",
67
+ en: "On: register nothing and error out. Off: warn and register nothing."
68
+ },
69
+ fallback: true
70
+ },
71
+ {
72
+ key: "dataDir",
73
+ kind: "text",
74
+ label: {
75
+ zh: "数据目录",
76
+ en: "Data directory"
77
+ },
78
+ hint: {
79
+ zh: "留空用 ~/.dsh-scrapling;选择器自愈的指纹库落在这里,需要可写。",
80
+ en: "Leave empty for ~/.dsh-scrapling; the adaptive selector store lives here and must be writable."
81
+ },
82
+ fallback: ""
83
+ }
84
+ ]
85
+ },
86
+ {
87
+ id: "tools",
88
+ title: {
89
+ zh: "工具开关",
90
+ en: "Tools"
91
+ },
92
+ note: {
93
+ zh: "关掉的工具不会注册给模型,也不会在会话提示里占位置。",
94
+ en: "A disabled tool is never registered with the model and leaves the system prompt too."
95
+ },
96
+ fields: [
97
+ {
98
+ key: "fetchEnabled",
99
+ kind: "boolean",
100
+ label: {
101
+ zh: "scrapling_fetch",
102
+ en: "scrapling_fetch"
103
+ },
104
+ hint: {
105
+ zh: "curl_cffi 静态抓取。",
106
+ en: "Static fetching over curl_cffi."
107
+ },
108
+ fallback: true
109
+ },
110
+ {
111
+ key: "extractEnabled",
112
+ kind: "boolean",
113
+ label: {
114
+ zh: "scrapling_extract",
115
+ en: "scrapling_extract"
116
+ },
117
+ hint: {
118
+ zh: "对给定 HTML 做选择器抽取,不发请求。",
119
+ en: "Extract from given HTML without any request."
120
+ },
121
+ fallback: true
122
+ },
123
+ {
124
+ key: "renderEnabled",
125
+ kind: "boolean",
126
+ label: {
127
+ zh: "scrapling_render",
128
+ en: "scrapling_render"
129
+ },
130
+ hint: {
131
+ zh: "Playwright 渲染,能读 SPA。",
132
+ en: "Playwright rendering; reads SPAs."
133
+ },
134
+ fallback: true
135
+ },
136
+ {
137
+ key: "captureXhrEnabled",
138
+ kind: "boolean",
139
+ label: {
140
+ zh: "scrapling_capture_xhr",
141
+ en: "scrapling_capture_xhr"
142
+ },
143
+ hint: {
144
+ zh: "只回传 XHR 的 URL/状态/字节数;站点数据在接口里时用它找那个接口。",
145
+ en: "Returns XHR url/status/bytes only — use it to locate the JSON endpoint."
146
+ },
147
+ fallback: true
148
+ },
149
+ {
150
+ key: "stealthEnabled",
151
+ kind: "boolean",
152
+ label: {
153
+ zh: "scrapling_stealth_fetch",
154
+ en: "scrapling_stealth_fetch"
155
+ },
156
+ hint: {
157
+ zh: "patchright 反检测抓取,最吃资源,默认关。",
158
+ en: "patchright anti-detection; heavy, off by default."
159
+ },
160
+ fallback: false
161
+ },
162
+ {
163
+ key: "sessionEnabled",
164
+ kind: "boolean",
165
+ label: {
166
+ zh: "scrapling_session_*",
167
+ en: "scrapling_session_*"
168
+ },
169
+ hint: {
170
+ zh: "跨请求复用 cookie 与浏览器实例。",
171
+ en: "Reuse cookies and the browser across requests."
172
+ },
173
+ fallback: true
174
+ },
175
+ {
176
+ key: "crawlEnabled",
177
+ kind: "boolean",
178
+ label: {
179
+ zh: "scrapling_crawl",
180
+ en: "scrapling_crawl"
181
+ },
182
+ hint: {
183
+ zh: "后台 Spider 作业,默认关。",
184
+ en: "Background spider jobs; off by default."
185
+ },
186
+ fallback: false
187
+ },
188
+ {
189
+ key: "answerEnabled",
190
+ kind: "boolean",
191
+ label: {
192
+ zh: "scrapling_answer",
193
+ en: "scrapling_answer"
194
+ },
195
+ hint: {
196
+ zh: "抓完页面交给模型问答。",
197
+ en: "Answer a question over a fetched page."
198
+ },
199
+ fallback: true
200
+ }
201
+ ]
202
+ },
203
+ {
204
+ id: "output",
205
+ title: {
206
+ zh: "抽取与输出",
207
+ en: "Extraction & output"
208
+ },
209
+ note: {
210
+ zh: "这几个数字直接决定模型每轮看到多少 token。",
211
+ en: "These numbers are the per-call token bill the model pays."
212
+ },
213
+ fields: [
214
+ {
215
+ key: "extractionType",
216
+ kind: "enum",
217
+ label: {
218
+ zh: "默认抽取形态",
219
+ en: "Default extraction"
220
+ },
221
+ hint: {
222
+ zh: "markdown 最省 token;html 保留标签;text 只留文字。",
223
+ en: "markdown is cheapest; html keeps tags; text keeps words only."
224
+ },
225
+ fallback: "markdown",
226
+ options: [
227
+ "markdown",
228
+ "html",
229
+ "text"
230
+ ]
231
+ },
232
+ {
233
+ key: "mainContentOnly",
234
+ kind: "boolean",
235
+ label: {
236
+ zh: "只保留正文",
237
+ en: "Main content only"
238
+ },
239
+ hint: {
240
+ zh: "去掉导航、页脚、侧栏。开:更短更干净;关:抓页面控件类目标需要它。",
241
+ en: "Drop nav/footer/sidebar. On for shorter output; off when you need page chrome."
242
+ },
243
+ fallback: true
244
+ },
245
+ {
246
+ key: "fetchMaxOutputChars",
247
+ kind: "number",
248
+ label: {
249
+ zh: "静态抓取正文上限",
250
+ en: "fetch output cap"
251
+ },
252
+ hint: {
253
+ zh: "字符数,超出部分截断并标注。",
254
+ en: "Characters; the payload is clipped and flagged."
255
+ },
256
+ fallback: 2e5
257
+ },
258
+ {
259
+ key: "extractMaxOutputChars",
260
+ kind: "number",
261
+ label: {
262
+ zh: "抽取结果上限",
263
+ en: "extract output cap"
264
+ },
265
+ hint: {
266
+ zh: "字符数。",
267
+ en: "Characters."
268
+ },
269
+ fallback: 2e5
270
+ },
271
+ {
272
+ key: "renderMaxOutputChars",
273
+ kind: "number",
274
+ label: {
275
+ zh: "渲染结果上限",
276
+ en: "render output cap"
277
+ },
278
+ hint: {
279
+ zh: "字符数;会话类工具共用这一档。",
280
+ en: "Characters; the session tools share this budget."
281
+ },
282
+ fallback: 2e5
283
+ },
284
+ {
285
+ key: "xhrMaxOutputChars",
286
+ kind: "number",
287
+ label: {
288
+ zh: "XHR 快照上限",
289
+ en: "XHR snapshot cap"
290
+ },
291
+ hint: {
292
+ zh: "字符数。",
293
+ en: "Characters."
294
+ },
295
+ fallback: 12e4
296
+ },
297
+ {
298
+ key: "crawlMaxItems",
299
+ kind: "number",
300
+ label: {
301
+ zh: "爬虫默认页数上限",
302
+ en: "Crawl page cap"
303
+ },
304
+ hint: {
305
+ zh: "爬虫工具每次调用的默认上限,模型可按次调小。",
306
+ en: "Default per-call page cap; the model may lower it per call."
307
+ },
308
+ fallback: 1e3
309
+ }
310
+ ]
311
+ },
312
+ {
313
+ id: "browser",
314
+ title: {
315
+ zh: "浏览器行为",
316
+ en: "Browser behaviour"
317
+ },
318
+ note: {
319
+ zh: "只在渲染 / 反检测 / XHR 三条路径上生效。",
320
+ en: "Only affects the render / stealth / XHR paths."
321
+ },
322
+ fields: [
323
+ {
324
+ key: "headless",
325
+ kind: "boolean",
326
+ label: {
327
+ zh: "无头运行",
328
+ en: "Headless"
329
+ },
330
+ hint: {
331
+ zh: "关掉会弹出浏览器窗口,反检测站点上成功率更高。",
332
+ en: "Off shows a window and beats more anti-bot checks."
333
+ },
334
+ fallback: true
335
+ },
336
+ {
337
+ key: "networkIdle",
338
+ kind: "boolean",
339
+ label: {
340
+ zh: "等网络静默",
341
+ en: "Wait for network idle"
342
+ },
343
+ hint: {
344
+ zh: "等到没有在途请求再返回。更准,但慢;埋了长连接的站点可能一直等不到。",
345
+ en: "Wait until no requests are in flight. Slower, and pages with long-lived sockets never settle."
346
+ },
347
+ fallback: false
348
+ },
349
+ {
350
+ key: "blockAds",
351
+ kind: "boolean",
352
+ label: {
353
+ zh: "拦截广告与追踪",
354
+ en: "Block ads and trackers"
355
+ },
356
+ hint: {
357
+ zh: "约 3500 个已知广告域。省流量,偶尔误伤。",
358
+ en: "~3,500 known ad domains. Saves bandwidth, occasionally over-blocks."
359
+ },
360
+ fallback: true
361
+ },
362
+ {
363
+ key: "captureXhrPattern",
364
+ kind: "text",
365
+ label: {
366
+ zh: "XHR 捕获正则",
367
+ en: "XHR capture pattern"
368
+ },
369
+ hint: {
370
+ zh: "只记录 URL 匹配这个正则的请求;.* 是全部。",
371
+ en: "Only record requests whose URL matches; .* means all."
372
+ },
373
+ fallback: ".*"
374
+ },
375
+ {
376
+ key: "waitSelectorState",
377
+ kind: "enum",
378
+ label: {
379
+ zh: "等待选择器的状态",
380
+ en: "Wait selector state"
381
+ },
382
+ hint: {
383
+ zh: "配 waitSelector 用;attached 最宽松。",
384
+ en: "Used with waitSelector; attached is the loosest."
385
+ },
386
+ fallback: "attached",
387
+ options: [
388
+ "attached",
389
+ "detached",
390
+ "hidden",
391
+ "visible"
392
+ ]
393
+ }
394
+ ]
395
+ },
396
+ {
397
+ id: "timeouts",
398
+ title: {
399
+ zh: "超时与回收",
400
+ en: "Timeouts & teardown"
401
+ },
402
+ note: {
403
+ zh: "抓取慢若不在这几项里,先看页面上要等的东西。",
404
+ en: "If a fetch is slow for a reason other than these, look at what the page waits for."
405
+ },
406
+ fields: [
407
+ {
408
+ key: "fetchTimeoutMs",
409
+ kind: "number",
410
+ label: {
411
+ zh: "单次抓取超时(毫秒)",
412
+ en: "Per-fetch timeout (ms)"
413
+ },
414
+ hint: {
415
+ zh: "同时是工具的超时预算与 Python 侧的请求上限。",
416
+ en: "Also the tool budget and the Python-side request cap."
417
+ },
418
+ fallback: 12e4
419
+ },
420
+ {
421
+ key: "sidecarHandshakeTimeoutMs",
422
+ kind: "number",
423
+ label: {
424
+ zh: "sidecar 握手超时(毫秒)",
425
+ en: "Handshake timeout (ms)"
426
+ },
427
+ hint: {
428
+ zh: "冷启动要拉起一个 Python 进程并导入 Scrapling。",
429
+ en: "Cold start spawns a Python process and imports Scrapling."
430
+ },
431
+ fallback: 3e4
432
+ },
433
+ {
434
+ key: "sidecarGraceMs",
435
+ kind: "number",
436
+ label: {
437
+ zh: "退出宽限(毫秒)",
438
+ en: "Shutdown grace (ms)"
439
+ },
440
+ hint: {
441
+ zh: "插件卸载后等 Python 收尾的时间。",
442
+ en: "How long Python gets to finish after the plugin unloads."
443
+ },
444
+ fallback: 5e3
445
+ }
446
+ ]
447
+ },
448
+ {
449
+ id: "network",
450
+ title: {
451
+ zh: "网络与安全",
452
+ en: "Network & safety"
453
+ },
454
+ note: {
455
+ zh: "通常什么都不用填:TUN 模式的代理由系统接管,环境里的 HTTPS_PROXY 与 macOS / Windows 的「系统代理」都会被自动接上。",
456
+ en: "Usually nothing to fill: a TUN-mode proxy is taken from the system, and both HTTPS_PROXY in the environment and the macOS / Windows system proxy are picked up automatically."
457
+ },
458
+ fields: [
459
+ {
460
+ key: "proxyUrl",
461
+ kind: "text",
462
+ label: {
463
+ zh: "代理地址",
464
+ en: "Proxy URL"
465
+ },
466
+ hint: {
467
+ zh: "如 http://127.0.0.1:7890 或 socks5://127.0.0.1:7890,可带 user:pass@。留空=依次跟随环境变量与系统代理设置;填了就压过那两者。",
468
+ en: "e.g. http://127.0.0.1:7890 or socks5://127.0.0.1:7890, user:pass@ allowed. Empty follows the environment and then the OS proxy settings; setting it outranks both."
469
+ },
470
+ fallback: ""
471
+ },
472
+ {
473
+ key: "proxyBypass",
474
+ kind: "text",
475
+ label: {
476
+ zh: "代理绕过清单",
477
+ en: "Proxy bypass"
478
+ },
479
+ hint: {
480
+ zh: "逗号分隔的主机名,命中即直连;内网站点要填。整台机器直连填 *。浏览器会话不逐条生效(它的代理在开场时定死)。",
481
+ en: "Comma-separated hostnames that connect directly; list your internal sites. Use * for the whole machine. Not applied per URL inside a browser session (its proxy is fixed at open)."
482
+ },
483
+ fallback: ""
484
+ },
485
+ {
486
+ key: "syntheticDnsRanges",
487
+ kind: "text",
488
+ label: {
489
+ zh: "合成 DNS 段(兜底)",
490
+ en: "Synthetic DNS ranges (fallback)"
491
+ },
492
+ hint: {
493
+ zh: "平时留空:fake-ip 型代理由系统自动认出,这里只用于识别失灵时。列进来的段等于放弃对它们的 SSRF 防护。",
494
+ en: "Leave empty: fake-ip resolvers are detected automatically. Only for detection failures. Listed ranges give up SSRF protection."
495
+ },
496
+ fallback: ""
497
+ },
498
+ {
499
+ key: "allowedHosts",
500
+ kind: "text",
501
+ label: {
502
+ zh: "额外放行的内网主机",
503
+ en: "Extra allowed hosts"
504
+ },
505
+ hint: {
506
+ zh: "逗号分隔。指到内网等于主动放弃对那些目标的 SSRF 防护,只在真要抓自建站点时填。",
507
+ en: "Comma-separated. Pointing these at internal hosts gives up SSRF protection for them — only for self-hosted targets."
508
+ },
509
+ fallback: ""
510
+ },
511
+ {
512
+ key: "nat64Prefixes",
513
+ kind: "text",
514
+ label: {
515
+ zh: "额外 NAT64 前缀",
516
+ en: "Extra NAT64 prefixes"
517
+ },
518
+ hint: {
519
+ zh: "用运营商自建 NAT64 的网络才需要,形如 2001:4860:4860::/96。",
520
+ en: "Only for carrier-grade NAT64 networks, e.g. 2001:4860:4860::/96."
521
+ },
522
+ fallback: ""
523
+ },
524
+ {
525
+ key: "provideWebFetch",
526
+ kind: "boolean",
527
+ label: {
528
+ zh: "接管内置 web_fetch",
529
+ en: "Provide the built-in web_fetch"
530
+ },
531
+ hint: {
532
+ zh: "打开后还要在部署配置里写 fetchProvider: \"scrapling\"。",
533
+ en: "Also needs fetchProvider: \"scrapling\" in the deployment config."
534
+ },
535
+ fallback: false
536
+ },
537
+ {
538
+ key: "provideWebSearch",
539
+ kind: "boolean",
540
+ label: {
541
+ zh: "接管内置 web_search",
542
+ en: "Provide the built-in web_search"
543
+ },
544
+ hint: {
545
+ zh: "打开后还要配 searchEndpoint(SearXNG 兼容接口)与 searchProvider。",
546
+ en: "Also needs a SearXNG-compatible searchEndpoint and searchProvider."
547
+ },
548
+ fallback: false
549
+ }
550
+ ]
551
+ },
552
+ {
553
+ id: "answer",
554
+ title: {
555
+ zh: "页面问答",
556
+ en: "Page Q&A"
557
+ },
558
+ note: {
559
+ zh: "三项都留空就跟随当前会话用的模型,不额外烧一份额度。",
560
+ en: "All blank follows the session's own model, costing no extra quota."
561
+ },
562
+ fields: [
563
+ {
564
+ key: "answerProvider",
565
+ kind: "text",
566
+ label: {
567
+ zh: "回答用 provider",
568
+ en: "Answer provider"
569
+ },
570
+ hint: {
571
+ zh: "留空跟随会话模型。",
572
+ en: "Blank follows the session model."
573
+ },
574
+ fallback: ""
575
+ },
576
+ {
577
+ key: "answerModel",
578
+ kind: "text",
579
+ label: {
580
+ zh: "回答用模型 id",
581
+ en: "Answer model id"
582
+ },
583
+ hint: {
584
+ zh: "与 provider 一起生效。",
585
+ en: "Takes effect together with the provider."
586
+ },
587
+ fallback: ""
588
+ },
589
+ {
590
+ key: "answerMaxTokens",
591
+ kind: "number",
592
+ label: {
593
+ zh: "单次回答 token 上限",
594
+ en: "Answer token cap"
595
+ },
596
+ hint: {
597
+ zh: "一次回答的输出预算。",
598
+ en: "Output budget per answer."
599
+ },
600
+ fallback: 2e3
601
+ }
602
+ ]
603
+ }
604
+ ];
605
+ /** 展平后的字段表,顺序与卡上一致。 */
606
+ const SETTINGS_FIELDS = SETTINGS_GROUPS.flatMap((group) => group.fields);
607
+ SETTINGS_FIELDS.map((field) => field.key);
608
+ //#endregion
609
+ //#region src/settings-card.ts
610
+ /** 把任意设置值转成控件能显示的字符串;结构值给空串而不是 `[object Object]`。 */
611
+ function textOf(value) {
612
+ if (typeof value === "string") return value;
613
+ if (typeof value === "number" || typeof value === "boolean") return String(value);
614
+ return "";
615
+ }
616
+ /**
617
+ * 控件里显示的字符串。草稿优先,其次宿主值,最后字段表里的默认值。
618
+ *
619
+ * @param field - 字段描述
620
+ * @param host - 宿主给的值
621
+ * @param draft - 用户改过但还没保存的值
622
+ * @returns 控件该显示的字符串
623
+ */
624
+ function displayValue(field, host, draft) {
625
+ const edited = draft[field.key];
626
+ if (edited !== void 0) return textOf(edited);
627
+ const current = host?.[field.key];
628
+ return current === void 0 ? String(field.fallback) : textOf(current);
629
+ }
630
+ /**
631
+ * 把控件里敲进去的一串字翻成设置值。
632
+ *
633
+ * 清空 = 交回默认值,所以空串返回 null(存不下 null —— null 与「没设」在 schema 里同义,
634
+ * 但 unset 表达得更准,且不会在用户层留下一条永远等于默认值的记录)。数字字段按 schema 的
635
+ * 下界夹住,负数与非数字在这里就被挡住,而不是等宿主校验失败再回一句看不懂的报错。
636
+ *
637
+ * @param field - 字段描述
638
+ * @param raw - 控件里的原始字符串
639
+ * @returns 要存的值;null 表示「清除这一项」
640
+ */
641
+ function parseInput(field, raw) {
642
+ if (field.kind === "boolean") return raw === "true";
643
+ const text = raw.trim();
644
+ if (text === "") return null;
645
+ if (field.kind === "number") {
646
+ const parsed = Number(text);
647
+ return Number.isFinite(parsed) && parsed > 0 ? Math.floor(parsed) : null;
648
+ }
649
+ return text;
650
+ }
651
+ /**
652
+ * 把草稿翻成一串写入操作。
653
+ *
654
+ * 与默认值相同的项走 unset:用户层里「显式写了默认值」和「没写」在将来改默认值时行为完全
655
+ * 不同,只有 unset 能保住这个区别。顺序跟字段表一致,于是同一份草稿每次生成同样的操作串,
656
+ * 宿主按 revision 拒绝时也不会因为顺序抖动而给出不同的理由。
657
+ *
658
+ * @param draft - 用户改过的字段(只有改过的在里面)
659
+ * @returns 写入操作
660
+ */
661
+ function buildOps(draft) {
662
+ return SETTINGS_FIELDS.filter((field) => field.key in draft).map((field) => {
663
+ const value = draft[field.key];
664
+ return value === null || value === field.fallback ? {
665
+ op: "unset",
666
+ path: [field.key]
667
+ } : {
668
+ op: "set",
669
+ path: [field.key],
670
+ value
671
+ };
672
+ });
673
+ }
674
+ /** 卡片样式。跟着宿主的语义色走,因此明暗两套主题都成立。 */
675
+ const CARD_CSS = `
676
+ .dss-card{display:flex;flex-direction:column;gap:16px;color:var(--dsw-alias-label-primary)}
677
+ .dss-group{display:flex;flex-direction:column;gap:10px}
678
+ .dss-group+.dss-group{border-top:.5px solid var(--dsw-alias-border-l2);padding-top:16px}
679
+ .dss-groupTitle{margin:0;font-size:14px;font-weight:600;line-height:22px}
680
+ .dss-note{margin:0;color:var(--dsw-alias-label-tertiary);font-size:12px;line-height:18px}
681
+ .dss-fields{display:flex;flex-direction:column;gap:10px}
682
+ .dss-field{display:grid;grid-template-columns:minmax(0,1fr) minmax(0,1fr);gap:4px 16px;align-items:start}
683
+ .dss-label{font-size:13px;line-height:20px}
684
+ .dss-hint{margin:0;color:var(--dsw-alias-label-tertiary);font-size:12px;line-height:18px;grid-column:1}
685
+ .dss-control{grid-column:2;display:flex;justify-content:flex-end}
686
+ .dss-control input[type=text],.dss-control input[type=number]{border:.5px solid var(--dsw-alias-border-l3);border-radius:var(--dsw-radius-sm);background:var(--dsw-alias-bg-layer-1);color:var(--dsw-alias-label-primary);font:inherit;font-size:13px;padding:5px 8px;width:100%;max-width:280px}
687
+ .dss-control input:focus-visible{outline:var(--dsw-focus-ring-width) solid var(--dsw-focus-ring-color,var(--dsw-alias-state-business-primary));outline-offset:1px}
688
+ .dss-control select{border:.5px solid var(--dsw-alias-border-l3);border-radius:var(--dsw-radius-sm);background:var(--dsw-alias-bg-layer-1);color:var(--dsw-alias-label-primary);font:inherit;font-size:13px;padding:5px 8px;max-width:280px}
689
+ .dss-switch{appearance:none;width:36px;height:20px;border-radius:10px;background:var(--dsw-alias-border-l3);position:relative;cursor:pointer;border:0}
690
+ .dss-switch:checked{background:var(--dsw-alias-state-business-primary)}
691
+ .dss-switch::after{content:"";position:absolute;top:2px;left:2px;width:16px;height:16px;border-radius:50%;background:#fff;transition:transform .14s}
692
+ .dss-switch:checked::after{transform:translateX(16px)}
693
+ .dss-switch:disabled{cursor:not-allowed;opacity:.55}
694
+ .dss-actions{display:flex;gap:8px;align-items:center}
695
+ .dss-button{border:.5px solid var(--dsw-alias-border-l3);border-radius:var(--dsw-radius-sm);background:0 0;color:var(--dsw-alias-label-primary);font:inherit;font-size:13px;cursor:pointer;padding:5px 14px}
696
+ .dss-button[data-primary=true]{background:var(--dsw-alias-state-business-primary);border-color:transparent}
697
+ .dss-button:disabled{opacity:.5;cursor:not-allowed}
698
+ .dss-status{margin:0;color:var(--dsw-alias-label-tertiary);font-size:12px}
699
+ .dss-status[data-error=true]{color:var(--dsw-alias-state-error-primary)}
700
+ @media (max-width:560px){.dss-field{grid-template-columns:minmax(0,1fr)}.dss-hint,.dss-control{grid-column:1}}
701
+ `;
702
+ /** 这个值是不是一个带本卡所需那点 API 的 document。类型守卫而不是断言。 */
703
+ function isDocumentFace(value) {
704
+ return typeof value === "object" && value !== null && typeof Reflect.get(value, "querySelector") === "function" && typeof Reflect.get(value, "createElement") === "function";
705
+ }
706
+ /** 宿主浏览器里的 document;Node 侧(测试、打包)没有,返回 undefined。
707
+ *
708
+ * 导出是为了能被直接测:这一层的两条分支(是 / 不是 document)在真浏览器里各要一次挂载
709
+ * 才走得到,而把它藏在组件里就只能靠渲染来覆盖。`test/settings-card.test.ts` 直接打它。
710
+ */
711
+ function browserDocument() {
712
+ const candidate = Reflect.get(globalThis, "document");
713
+ return isDocumentFace(candidate) ? candidate : void 0;
714
+ }
715
+ /** 把样式挂进文档,只挂一次。重复挂载会重复注入同一段 CSS,所以那个短路是必要的。
716
+ *
717
+ * 导出是为了能被直接测 —— 同 :func:`browserDocument` 的理由。
718
+ */
719
+ function installStyles(doc) {
720
+ const id = "dsh-scrapling-settings-css";
721
+ if (doc.querySelector(`#${id}`) !== null) return;
722
+ const tag = doc.createElement("style");
723
+ tag.id = id;
724
+ tag.textContent = CARD_CSS;
725
+ doc.head.append(tag);
726
+ }
727
+ /** 一个字段的控件。 */
728
+ function renderControl(props) {
729
+ const { field, shown, disabled, onChange } = props;
730
+ if (field.kind === "boolean") return (0, react.createElement)("input", {
731
+ type: "checkbox",
732
+ className: "dss-switch",
733
+ checked: shown === "true",
734
+ disabled,
735
+ "aria-label": field.label.zh,
736
+ onChange: (event) => {
737
+ onChange(String(event.target.checked));
738
+ }
739
+ });
740
+ if (field.kind === "enum") return (0, react.createElement)("select", {
741
+ disabled,
742
+ value: shown,
743
+ "aria-label": field.label.zh,
744
+ onChange: (event) => {
745
+ onChange(event.target.value);
746
+ }
747
+ }, (field.options ?? []).map((option) => (0, react.createElement)("option", {
748
+ key: option,
749
+ value: option
750
+ }, option)));
751
+ return (0, react.createElement)("input", {
752
+ type: field.kind === "number" ? "number" : "text",
753
+ value: shown,
754
+ disabled,
755
+ min: field.kind === "number" ? 1 : void 0,
756
+ "aria-label": field.label.zh,
757
+ onChange: (event) => {
758
+ onChange(event.target.value);
759
+ }
760
+ });
761
+ }
762
+ /** 一行:左边标签与说明,右边控件。 */
763
+ function renderFieldRow(props) {
764
+ const { field, shown, disabled, onChange } = props;
765
+ return (0, react.createElement)("div", { className: "dss-field" }, (0, react.createElement)("div", { className: "dss-label" }, field.label.zh, (0, react.createElement)("p", { className: "dss-hint" }, field.hint.zh)), (0, react.createElement)("div", { className: "dss-control" }, renderControl({
766
+ field,
767
+ shown,
768
+ disabled,
769
+ onChange
770
+ })));
771
+ }
772
+ /** 一组字段。 */
773
+ function renderGroup(props) {
774
+ const { group, shown, disabled, onChange } = props;
775
+ return (0, react.createElement)("section", { className: "dss-group" }, (0, react.createElement)("h3", { className: "dss-groupTitle" }, group.title.zh), (0, react.createElement)("p", { className: "dss-note" }, group.note.zh), (0, react.createElement)("div", { className: "dss-fields" }, group.fields.map((field) => renderFieldRow({
776
+ field,
777
+ shown: shown(field),
778
+ disabled,
779
+ onChange: (raw) => {
780
+ onChange(field, raw);
781
+ }
782
+ }))));
783
+ }
784
+ /** 顶部那行状态说明。拆成函数是为了不在 JSX 里套三元。 */
785
+ function statusLine(state, changed) {
786
+ if (state.status === "loading") return "正在读取设置…";
787
+ if (state.status === "unavailable" || !state.writable) return "这个连接以只读方式运行,设置改不了。";
788
+ return changed === 0 ? "没有未保存的改动" : `已改动 ${changed} 项,未保存`;
789
+ }
790
+ /** 设置卡本体。导出为常量而非函数声明:它是 React 组件,不是可 new 的类。 */
791
+ function SettingsCard(props) {
792
+ const { form } = props;
793
+ const [draft, setDraft] = (0, react.useState)({});
794
+ const [failure, setFailure] = (0, react.useState)("");
795
+ const [saving, setSaving] = (0, react.useState)(false);
796
+ const doc = browserDocument();
797
+ if (doc !== void 0) installStyles(doc);
798
+ if (props.view !== "page" || form === void 0) return (0, react.createElement)("div", { className: "dss-card" });
799
+ const { state } = form;
800
+ const editable = state.writable && state.status === "ready";
801
+ const ops = buildOps(draft);
802
+ const save = async () => {
803
+ setSaving(true);
804
+ setFailure("");
805
+ try {
806
+ if (await form.mutate(ops, state.revision)) setDraft({});
807
+ else setFailure("保存被宿主拒绝了,设置未改动");
808
+ } catch (error) {
809
+ setFailure(`保存失败:${String(error)}`);
810
+ } finally {
811
+ setSaving(false);
812
+ }
813
+ };
814
+ const onFieldChange = (field, raw) => {
815
+ setFailure("");
816
+ setDraft((previous) => ({
817
+ ...previous,
818
+ [field.key]: parseInput(field, raw)
819
+ }));
820
+ };
821
+ const groups = SETTINGS_GROUPS.map((group) => renderGroup({
822
+ group,
823
+ shown: (field) => displayValue(field, state.value, draft),
824
+ disabled: !editable,
825
+ onChange: onFieldChange
826
+ }));
827
+ const actions = (0, react.createElement)("div", { className: "dss-actions" }, (0, react.createElement)("button", {
828
+ type: "button",
829
+ className: "dss-button",
830
+ "data-primary": "true",
831
+ disabled: ops.length === 0 || saving || !editable,
832
+ onClick: () => {
833
+ save();
834
+ }
835
+ }, saving ? "保存中…" : "保存"), (0, react.createElement)("button", {
836
+ type: "button",
837
+ className: "dss-button",
838
+ disabled: ops.length === 0 || saving,
839
+ onClick: () => {
840
+ setDraft({});
841
+ setFailure("");
842
+ }
843
+ }, "撤销"));
844
+ const status = (0, react.createElement)("p", {
845
+ className: "dss-status",
846
+ "data-error": "false"
847
+ }, statusLine(state, ops.length));
848
+ const reported = failure === "" ? null : (0, react.createElement)("p", {
849
+ className: "dss-status",
850
+ "data-error": "true"
851
+ }, failure);
852
+ return (0, react.createElement)("div", { className: "dss-card" }, status, reported, ...groups, actions);
853
+ }
854
+ //#endregion
855
+ //#region src/client-entry.ts
856
+ /**
857
+ * 需要专用结果行的工具。
858
+ *
859
+ * 七个「面向用户」的工具各一行;会话的四个工具共用一组 key,但分散的 key 更省心 ——
860
+ * slot 按 key 派发,漏掉一个只是那一个不画行,不会连累别的。
861
+ */
862
+ const ROW_TOOLS = [
863
+ "scrapling_fetch",
864
+ "scrapling_extract",
865
+ "scrapling_render",
866
+ "scrapling_capture_xhr",
867
+ "scrapling_stealth_fetch",
868
+ "scrapling_session_open",
869
+ "scrapling_session_fetch",
870
+ "scrapling_session_list",
871
+ "scrapling_session_close",
872
+ "scrapling_crawl"
873
+ ];
874
+ /** 把任意值转成字符串;非字符串/非数字一律给空串。 */
875
+ function text(value) {
876
+ if (typeof value === "string") return value;
877
+ if (typeof value === "number" || typeof value === "boolean") return String(value);
878
+ return "";
879
+ }
880
+ /** 取第一个非空字符串候选。 */
881
+ function firstText(...candidates) {
882
+ for (const candidate of candidates) {
883
+ const value = text(candidate);
884
+ if (value !== "") return value;
885
+ }
886
+ return "";
887
+ }
888
+ /**
889
+ * 从一次工具调用的参数与投影里抽出要展示的行。
890
+ *
891
+ * 两侧都按「可能缺字段」处理:卡片的输入是别的进程拼出来的载荷,少一个字段就少展示
892
+ * 一项,而不是抛错把整行画没了。
893
+ *
894
+ * @param tool 工具名
895
+ * @param args 调用参数
896
+ * @param meta Host 侧 presentationMeta 投影(可能缺席)
897
+ * @returns 行规格
898
+ */
899
+ function buildRow(tool, args, meta) {
900
+ const parameters = e(args) ? args : {};
901
+ const projected = e(meta) ? meta : {};
902
+ const url = firstText(projected["url"], parameters["url"]);
903
+ if (tool === "scrapling_extract") {
904
+ const selector = text(parameters["selector"]);
905
+ return {
906
+ tool,
907
+ title: selector === "" ? tool : `${tool} ${selector}`,
908
+ fields: [{
909
+ label: "匹配",
910
+ value: text(projected["count"])
911
+ }]
912
+ };
913
+ }
914
+ if (tool === "scrapling_session_list") return {
915
+ tool,
916
+ title: tool,
917
+ fields: [{
918
+ label: "会话数",
919
+ value: text(projected["count"])
920
+ }]
921
+ };
922
+ if (tool === "scrapling_session_open") return {
923
+ tool,
924
+ title: tool,
925
+ fields: [{
926
+ label: "类型",
927
+ value: text(parameters["sessionType"])
928
+ }, {
929
+ label: "会话",
930
+ value: text(projected["sessionId"])
931
+ }]
932
+ };
933
+ if (tool === "scrapling_session_close") return {
934
+ tool,
935
+ title: tool,
936
+ fields: [{
937
+ label: "会话",
938
+ value: text(parameters["sessionId"])
939
+ }]
940
+ };
941
+ if (tool === "scrapling_crawl") return {
942
+ tool,
943
+ title: tool,
944
+ fields: [
945
+ {
946
+ label: "起始",
947
+ value: url
948
+ },
949
+ {
950
+ label: "上限",
951
+ value: text(parameters["maxPages"])
952
+ },
953
+ {
954
+ label: "作业",
955
+ value: text(projected["jobId"])
956
+ }
957
+ ]
958
+ };
959
+ const fields = [];
960
+ if (url !== "") fields.push({
961
+ label: "URL",
962
+ value: url
963
+ });
964
+ const status = projected["statusCode"];
965
+ if (typeof status === "number") fields.push({
966
+ label: "HTTP",
967
+ value: String(status)
968
+ });
969
+ if (projected["truncated"] === true) fields.push({
970
+ label: "正文",
971
+ value: "已截断"
972
+ });
973
+ return {
974
+ tool,
975
+ title: tool,
976
+ fields
977
+ };
978
+ }
979
+ /** 一行结果的渲染。 */
980
+ function RowView(props) {
981
+ return (0, react.createElement)("div", {
982
+ className: "dsh-scrapling-row",
983
+ "data-tool": props.tool
984
+ }, props.tool);
985
+ }
986
+ /** 本 Client 半注入的服务。 */
987
+ const inject = ["slots"];
988
+ /**
989
+ * Client 插件入口:给每个工具注册一行结果视图,并把设置卡挂上插件详情页。
990
+ *
991
+ * @param ctx - 客户端上下文
992
+ */
993
+ function apply(ctx) {
994
+ if (!e$1(ctx, "__dshScraplingToolviewApplied", "dsh-scrapling toolview")) return;
995
+ for (const tool of ROW_TOOLS) ctx.slots.inject("tool.call.toolview", () => {
996
+ ctx.slots.register({
997
+ name: "tool.call.toolview",
998
+ key: tool
999
+ }, () => (0, react.createElement)(RowView, { tool }));
1000
+ });
1001
+ ctx.slots.inject("plugins.bundle.config", () => {
1002
+ ctx.slots.register({
1003
+ name: "plugins.bundle.config",
1004
+ key: BUNDLE_PACKAGE_NAME
1005
+ }, (props) => (0, react.createElement)(SettingsCard, props));
1006
+ });
1007
+ }
1008
+ //#endregion
1009
+ exports.ROW_TOOLS = ROW_TOOLS;
1010
+ exports.apply = apply;
1011
+ exports.buildRow = buildRow;
1012
+ exports.inject = inject;
1013
+ return module.exports;
1014
+ },
1015
+ });