@jayyuen66/dsh-scrapling 0.0.1-rc.3 → 0.0.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 CHANGED
@@ -1,12 +1,13 @@
1
1
  // GENERATED by build-client.ts — edit src/client-entry.ts, then run `node build-client.ts`.
2
2
  window.__ModuleLoader__.load({
3
- id: '@jayyuen66/dsh-scrapling',
3
+ id: "@jayyuen66/dsh-scrapling",
4
4
  factory: (require) => {
5
+ "use strict";
5
6
  var module = { exports: {} };
6
7
  var exports = module.exports;
7
8
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
8
9
  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
+ //#region node_modules/.pnpm/@jayyuen66+dsh-plugin-shared@0.1.4_c79f541388a18a6a0476b83d3755b171/node_modules/@jayyuen66/dsh-plugin-shared/dist/card-apply.js
10
11
  function e$1(e, t, n) {
11
12
  let r = globalThis;
12
13
  if (r[t] === !0) return !1;
@@ -21,12 +22,14 @@ function e$1(e, t, n) {
21
22
  return !0;
22
23
  }
23
24
  //#endregion
24
- //#region node_modules/.pnpm/@jayyuen66+dsh-plugin-shared@0.1.3_b27ef5d0d5133ce558f2812ee20c5ef8/node_modules/@jayyuen66/dsh-plugin-shared/dist/record.js
25
+ //#region node_modules/.pnpm/@jayyuen66+dsh-plugin-shared@0.1.4_c79f541388a18a6a0476b83d3755b171/node_modules/@jayyuen66/dsh-plugin-shared/dist/record.js
25
26
  function e(e) {
26
27
  return typeof e == `object` && !!e && !Array.isArray(e);
27
28
  }
28
29
  //#endregion
29
30
  //#region src/settings-fields.ts
31
+ /** 设置卡对应的 Host 条目 id(cordis.patch.yml 里 `insert` 的 `id`),也是设置命名空间。 */
32
+ const SETTINGS_ENTRY_ID = "scrapling";
30
33
  /** 本包在插件管理器里的包名,也就是 `plugins.bundle.config` 这张 keyed slot 的键。 */
31
34
  const BUNDLE_PACKAGE_NAME = "@jayyuen66/dsh-scrapling";
32
35
  /** 字段表。顺序即卡上的呈现顺序,按「先让用户认出东西、再调细节」排。 */
@@ -472,13 +475,14 @@ const SETTINGS_GROUPS = [
472
475
  {
473
476
  key: "proxyUrl",
474
477
  kind: "text",
478
+ secret: true,
475
479
  label: {
476
480
  zh: "代理地址",
477
481
  en: "Proxy URL"
478
482
  },
479
483
  hint: {
480
- zh: "如 http://127.0.0.1:7890 或 socks5://127.0.0.1:7890,可带 user:pass@。留空=依次跟随环境变量与系统代理设置;填了就压过那两者。",
481
- 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."
484
+ zh: "如 http://127.0.0.1:7890 或 socks5://127.0.0.1:7890,可带 user:pass@。留空=依次跟随环境变量与系统代理设置;填了就压过那两者。值按密码遮罩显示,点「显示」可查看。",
485
+ 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. Masked like a password; press Show to reveal it."
482
486
  },
483
487
  fallback: ""
484
488
  },
@@ -618,6 +622,108 @@ const SETTINGS_GROUPS = [
618
622
  /** 展平后的字段表,顺序与卡上一致。 */
619
623
  const SETTINGS_FIELDS = SETTINGS_GROUPS.flatMap((group) => group.fields);
620
624
  SETTINGS_FIELDS.map((field) => field.key);
625
+ /** 本包 Client 半注册的字典命名空间。 */
626
+ const LOCALE_NS = "dshScrapling";
627
+ /** 设置卡自身的文案:按钮、状态行与读屏提示。字段标签不在这里 —— 它们由字段表摊出来。 */
628
+ const CHROME = {
629
+ zh: {
630
+ save: "保存",
631
+ saving: "保存中…",
632
+ undo: "撤销",
633
+ reveal: "显示",
634
+ conceal: "隐藏",
635
+ revealHint: "这一项按密码遮罩显示",
636
+ statusLoading: "正在读取设置…",
637
+ statusReadonly: "这个连接以只读方式运行,设置改不了。",
638
+ statusClean: "没有未保存的改动",
639
+ statusChanged: "已改动 {count} 项,未保存",
640
+ statusInvalid: "有 {count} 项填的不是合法数字,改好后才能保存",
641
+ rejected: "保存被宿主拒绝了,设置未改动",
642
+ stale: "设置已经被别处改过,请重新打开这张卡再保存",
643
+ failed: "保存失败:{reason}"
644
+ },
645
+ en: {
646
+ save: "Save",
647
+ saving: "Saving…",
648
+ undo: "Undo",
649
+ reveal: "Show",
650
+ conceal: "Hide",
651
+ revealHint: "this field is masked like a password",
652
+ statusLoading: "Reading settings…",
653
+ statusReadonly: "This connection runs read-only; settings cannot be changed here.",
654
+ statusClean: "No unsaved changes",
655
+ statusChanged: "{count} change(s) not saved yet",
656
+ statusInvalid: "{count} field(s) are not valid numbers; fix them to save",
657
+ rejected: "The host refused the save; nothing changed",
658
+ stale: "Settings changed elsewhere; reopen this page before saving",
659
+ failed: "Save failed: {reason}"
660
+ }
661
+ };
662
+ /** 结果行那一行的文案:字段名与那一个「看详情」的入口。 */
663
+ const ROW_CHROME = {
664
+ zh: {
665
+ "row.url": "URL",
666
+ "row.http": "HTTP",
667
+ "row.body": "正文",
668
+ "row.matched": "匹配",
669
+ "row.sessionCount": "会话数",
670
+ "row.session": "会话",
671
+ "row.type": "类型",
672
+ "row.start": "起始",
673
+ "row.pageCap": "上限",
674
+ "row.job": "作业",
675
+ "row.inspect": "在轨迹里查看",
676
+ "row.truncated": "已截断"
677
+ },
678
+ en: {
679
+ "row.url": "URL",
680
+ "row.http": "HTTP",
681
+ "row.body": "Body",
682
+ "row.matched": "Matched",
683
+ "row.sessionCount": "Sessions",
684
+ "row.session": "Session",
685
+ "row.type": "Type",
686
+ "row.start": "Start",
687
+ "row.pageCap": "Page cap",
688
+ "row.job": "Job",
689
+ "row.inspect": "Inspect this call",
690
+ "row.truncated": "Truncated"
691
+ }
692
+ };
693
+ /**
694
+ * 把字段表与卡面文案摊成宿主 locale 服务要的字典。
695
+ *
696
+ * 键的形状:字段是 `field.<key>.label` / `field.<key>.hint`,分组是
697
+ * `group.<id>.title` / `group.<id>.note`,卡面文案直接用自己的键。键从字段表推出来,
698
+ * 所以往表里加一个字段就自动多出两条词典项 —— 不需要在这里再抄一遍。
699
+ *
700
+ * @returns 语言 → 键值表;两份的键集相等
701
+ */
702
+ function localeDictionaries() {
703
+ const result = {
704
+ zh: {
705
+ ...CHROME.zh,
706
+ ...ROW_CHROME.zh
707
+ },
708
+ en: {
709
+ ...CHROME.en,
710
+ ...ROW_CHROME.en
711
+ }
712
+ };
713
+ for (const group of SETTINGS_GROUPS) {
714
+ result.zh[`group.${group.id}.title`] = group.title.zh;
715
+ result.zh[`group.${group.id}.note`] = group.note.zh;
716
+ result.en[`group.${group.id}.title`] = group.title.en;
717
+ result.en[`group.${group.id}.note`] = group.note.en;
718
+ for (const field of group.fields) {
719
+ result.zh[`field.${field.key}.label`] = field.label.zh;
720
+ result.zh[`field.${field.key}.hint`] = field.hint.zh;
721
+ result.en[`field.${field.key}.label`] = field.label.en;
722
+ result.en[`field.${field.key}.hint`] = field.hint.en;
723
+ }
724
+ }
725
+ return result;
726
+ }
621
727
  //#endregion
622
728
  //#region src/settings-card.ts
623
729
  /** 把任意设置值转成控件能显示的字符串;结构值给空串而不是 `[object Object]`。 */
@@ -643,36 +749,43 @@ function displayValue(field, host, draft) {
643
749
  /**
644
750
  * 把控件里敲进去的一串字翻成设置值。
645
751
  *
646
- * 清空 = 交回默认值,所以空串返回 null(存不下 null —— null 与「没设」在 schema 里同义,
647
- * 但 unset 表达得更准,且不会在用户层留下一条永远等于默认值的记录)。数字字段按 schema 的
648
- * 下界夹住,负数与非数字在这里就被挡住,而不是等宿主校验失败再回一句看不懂的报错。
752
+ * 清空 = 交回默认值,所以空串给 `clear`。数字字段按 schema 的下界夹住;非数字、零、负数与
753
+ * 小数一律给 `invalid` 而不是 `clear`。早先把它们当成清除,用户填错一个数就会把自己原来
754
+ * 那条覆盖静默删掉,而且界面上看不出发生过什么 —— 非法输入留在控件里、保存按钮因此不可用,
755
+ * 比悄悄改掉用户的配置好。
649
756
  *
650
757
  * @param field - 字段描述
651
758
  * @param raw - 控件里的原始字符串
652
- * @returns 要存的值;null 表示「清除这一项」
759
+ * @returns 解析结果
653
760
  */
654
761
  function parseInput(field, raw) {
655
- if (field.kind === "boolean") return raw === "true";
762
+ if (field.kind === "boolean") return {
763
+ kind: "value",
764
+ value: raw === "true"
765
+ };
656
766
  const text = raw.trim();
657
- if (text === "") return null;
658
- if (field.kind === "number") {
659
- const parsed = Number(text);
660
- return Number.isFinite(parsed) && parsed > 0 ? Math.floor(parsed) : null;
661
- }
662
- return text;
767
+ if (text === "") return { kind: "clear" };
768
+ if (field.kind === "number") return /^[1-9]\d*$/u.test(text) ? {
769
+ kind: "value",
770
+ value: Number(text)
771
+ } : { kind: "invalid" };
772
+ return {
773
+ kind: "value",
774
+ value: text
775
+ };
663
776
  }
664
777
  /**
665
778
  * 把草稿翻成一串写入操作。
666
779
  *
667
780
  * 与默认值相同的项走 unset:用户层里「显式写了默认值」和「没写」在将来改默认值时行为完全
668
781
  * 不同,只有 unset 能保住这个区别。顺序跟字段表一致,于是同一份草稿每次生成同样的操作串,
669
- * 宿主按 revision 拒绝时也不会因为顺序抖动而给出不同的理由。
782
+ * 宿主按 revision 拒绝时也不会因为顺序抖动而给出不同的理由。非法字段不出现在串里。
670
783
  *
671
784
  * @param draft - 用户改过的字段(只有改过的在里面)
672
785
  * @returns 写入操作
673
786
  */
674
787
  function buildOps(draft) {
675
- return SETTINGS_FIELDS.filter((field) => field.key in draft).map((field) => {
788
+ return SETTINGS_FIELDS.filter((field) => Object.hasOwn(draft, field.key)).filter((field) => draft[field.key] !== void 0).map((field) => {
676
789
  const value = draft[field.key];
677
790
  return value === null || value === field.fallback ? {
678
791
  op: "unset",
@@ -696,6 +809,8 @@ const CARD_CSS = `
696
809
  .dss-label{font-size:13px;line-height:20px}
697
810
  .dss-hint{margin:0;color:var(--dsw-alias-label-tertiary);font-size:12px;line-height:18px;grid-column:1}
698
811
  .dss-control{grid-column:2;display:flex;justify-content:flex-end}
812
+ .dss-secret{display:flex;gap:8px;align-items:center;justify-content:flex-end;width:100%}
813
+ .dss-reveal{flex:none;padding:5px 8px;white-space:nowrap}
699
814
  .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}
700
815
  .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}
701
816
  .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}
@@ -714,7 +829,9 @@ const CARD_CSS = `
714
829
  `;
715
830
  /** 这个值是不是一个带本卡所需那点 API 的 document。类型守卫而不是断言。 */
716
831
  function isDocumentFace(value) {
717
- return typeof value === "object" && value !== null && typeof Reflect.get(value, "querySelector") === "function" && typeof Reflect.get(value, "createElement") === "function";
832
+ if (typeof value !== "object" || value === null) return false;
833
+ const head = Reflect.get(value, "head");
834
+ return typeof Reflect.get(value, "querySelector") === "function" && typeof Reflect.get(value, "createElement") === "function" && typeof head === "object" && head !== null && typeof Reflect.get(head, "append") === "function";
718
835
  }
719
836
  /** 宿主浏览器里的 document;Node 侧(测试、打包)没有,返回 undefined。
720
837
  *
@@ -725,27 +842,64 @@ function browserDocument() {
725
842
  const candidate = Reflect.get(globalThis, "document");
726
843
  return isDocumentFace(candidate) ? candidate : void 0;
727
844
  }
728
- /** 把样式挂进文档,只挂一次。重复挂载会重复注入同一段 CSS,所以那个短路是必要的。
845
+ /**
846
+ * 把一段样式挂进文档,返回摘掉它的函数。同一 id 只会有一段,重复调用是空操作。
847
+ *
848
+ * **由 apply 注册在 ctx.effect 里,不在组件的 render 里调**:渲染期改 DOM 是 React 明令
849
+ * 禁止的副作用,并发渲染与 StrictMode 的双调用都会让注入时机不可预测,而同一段 `<style>`
850
+ * 重复注入就会叠两份规则。文档里已经有它时返回的函数也就不该把别人的摘掉。
729
851
  *
730
852
  * 导出是为了能被直接测 —— 同 :func:`browserDocument` 的理由。
853
+ *
854
+ * @param doc - 宿主文档
855
+ * @param id - 样式元素的 id,也是「已经挂过没有」的判据
856
+ * @param css - 样式正文
857
+ * @returns 摘掉本次注入的样式;没有注入时是空操作
731
858
  */
732
- function installStyles(doc) {
733
- const id = "dsh-scrapling-settings-css";
734
- if (doc.querySelector(`#${id}`) !== null) return;
859
+ function installStyle(doc, id, css) {
860
+ const present = doc.querySelector(`#${id}`);
861
+ if (present !== void 0 && present !== null) return () => void 0;
735
862
  const tag = doc.createElement("style");
736
863
  tag.id = id;
737
- tag.textContent = CARD_CSS;
864
+ tag.textContent = css;
738
865
  doc.head.append(tag);
866
+ return () => {
867
+ tag.remove();
868
+ };
869
+ }
870
+ /**
871
+ * 把设置卡的样式挂进文档。
872
+ *
873
+ * @param doc - 宿主文档
874
+ * @returns 摘掉本次注入的样式
875
+ */
876
+ function installStyles(doc) {
877
+ return installStyle(doc, "dsh-scrapling-settings-css", CARD_CSS);
878
+ }
879
+ /**
880
+ * 文本类控件的 input type。
881
+ *
882
+ * 凭据型字段默认走 ``password``:浏览器只画圆点,值本身丝毫不动 —— 编辑、草稿、保存全都
883
+ * 按原串走,遮住的只是屏幕上那一份。导出为纯函数是为了能被直接测:这一层的两条分支在
884
+ * 真浏览器里各要一次点击才走得到,藏在组件里就只能靠渲染来覆盖。
885
+ *
886
+ * @param field - 字段描述
887
+ * @param revealed - 用户是否已经点过「显示」
888
+ * @returns input 的 type
889
+ */
890
+ function controlType(field, revealed) {
891
+ return field.secret === true && !revealed ? "password" : "text";
739
892
  }
740
893
  /** 一个字段的控件。 */
741
894
  function renderControl(props) {
742
- const { field, shown, disabled, onChange } = props;
895
+ const { field, label, shown, disabled, revealed, onReveal, t, onChange } = props;
743
896
  if (field.kind === "boolean") return (0, react.createElement)("input", {
744
897
  type: "checkbox",
745
898
  className: "dss-switch",
899
+ role: "switch",
746
900
  checked: shown === "true",
747
901
  disabled,
748
- "aria-label": field.label.zh,
902
+ "aria-label": label,
749
903
  onChange: (event) => {
750
904
  onChange(String(event.target.checked));
751
905
  }
@@ -753,7 +907,7 @@ function renderControl(props) {
753
907
  if (field.kind === "enum") return (0, react.createElement)("select", {
754
908
  disabled,
755
909
  value: shown,
756
- "aria-label": field.label.zh,
910
+ "aria-label": label,
757
911
  onChange: (event) => {
758
912
  onChange(event.target.value);
759
913
  }
@@ -761,106 +915,191 @@ function renderControl(props) {
761
915
  key: option,
762
916
  value: option
763
917
  }, option)));
764
- return (0, react.createElement)("input", {
765
- type: field.kind === "number" ? "number" : "text",
918
+ const input = (0, react.createElement)("input", {
919
+ type: field.kind === "number" ? "number" : controlType(field, revealed),
766
920
  value: shown,
767
921
  disabled,
922
+ ...field.secret === true ? {
923
+ autoComplete: "off",
924
+ spellCheck: false
925
+ } : {},
768
926
  min: field.kind === "number" ? 1 : void 0,
769
- "aria-label": field.label.zh,
927
+ step: field.kind === "number" ? 1 : void 0,
928
+ "aria-label": label,
770
929
  onChange: (event) => {
771
930
  onChange(event.target.value);
772
931
  }
773
932
  });
933
+ if (field.secret !== true) return input;
934
+ return (0, react.createElement)("span", { className: "dss-secret" }, input, (0, react.createElement)("button", {
935
+ type: "button",
936
+ className: "dss-button dss-reveal",
937
+ disabled,
938
+ "aria-pressed": revealed,
939
+ "aria-label": label,
940
+ onClick: () => {
941
+ onReveal();
942
+ }
943
+ }, t(revealed ? "conceal" : "reveal")));
774
944
  }
775
945
  /** 一行:左边标签与说明,右边控件。 */
776
946
  function renderFieldRow(props) {
777
- const { field, shown, disabled, onChange } = props;
778
- 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({
947
+ const { field, t, shown, disabled, revealed, onReveal, onChange } = props;
948
+ const label = t(`field.${field.key}.label`);
949
+ const control = renderControl({
779
950
  field,
951
+ label,
780
952
  shown,
781
953
  disabled,
954
+ revealed,
955
+ onReveal,
956
+ t,
782
957
  onChange
783
- })));
958
+ });
959
+ const caption = (0, react.createElement)("div", { className: "dss-label" }, label, (0, react.createElement)("p", { className: "dss-hint" }, t(`field.${field.key}.hint`)));
960
+ return (0, react.createElement)("div", {
961
+ className: "dss-field",
962
+ key: field.key
963
+ }, caption, (0, react.createElement)("div", { className: "dss-control" }, control));
784
964
  }
785
965
  /** 一组字段。 */
786
966
  function renderGroup(props) {
787
- const { group, shown, disabled, onChange } = props;
788
- 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({
967
+ const { group, t, shown, disabled, revealed, onReveal, onChange } = props;
968
+ return (0, react.createElement)("section", {
969
+ className: "dss-group",
970
+ key: group.id
971
+ }, (0, react.createElement)("h3", { className: "dss-groupTitle" }, t(`group.${group.id}.title`)), (0, react.createElement)("p", { className: "dss-note" }, t(`group.${group.id}.note`)), (0, react.createElement)("div", { className: "dss-fields" }, group.fields.map((field) => renderFieldRow({
789
972
  field,
973
+ t,
790
974
  shown: shown(field),
791
975
  disabled,
976
+ revealed: revealed(field),
977
+ onReveal: () => {
978
+ onReveal(field);
979
+ },
792
980
  onChange: (raw) => {
793
981
  onChange(field, raw);
794
982
  }
795
983
  }))));
796
984
  }
797
985
  /** 顶部那行状态说明。拆成函数是为了不在 JSX 里套三元。 */
798
- function statusLine(state, changed) {
799
- if (state.status === "loading") return "正在读取设置…";
800
- if (state.status === "unavailable" || !state.writable) return "这个连接以只读方式运行,设置改不了。";
801
- return changed === 0 ? "没有未保存的改动" : `已改动 ${changed} 项,未保存`;
986
+ function statusLine(t, state, changed, invalid) {
987
+ if (state.status === "loading") return t("statusLoading");
988
+ if (invalid > 0) return t("statusInvalid", { count: invalid });
989
+ if (state.status === "unavailable" || !state.writable) return t("statusReadonly");
990
+ return changed === 0 ? t("statusClean") : t("statusChanged", { count: changed });
991
+ }
992
+ const EMPTY_DRAFT = {
993
+ values: {},
994
+ baseRevision: void 0,
995
+ invalid: []
996
+ };
997
+ /** 底部那一行按钮。 */
998
+ function renderActions(props) {
999
+ const { t, saving, editable, blocked, changed, onSave, onUndo } = props;
1000
+ const idle = changed === 0;
1001
+ return (0, react.createElement)("div", { className: "dss-actions" }, (0, react.createElement)("button", {
1002
+ type: "button",
1003
+ className: "dss-button",
1004
+ "data-primary": "true",
1005
+ disabled: idle || blocked || saving || !editable,
1006
+ onClick: onSave
1007
+ }, saving ? t("saving") : t("save")), (0, react.createElement)("button", {
1008
+ type: "button",
1009
+ className: "dss-button",
1010
+ disabled: idle || blocked || saving,
1011
+ onClick: onUndo
1012
+ }, t("undo")));
802
1013
  }
803
1014
  /** 设置卡本体。导出为常量而非函数声明:它是 React 组件,不是可 new 的类。 */
804
1015
  function SettingsCard(props) {
805
- const { form } = props;
806
- const [draft, setDraft] = (0, react.useState)({});
1016
+ const { form, t } = props;
1017
+ const [draft, setDraft] = (0, react.useState)(EMPTY_DRAFT);
807
1018
  const [failure, setFailure] = (0, react.useState)("");
808
1019
  const [saving, setSaving] = (0, react.useState)(false);
809
- const doc = browserDocument();
810
- if (doc !== void 0) installStyles(doc);
1020
+ const [revealed, setRevealed] = (0, react.useState)([]);
811
1021
  if (props.view !== "page" || form === void 0) return (0, react.createElement)("div", { className: "dss-card" });
812
1022
  const { state } = form;
813
1023
  const editable = state.writable && state.status === "ready";
814
- const ops = buildOps(draft);
1024
+ const ops = buildOps(draft.values);
815
1025
  const save = async () => {
816
1026
  setSaving(true);
817
1027
  setFailure("");
818
1028
  try {
819
- if (await form.mutate(ops, state.revision)) setDraft({});
820
- else setFailure("保存被宿主拒绝了,设置未改动");
1029
+ if (await form.mutate(ops, draft.baseRevision)) setDraft((previous) => {
1030
+ const values = { ...previous.values };
1031
+ for (const op of ops) {
1032
+ const [key] = op.path;
1033
+ Reflect.deleteProperty(values, key);
1034
+ }
1035
+ return Object.keys(values).length === 0 ? EMPTY_DRAFT : {
1036
+ ...previous,
1037
+ values
1038
+ };
1039
+ });
1040
+ else setFailure(draft.baseRevision !== void 0 && draft.baseRevision !== state.revision ? t("stale") : t("rejected"));
821
1041
  } catch (error) {
822
- setFailure(`保存失败:${String(error)}`);
1042
+ setFailure(t("failed", { reason: String(error) }));
823
1043
  } finally {
824
1044
  setSaving(false);
825
1045
  }
826
1046
  };
827
1047
  const onFieldChange = (field, raw) => {
828
1048
  setFailure("");
829
- setDraft((previous) => ({
830
- ...previous,
831
- [field.key]: parseInput(field, raw)
832
- }));
1049
+ const parsed = parseInput(field, raw);
1050
+ setDraft((previous) => {
1051
+ const values = { ...previous.values };
1052
+ const invalid = previous.invalid.filter((key) => key !== field.key);
1053
+ const baseRevision = previous.baseRevision ?? state.revision;
1054
+ if (parsed.kind === "invalid") return {
1055
+ values,
1056
+ baseRevision,
1057
+ invalid: [...invalid, field.key]
1058
+ };
1059
+ values[field.key] = parsed.kind === "clear" ? null : parsed.value;
1060
+ return {
1061
+ values,
1062
+ baseRevision,
1063
+ invalid
1064
+ };
1065
+ });
833
1066
  };
834
1067
  const groups = SETTINGS_GROUPS.map((group) => renderGroup({
835
1068
  group,
836
- shown: (field) => displayValue(field, state.value, draft),
837
- disabled: !editable,
1069
+ t,
1070
+ shown: (field) => displayValue(field, state.value, draft.values),
1071
+ disabled: !editable || saving,
1072
+ revealed: (field) => revealed.includes(field.key),
1073
+ onReveal: (field) => {
1074
+ setRevealed((previous) => previous.includes(field.key) ? previous.filter((key) => key !== field.key) : [...previous, field.key]);
1075
+ },
838
1076
  onChange: onFieldChange
839
1077
  }));
840
- const actions = (0, react.createElement)("div", { className: "dss-actions" }, (0, react.createElement)("button", {
841
- type: "button",
842
- className: "dss-button",
843
- "data-primary": "true",
844
- disabled: ops.length === 0 || saving || !editable,
845
- onClick: () => {
1078
+ const actions = renderActions({
1079
+ t,
1080
+ saving,
1081
+ editable,
1082
+ blocked: draft.invalid.length > 0,
1083
+ changed: ops.length,
1084
+ onSave: () => {
846
1085
  save();
847
- }
848
- }, saving ? "保存中…" : "保存"), (0, react.createElement)("button", {
849
- type: "button",
850
- className: "dss-button",
851
- disabled: ops.length === 0 || saving,
852
- onClick: () => {
853
- setDraft({});
1086
+ },
1087
+ onUndo: () => {
1088
+ setDraft(EMPTY_DRAFT);
854
1089
  setFailure("");
855
1090
  }
856
- }, "撤销"));
1091
+ });
857
1092
  const status = (0, react.createElement)("p", {
858
1093
  className: "dss-status",
859
- "data-error": "false"
860
- }, statusLine(state, ops.length));
1094
+ "data-error": "false",
1095
+ role: "status",
1096
+ "aria-live": "polite"
1097
+ }, statusLine(t, state, ops.length, draft.invalid.length));
861
1098
  const reported = failure === "" ? null : (0, react.createElement)("p", {
862
1099
  className: "dss-status",
863
- "data-error": "true"
1100
+ "data-error": "true",
1101
+ role: "alert",
1102
+ "aria-live": "assertive"
864
1103
  }, failure);
865
1104
  return (0, react.createElement)("div", { className: "dss-card" }, status, reported, ...groups, actions);
866
1105
  }
@@ -869,8 +1108,9 @@ function SettingsCard(props) {
869
1108
  /**
870
1109
  * 需要专用结果行的工具。
871
1110
  *
872
- * 七个「面向用户」的工具各一行;会话的四个工具共用一组 key,但分散的 key 更省心 ——
873
- * slot 按 key 派发,漏掉一个只是那一个不画行,不会连累别的。
1111
+ * 每个工具一个 key:slot 按 key 派发,漏掉一个只是那一个不画行,不会连累别的。这份清单
1112
+ * 必须与 `host.ts` 的 `TOOL_SPECS` 对齐 —— 少登记一个,用户看到的就是宿主那条不带本包
1113
+ * 摘要信息的通用行。
874
1114
  */
875
1115
  const ROW_TOOLS = [
876
1116
  "scrapling_fetch",
@@ -878,6 +1118,7 @@ const ROW_TOOLS = [
878
1118
  "scrapling_render",
879
1119
  "scrapling_capture_xhr",
880
1120
  "scrapling_stealth_fetch",
1121
+ "scrapling_answer",
881
1122
  "scrapling_session_open",
882
1123
  "scrapling_session_fetch",
883
1124
  "scrapling_session_list",
@@ -919,7 +1160,7 @@ function buildRow(tool, args, meta) {
919
1160
  tool,
920
1161
  title: selector === "" ? tool : `${tool} ${selector}`,
921
1162
  fields: [{
922
- label: "匹配",
1163
+ labelKey: "row.matched",
923
1164
  value: text(projected["count"])
924
1165
  }]
925
1166
  };
@@ -928,7 +1169,7 @@ function buildRow(tool, args, meta) {
928
1169
  tool,
929
1170
  title: tool,
930
1171
  fields: [{
931
- label: "会话数",
1172
+ labelKey: "row.sessionCount",
932
1173
  value: text(projected["count"])
933
1174
  }]
934
1175
  };
@@ -936,10 +1177,10 @@ function buildRow(tool, args, meta) {
936
1177
  tool,
937
1178
  title: tool,
938
1179
  fields: [{
939
- label: "类型",
1180
+ labelKey: "row.type",
940
1181
  value: text(parameters["sessionType"])
941
1182
  }, {
942
- label: "会话",
1183
+ labelKey: "row.session",
943
1184
  value: text(projected["sessionId"])
944
1185
  }]
945
1186
  };
@@ -947,7 +1188,7 @@ function buildRow(tool, args, meta) {
947
1188
  tool,
948
1189
  title: tool,
949
1190
  fields: [{
950
- label: "会话",
1191
+ labelKey: "row.session",
951
1192
  value: text(parameters["sessionId"])
952
1193
  }]
953
1194
  };
@@ -956,32 +1197,33 @@ function buildRow(tool, args, meta) {
956
1197
  title: tool,
957
1198
  fields: [
958
1199
  {
959
- label: "起始",
1200
+ labelKey: "row.start",
960
1201
  value: url
961
1202
  },
962
1203
  {
963
- label: "上限",
1204
+ labelKey: "row.pageCap",
964
1205
  value: text(parameters["maxPages"])
965
1206
  },
966
1207
  {
967
- label: "作业",
1208
+ labelKey: "row.job",
968
1209
  value: text(projected["jobId"])
969
1210
  }
970
1211
  ]
971
1212
  };
972
1213
  const fields = [];
973
1214
  if (url !== "") fields.push({
974
- label: "URL",
1215
+ labelKey: "row.url",
975
1216
  value: url
976
1217
  });
977
1218
  const status = projected["statusCode"];
978
1219
  if (typeof status === "number") fields.push({
979
- label: "HTTP",
1220
+ labelKey: "row.http",
980
1221
  value: String(status)
981
1222
  });
982
1223
  if (projected["truncated"] === true) fields.push({
983
- label: "正文",
984
- value: "已截断"
1224
+ labelKey: "row.body",
1225
+ value: "",
1226
+ valueKey: "row.truncated"
985
1227
  });
986
1228
  return {
987
1229
  tool,
@@ -989,33 +1231,152 @@ function buildRow(tool, args, meta) {
989
1231
  fields
990
1232
  };
991
1233
  }
992
- /** 一行结果的渲染。 */
1234
+ /** 结果行用得到的参数键。视图是惰性的,只挑这几个读才不会物化整段参数 JSON。 */
1235
+ const ARG_KEYS = [
1236
+ "url",
1237
+ "selector",
1238
+ "sessionType",
1239
+ "sessionId",
1240
+ "maxPages"
1241
+ ];
1242
+ /**
1243
+ * 把宿主的参数视图摊成 buildRow 认得的普通对象。
1244
+ *
1245
+ * @param args - 宿主给的参数视图
1246
+ * @returns 普通对象;视图缺席时是空对象
1247
+ */
1248
+ function readArgs(args) {
1249
+ if (args === void 0) return {};
1250
+ const values = {};
1251
+ for (const key of ARG_KEYS) {
1252
+ const asValue = args.text(key) ?? args.value(key);
1253
+ if (asValue !== void 0) values[key] = asValue;
1254
+ }
1255
+ return values;
1256
+ }
1257
+ /** 结果行自己的样式。跟着宿主的语义色走,明暗两套主题都成立。 */
1258
+ const ROW_CSS = `
1259
+ .dsh-scrapling-row{display:flex;flex-direction:column;gap:2px;min-width:0}
1260
+ .dsh-scrapling-rowTitle{color:var(--dsw-alias-label-primary);font-size:13px;line-height:20px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
1261
+ .dsh-scrapling-rowFields{display:flex;flex-wrap:wrap;gap:2px 12px;margin:0;padding:0;list-style:none}
1262
+ .dsh-scrapling-rowField{display:flex;gap:4px;min-width:0;font-size:12px;line-height:18px}
1263
+ .dsh-scrapling-rowLabel{color:var(--dsw-alias-label-tertiary);flex:none}
1264
+ .dsh-scrapling-rowValue{color:var(--dsw-alias-label-secondary);overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
1265
+ .dsh-scrapling-rowButton{align-self:flex-start;border:0;background:0 0;padding:0;color:var(--dsw-alias-label-tertiary);font:inherit;font-size:12px;cursor:pointer}
1266
+ .dsh-scrapling-rowButton:hover{color:var(--dsw-alias-label-primary)}
1267
+ .dsh-scrapling-rowButton:focus-visible{outline:var(--dsw-focus-ring-width) solid var(--dsw-focus-ring-color,var(--dsw-alias-state-business-primary));outline-offset:1px}
1268
+ `;
1269
+ /**
1270
+ * 一行结果的渲染。
1271
+ *
1272
+ * 入参走 `block.args`,结果侧的投影走 `block.meta`;两者都按「可能还没到」处理 —— 调用刚发出
1273
+ * 时既没有 meta 也没有完整入参,这一行就只画标题。
1274
+ */
993
1275
  function RowView(props) {
1276
+ const { toolName, block, inspect, t } = props;
1277
+ const meta = block.phase === "result" ? block.meta : void 0;
1278
+ const spec = buildRow(toolName, readArgs(block.args), meta);
1279
+ const children = [(0, react.createElement)("div", {
1280
+ className: "dsh-scrapling-rowTitle",
1281
+ key: "title"
1282
+ }, spec.title)];
1283
+ if (spec.fields.length > 0) children.push((0, react.createElement)("ul", {
1284
+ className: "dsh-scrapling-rowFields",
1285
+ key: "fields"
1286
+ }, spec.fields.map((field) => (0, react.createElement)("li", {
1287
+ className: "dsh-scrapling-rowField",
1288
+ key: field.labelKey
1289
+ }, (0, react.createElement)("span", { className: "dsh-scrapling-rowLabel" }, t(field.labelKey)), (0, react.createElement)("span", { className: "dsh-scrapling-rowValue" }, field.valueKey ?? field.value)))));
1290
+ if (inspect !== void 0) children.push((0, react.createElement)("button", {
1291
+ type: "button",
1292
+ className: "dsh-scrapling-rowButton",
1293
+ key: "inspect",
1294
+ onClick: () => {
1295
+ inspect();
1296
+ }
1297
+ }, t("row.inspect")));
994
1298
  return (0, react.createElement)("div", {
995
1299
  className: "dsh-scrapling-row",
996
- "data-tool": props.tool
997
- }, props.tool);
1300
+ "data-tool": toolName
1301
+ }, ...children);
998
1302
  }
999
1303
  /** 本 Client 半注入的服务。 */
1000
1304
  const inject = ["slots"];
1001
1305
  /**
1306
+ * 给每个需要专用结果行的工具登记一行视图。
1307
+ *
1308
+ * @param scoped - 已就绪 slots 与 locale 的子上下文
1309
+ * @returns 注销全部登记的函数
1310
+ */
1311
+ function registerToolViews(scoped) {
1312
+ const t = scoped.locale.bind(LOCALE_NS);
1313
+ const disposers = [];
1314
+ for (const tool of ROW_TOOLS) disposers.push(scoped.slots.register({
1315
+ name: "tool.call.toolview",
1316
+ key: tool,
1317
+ locale: LOCALE_NS
1318
+ }, (props) => (0, react.createElement)(RowView, {
1319
+ ...props,
1320
+ t
1321
+ })));
1322
+ return () => {
1323
+ for (const dispose of disposers) dispose();
1324
+ };
1325
+ }
1326
+ /**
1002
1327
  * Client 插件入口:给每个工具注册一行结果视图,并把设置卡挂上插件详情页。
1003
1328
  *
1004
1329
  * @param ctx - 客户端上下文
1005
1330
  */
1006
1331
  function apply(ctx) {
1007
1332
  if (!e$1(ctx, "__dshScraplingToolviewApplied", "dsh-scrapling toolview")) return;
1008
- for (const tool of ROW_TOOLS) ctx.slots.inject("tool.call.toolview", () => {
1009
- ctx.slots.register({
1010
- name: "tool.call.toolview",
1011
- key: tool
1012
- }, () => (0, react.createElement)(RowView, { tool }));
1013
- });
1014
- ctx.slots.inject("plugins.bundle.config", () => {
1015
- ctx.slots.register({
1016
- name: "plugins.bundle.config",
1017
- key: BUNDLE_PACKAGE_NAME
1018
- }, (props) => (0, react.createElement)(SettingsCard, props));
1333
+ ctx.effect(() => {
1334
+ const doc = browserDocument();
1335
+ if (doc === void 0) return () => void 0;
1336
+ const disposeCard = installStyles(doc);
1337
+ const disposeRow = installStyle(doc, "dsh-scrapling-row-css", ROW_CSS);
1338
+ return () => {
1339
+ disposeCard();
1340
+ disposeRow();
1341
+ };
1342
+ }, "dsh-scrapling: client styles");
1343
+ ctx.inject(["locale"], (scoped) => {
1344
+ scoped.effect(() => {
1345
+ const dictionaries = localeDictionaries();
1346
+ const disposers = [ctx.locale.register(LOCALE_NS, "zh", dictionaries.zh), ctx.locale.register(LOCALE_NS, "en", dictionaries.en)];
1347
+ let disposeRows;
1348
+ scoped.slots.inject("tool.call.toolview", () => {
1349
+ disposeRows = registerToolViews(scoped);
1350
+ });
1351
+ return () => {
1352
+ disposeRows?.();
1353
+ for (const dispose of disposers) dispose();
1354
+ };
1355
+ }, "dsh-scrapling: dictionaries and tool rows");
1356
+ scoped.inject(["configForms"], (withForms) => {
1357
+ withForms.effect(() => {
1358
+ const form = ctx.configForms.get(SETTINGS_ENTRY_ID);
1359
+ const t = ctx.locale.bind(LOCALE_NS);
1360
+ let disposeCard;
1361
+ scoped.slots.inject("plugins.bundle.config", () => {
1362
+ disposeCard = scoped.slots.register({
1363
+ name: "plugins.bundle.config",
1364
+ key: BUNDLE_PACKAGE_NAME,
1365
+ locale: LOCALE_NS
1366
+ }, (props) => (0, react.createElement)(SettingsCard, {
1367
+ ...props,
1368
+ t,
1369
+ form: {
1370
+ state: form.getSnapshot(),
1371
+ mutate: (operations, revision) => form.mutate(operations, revision)
1372
+ }
1373
+ }));
1374
+ });
1375
+ return () => {
1376
+ disposeCard?.();
1377
+ };
1378
+ }, "dsh-scrapling: settings card");
1379
+ });
1019
1380
  });
1020
1381
  }
1021
1382
  //#endregion
@@ -1023,6 +1384,8 @@ exports.ROW_TOOLS = ROW_TOOLS;
1023
1384
  exports.apply = apply;
1024
1385
  exports.buildRow = buildRow;
1025
1386
  exports.inject = inject;
1387
+ exports.readArgs = readArgs;
1388
+
1026
1389
  return module.exports;
1027
1390
  },
1028
1391
  });