@saar-ai/web-widget 0.11.0 → 0.13.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/dist/capture-card/capture-card-dom.d.ts +52 -0
- package/dist/capture-card/capture-card-dom.d.ts.map +1 -0
- package/dist/capture-card/capture-card-host.d.ts +9 -0
- package/dist/capture-card/capture-card-host.d.ts.map +1 -0
- package/dist/chunks/capture-card-BlJ6EkWn.js +156 -0
- package/dist/chunks/capture-card-dom-BqDqOvQF.js +383 -0
- package/dist/chunks/{chat-CU9VPqVO.js → chat-R7z4DtMM.js} +110 -8
- package/dist/chunks/form-fill-B-h6Uw1t.js +45 -0
- package/dist/chunks/form-fill-dom-skU-ZwLB.js +191 -0
- package/dist/chunks/{host-widget-BZKCb7yR.js → host-widget-BZAYL5tQ.js} +1370 -425
- package/dist/chunks/{schemas-YN3wGSGZ.js → schemas-B2B3k74u.js} +2427 -1167
- package/dist/chunks/{telemetry-B1xBTk1s.js → telemetry-3f--vvdX.js} +18 -12
- package/dist/concierge-shell.css +1 -1
- package/dist/consent/consent-prompt-dom.d.ts +57 -0
- package/dist/consent/consent-prompt-dom.d.ts.map +1 -0
- package/dist/consent/consent-prompt-preview.d.ts +16 -0
- package/dist/consent/consent-prompt-preview.d.ts.map +1 -0
- package/dist/dock/dock-renderer.d.ts +1 -1
- package/dist/dock/dock-renderer.d.ts.map +1 -1
- package/dist/dock/dock-row.d.ts +3 -23
- package/dist/dock/dock-row.d.ts.map +1 -1
- package/dist/dock/dock.d.ts +11 -0
- package/dist/dock/dock.d.ts.map +1 -1
- package/dist/flow-link.d.ts +13 -0
- package/dist/flow-link.d.ts.map +1 -0
- package/dist/form-fill/form-fill-dom.d.ts +39 -0
- package/dist/form-fill/form-fill-dom.d.ts.map +1 -0
- package/dist/format-money.d.ts +3 -2
- package/dist/format-money.d.ts.map +1 -1
- package/dist/generated/concierge-shell-css.d.ts.map +1 -1
- package/dist/handoff-initials.d.ts +34 -0
- package/dist/handoff-initials.d.ts.map +1 -0
- package/dist/host-widget.d.ts +8 -5
- package/dist/host-widget.d.ts.map +1 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +139 -173
- package/dist/next/index.js +1 -1
- package/dist/use-chat-session.d.ts +23 -3
- package/dist/use-chat-session.d.ts.map +1 -1
- package/dist/use-voice-session.d.ts +39 -4
- package/dist/use-voice-session.d.ts.map +1 -1
- package/dist/voice-widget.d.ts +11 -5
- package/dist/voice-widget.d.ts.map +1 -1
- package/dist/widget/concierge-launcher.d.ts +16 -1
- package/dist/widget/concierge-launcher.d.ts.map +1 -1
- package/dist/widget/concierge-shell.d.ts +48 -2
- package/dist/widget/concierge-shell.d.ts.map +1 -1
- package/dist/widget/shell-renderer.d.ts +1 -1
- package/dist/widget/shell-renderer.d.ts.map +1 -1
- package/dist/widget/widget-view.d.ts +66 -2
- package/dist/widget/widget-view.d.ts.map +1 -1
- package/dist/widget-cart-summary.d.ts +4 -3
- package/dist/widget-cart-summary.d.ts.map +1 -1
- package/package.json +16 -16
- package/src/styles/brand.css +51 -0
- package/src/styles/concierge-shell-entry.css +13 -0
- package/src/styles/concierge.css +9 -38
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { a as
|
|
1
|
+
import { a as CAPTURE_CARD_INPUTS, i as CAPTURE_CARD_FIELD_PATTERN, n as CAPTURE_CARD_CONSENT_MAX, o as CAPTURE_CARD_REGION_PATTERN, r as CAPTURE_CARD_EVENT_TYPE, s as CAPTURE_CARD_VALUE_MAX, t as CAPTURE_CARD_CONSENT_CHANNELS } from "./capture-card-BlJ6EkWn.js";
|
|
2
|
+
import { a as isSafeFormSelector, i as isSafeFillValue, r as isSafeControlName, t as FILL_FORM_EVENT_TYPE } from "./form-fill-B-h6Uw1t.js";
|
|
3
|
+
import { a as httpUrl, c as number, d as string, f as union, i as discriminatedUnion, l as object, n as array, o as literal, p as uuid, r as boolean, t as _enum, u as record } from "./schemas-B2B3k74u.js";
|
|
2
4
|
//#region ../widget-protocol/dist/act.js
|
|
3
5
|
/**
|
|
4
6
|
* ACT EVENTS — the concierge moving the store, rather than only describing it.
|
|
@@ -177,6 +179,80 @@ var WidgetNavigateFailedReportSchema = object({
|
|
|
177
179
|
target: NavigateTargetSchema.optional()
|
|
178
180
|
}).strict();
|
|
179
181
|
//#endregion
|
|
182
|
+
//#region ../widget-protocol/dist/capture.js
|
|
183
|
+
/**
|
|
184
|
+
* Capture card wire schemas (zod half; the rules are in `./capture-card.ts`).
|
|
185
|
+
* The model supplies only a field key; label, purpose and choices are tenant
|
|
186
|
+
* text, built server-side and rendered as text. Version safety: the event is
|
|
187
|
+
* a plain object and an unknown `input` reads as `text`.
|
|
188
|
+
*/
|
|
189
|
+
/**
|
|
190
|
+
* Same rule as `@wesence/agent-core`'s `hasUnsafePromptText` (copied: no
|
|
191
|
+
* `@wesence/*` code may reach the widget bundle; a test in `apps/api` pins
|
|
192
|
+
* the two together). Refuses control/format (incl. bidi), separators,
|
|
193
|
+
* private-use, unassigned, variation selectors and tag characters; allows
|
|
194
|
+
* ZWJ/ZWNJ, LRM/RLM/ALM and U+FE0F after an emoji.
|
|
195
|
+
*/
|
|
196
|
+
var UNSAFE_CARD_CHARACTER = /[\p{Cc}\p{Cf}\p{Zl}\p{Zp}\p{Co}\p{Cn}\u{FE00}-\u{FE0F}\u{E0100}-\u{E01EF}]/u;
|
|
197
|
+
var ALLOWED_FORMAT_CHARACTERS = new Set([
|
|
198
|
+
8204,
|
|
199
|
+
8205,
|
|
200
|
+
8206,
|
|
201
|
+
8207,
|
|
202
|
+
1564
|
|
203
|
+
].map((codePoint) => String.fromCodePoint(codePoint)));
|
|
204
|
+
var EMOJI_PRESENTATION_SELECTOR = "️";
|
|
205
|
+
var EMOJI = /\p{Extended_Pictographic}/u;
|
|
206
|
+
/** @param text tenant wording: a label, a purpose or a choice */
|
|
207
|
+
function hasUnsafeCardText(text) {
|
|
208
|
+
let previous = "";
|
|
209
|
+
for (const character of text) {
|
|
210
|
+
if (!(ALLOWED_FORMAT_CHARACTERS.has(character) || character === EMOJI_PRESENTATION_SELECTOR && EMOJI.test(previous)) && UNSAFE_CARD_CHARACTER.test(character)) return true;
|
|
211
|
+
previous = character;
|
|
212
|
+
}
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
function cardText(max) {
|
|
216
|
+
return string().trim().min(1).max(max).refine((text) => !hasUnsafeCardText(text), "must not contain control or format characters");
|
|
217
|
+
}
|
|
218
|
+
var FieldKeySchema = string().regex(CAPTURE_CARD_FIELD_PATTERN);
|
|
219
|
+
/** The concierge asking the widget to show a card for one contact detail. */
|
|
220
|
+
var WidgetChatCaptureCardEventSchema = object({
|
|
221
|
+
type: literal(CAPTURE_CARD_EVENT_TYPE),
|
|
222
|
+
field: FieldKeySchema,
|
|
223
|
+
input: _enum(CAPTURE_CARD_INPUTS).catch("text"),
|
|
224
|
+
label: cardText(120),
|
|
225
|
+
purpose: cardText(500).optional(),
|
|
226
|
+
choices: array(cardText(120)).min(1).max(100).optional(),
|
|
227
|
+
maxLength: number().int().min(1).max(CAPTURE_CARD_VALUE_MAX).optional(),
|
|
228
|
+
/** A phone card only: the region a number without its country code is read in. */
|
|
229
|
+
region: string().regex(CAPTURE_CARD_REGION_PATTERN).optional().catch(void 0),
|
|
230
|
+
/**
|
|
231
|
+
* The tenant's consent wording for one outreach channel, shown beside an
|
|
232
|
+
* unticked checkbox. The server keeps which wording version this is; the
|
|
233
|
+
* widget only reports whether the box was ticked.
|
|
234
|
+
*/
|
|
235
|
+
consent: object({
|
|
236
|
+
channel: _enum(CAPTURE_CARD_CONSENT_CHANNELS),
|
|
237
|
+
text: cardText(CAPTURE_CARD_CONSENT_MAX)
|
|
238
|
+
}).strict().optional()
|
|
239
|
+
});
|
|
240
|
+
union([object({
|
|
241
|
+
field: FieldKeySchema,
|
|
242
|
+
value: string().min(1).max(CAPTURE_CARD_VALUE_MAX),
|
|
243
|
+
/** Whether the consent box was ticked; sent only for a card that showed one. */
|
|
244
|
+
consent: boolean().optional()
|
|
245
|
+
}).strict(), object({
|
|
246
|
+
field: FieldKeySchema,
|
|
247
|
+
refused: literal(true)
|
|
248
|
+
}).strict()]);
|
|
249
|
+
/** Sent after a typed answer so the concierge carries on. Never carries the value. */
|
|
250
|
+
var WidgetCaptureCardReportSchema = object({
|
|
251
|
+
kind: literal("capture_card"),
|
|
252
|
+
field: FieldKeySchema,
|
|
253
|
+
outcome: _enum(["saved", "declined"])
|
|
254
|
+
}).strict();
|
|
255
|
+
//#endregion
|
|
180
256
|
//#region ../widget-protocol/dist/currency.js
|
|
181
257
|
/**
|
|
182
258
|
* The ISO 4217 currency-code schema every currency field on this wire
|
|
@@ -208,6 +284,24 @@ var WidgetNavigateFailedReportSchema = object({
|
|
|
208
284
|
*/
|
|
209
285
|
var CURRENCY_CODE = string().regex(/^[A-Za-z]{3}$/, "must be a three-letter ISO 4217 currency code");
|
|
210
286
|
//#endregion
|
|
287
|
+
//#region ../widget-protocol/dist/form-fill-event.js
|
|
288
|
+
/**
|
|
289
|
+
* The `fill-form` widget event (zod half; the rules are in `./form-fill.ts`).
|
|
290
|
+
* Sent by the enquiry tool after an enquiry is recorded, when the tenant's
|
|
291
|
+
* published configuration maps the host page's own enquiry form.
|
|
292
|
+
*/
|
|
293
|
+
var WidgetFillFormFieldSchema = object({
|
|
294
|
+
name: string().refine(isSafeControlName, "must be a plain control name"),
|
|
295
|
+
value: string().refine(isSafeFillValue, "must be a bounded value without control characters")
|
|
296
|
+
}).strict();
|
|
297
|
+
/** The concierge asking the widget to fill, and maybe send, the host page's enquiry form. */
|
|
298
|
+
var WidgetChatFillFormEventSchema = object({
|
|
299
|
+
type: literal(FILL_FORM_EVENT_TYPE),
|
|
300
|
+
formSelector: string().refine(isSafeFormSelector, "must be a plain CSS selector"),
|
|
301
|
+
fields: array(WidgetFillFormFieldSchema).min(1).max(30),
|
|
302
|
+
submit: boolean()
|
|
303
|
+
}).strict();
|
|
304
|
+
//#endregion
|
|
211
305
|
//#region ../widget-protocol/dist/chat.js
|
|
212
306
|
/**
|
|
213
307
|
* The webchat SSE wire contract, and the sidecar `widgetEvent` a tool result
|
|
@@ -253,6 +347,9 @@ var CURRENCY_CODE = string().regex(/^[A-Za-z]{3}$/, "must be a three-letter ISO
|
|
|
253
347
|
* shopper's cart, for a shopper who asked for it in words rather than by
|
|
254
348
|
* pressing a card's button. The server never touches a cart itself — see
|
|
255
349
|
* {@link WidgetChatAddToCartEventSchema} for why that is structural.
|
|
350
|
+
* - `capture-card` — a small form for one contact detail (`./capture.ts`).
|
|
351
|
+
* - `fill-form` — fill, and maybe send, the host page's own enquiry form
|
|
352
|
+
* with the visitor's confirmed details (`./form-fill-event.ts`).
|
|
256
353
|
* - `done` — the turn is over. Carries the turn id so the widget can
|
|
257
354
|
* correlate a later `/widget/messages` page against what it already
|
|
258
355
|
* rendered inline.
|
|
@@ -553,6 +650,8 @@ var WidgetChatSseEventSchema = discriminatedUnion("type", [
|
|
|
553
650
|
WidgetChatCheckoutEventSchema,
|
|
554
651
|
WidgetChatAddToCartEventSchema,
|
|
555
652
|
...ACT_EVENT_SCHEMAS,
|
|
653
|
+
WidgetChatCaptureCardEventSchema,
|
|
654
|
+
WidgetChatFillFormEventSchema,
|
|
556
655
|
WidgetChatDoneEventSchema,
|
|
557
656
|
WidgetChatErrorEventSchema
|
|
558
657
|
]);
|
|
@@ -569,7 +668,9 @@ var WidgetToolEventSchema = discriminatedUnion("type", [
|
|
|
569
668
|
WidgetChatEscalatedEventSchema,
|
|
570
669
|
WidgetChatCheckoutEventSchema,
|
|
571
670
|
WidgetChatAddToCartEventSchema,
|
|
572
|
-
...ACT_EVENT_SCHEMAS
|
|
671
|
+
...ACT_EVENT_SCHEMAS,
|
|
672
|
+
WidgetChatCaptureCardEventSchema,
|
|
673
|
+
WidgetChatFillFormEventSchema
|
|
573
674
|
]);
|
|
574
675
|
/**
|
|
575
676
|
* ## Storefront reports — the OTHER direction, and why they are not messages
|
|
@@ -715,7 +816,8 @@ var WidgetChatReportSchema = discriminatedUnion("kind", [
|
|
|
715
816
|
WidgetCartAddFailedReportSchema,
|
|
716
817
|
WidgetSpotlightFailedReportSchema,
|
|
717
818
|
WidgetNavigateFailedReportSchema,
|
|
718
|
-
WidgetVariantChoiceRequestReportSchema
|
|
819
|
+
WidgetVariantChoiceRequestReportSchema,
|
|
820
|
+
WidgetCaptureCardReportSchema
|
|
719
821
|
]);
|
|
720
822
|
/**
|
|
721
823
|
* Parse a request body's `report` field. Never throws — the caller decides
|
|
@@ -884,11 +986,11 @@ function buildWidgetChatCardsEvent(curationId, items) {
|
|
|
884
986
|
* leg did not need is the only thing the spoken leg can have — and its
|
|
885
987
|
* staleness question is answered by pushing on every basket change while a
|
|
886
988
|
* call is live rather than by a timer. See `apps/api`'s
|
|
887
|
-
* `WidgetChatService.recordCart` and `@
|
|
888
|
-
* `
|
|
889
|
-
*
|
|
890
|
-
*
|
|
891
|
-
* browser bundle
|
|
989
|
+
* `WidgetChatService.recordCart` and `@saar-ai/commerce-pack`'s
|
|
990
|
+
* `shopper-cart.ts`, which is where the RENDERING both legs share now lives
|
|
991
|
+
* (it moved there from `@wesence/agent-core` under ADR 0022 D11; the control
|
|
992
|
+
* tier renders the call's block too). This file still owns the WIRE shape; a
|
|
993
|
+
* renderer does not belong in a browser bundle.
|
|
892
994
|
*
|
|
893
995
|
* ### Bounded, because a request must be
|
|
894
996
|
*
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
//#region ../widget-protocol/dist/form-fill.js
|
|
2
|
+
/**
|
|
3
|
+
* Rules for the `fill-form` widget event, shared by the server that builds it
|
|
4
|
+
* and the widget's lazy form-fill chunk. Must stay zod-free and import-free:
|
|
5
|
+
* the chunk may not pull zod.
|
|
6
|
+
*
|
|
7
|
+
* The event asks the widget to put the visitor's own enquiry details into the
|
|
8
|
+
* host page's enquiry form and, when the page has no bot protection, to send
|
|
9
|
+
* it. Nothing here is model prose: the form and the control names come from
|
|
10
|
+
* the tenant's published configuration, the values from what the visitor
|
|
11
|
+
* confirmed.
|
|
12
|
+
*/
|
|
13
|
+
/** The `type` of the widget event that fills a host form. */
|
|
14
|
+
var FILL_FORM_EVENT_TYPE = "fill-form";
|
|
15
|
+
/**
|
|
16
|
+
* The attribute the widget puts on a form it filled. `@saar-ai/widget-bridge`
|
|
17
|
+
* restates it as `FORM_FILLED_ATTRIBUTE` (its form-submit detector skips a
|
|
18
|
+
* marked form) and a test in `@saar-ai/web-widget` pins the two together.
|
|
19
|
+
*/
|
|
20
|
+
var FILL_FORM_MARKER_ATTRIBUTE = "data-enquiry-filled";
|
|
21
|
+
/**
|
|
22
|
+
* A CSS selector made of plain selector characters only: tag, id, class and
|
|
23
|
+
* attribute selectors, combinators and pseudo-classes. No backslash escapes,
|
|
24
|
+
* braces, semicolons, angle brackets or line breaks, so the value cannot be
|
|
25
|
+
* mistaken for markup or a style block wherever it is logged or shown.
|
|
26
|
+
*/
|
|
27
|
+
var FILL_FORM_SELECTOR_PATTERN = /^[A-Za-z0-9_\-#.:[\]="' >+~*^$|(),]+$/;
|
|
28
|
+
/** A form control's `name` attribute, as builders write them (`your-email`, `fields[email]`). */
|
|
29
|
+
var FILL_FORM_CONTROL_NAME_PATTERN = /^[A-Za-z0-9_\-.:[\]]{1,100}$/;
|
|
30
|
+
/** Control characters other than tab, line feed and carriage return. */
|
|
31
|
+
var UNSAFE_VALUE = /[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F]/;
|
|
32
|
+
/** @param value a candidate form selector */
|
|
33
|
+
function isSafeFormSelector(value) {
|
|
34
|
+
return typeof value === "string" && value.trim().length > 0 && value.length <= 200 && FILL_FORM_SELECTOR_PATTERN.test(value);
|
|
35
|
+
}
|
|
36
|
+
/** @param value a candidate control `name` */
|
|
37
|
+
function isSafeControlName(value) {
|
|
38
|
+
return typeof value === "string" && FILL_FORM_CONTROL_NAME_PATTERN.test(value);
|
|
39
|
+
}
|
|
40
|
+
/** @param value a candidate control value */
|
|
41
|
+
function isSafeFillValue(value) {
|
|
42
|
+
return typeof value === "string" && value.length > 0 && value.length <= 5e3 && !UNSAFE_VALUE.test(value);
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
export { isSafeFormSelector as a, isSafeFillValue as i, FILL_FORM_MARKER_ATTRIBUTE as n, isSafeControlName as r, FILL_FORM_EVENT_TYPE as t };
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { a as isSafeFormSelector, i as isSafeFillValue, n as FILL_FORM_MARKER_ATTRIBUTE, r as isSafeControlName } from "./form-fill-B-h6Uw1t.js";
|
|
2
|
+
//#region src/form-fill/form-fill-dom.ts
|
|
3
|
+
/**
|
|
4
|
+
* Fill, and maybe submit, the host page's enquiry form, in its own lazy chunk
|
|
5
|
+
* (`form-fill.mjs`). Must import only `@saar-ai/widget-protocol/form-fill` so
|
|
6
|
+
* the chunk is byte-identical in both UI builds (a test pins this). Never
|
|
7
|
+
* submits past bot protection (ADR 0020 D14). Behaviour: AGENTS.md, "Host form
|
|
8
|
+
* fill".
|
|
9
|
+
*/
|
|
10
|
+
var PROTECTION_MARKS = [
|
|
11
|
+
["recaptcha", ".g-recaptcha,.grecaptcha-badge,textarea[name=\"g-recaptcha-response\"],iframe[src*=\"recaptcha\"],script[src*=\"recaptcha\"]"],
|
|
12
|
+
["hcaptcha", ".h-captcha,textarea[name=\"h-captcha-response\"],iframe[src*=\"hcaptcha.com\"],script[src*=\"hcaptcha.com\"]"],
|
|
13
|
+
["turnstile", ".cf-turnstile,input[name=\"cf-turnstile-response\"],iframe[src*=\"challenges.cloudflare.com\"],script[src*=\"challenges.cloudflare.com\"]"],
|
|
14
|
+
["friendly-captcha", ".frc-captcha,script[src*=\"friendlycaptcha\"]"],
|
|
15
|
+
["sitekey", "[data-sitekey]"]
|
|
16
|
+
];
|
|
17
|
+
var PROTECTION_GLOBALS = [
|
|
18
|
+
["recaptcha", "grecaptcha"],
|
|
19
|
+
["hcaptcha", "hcaptcha"],
|
|
20
|
+
["turnstile", "turnstile"],
|
|
21
|
+
["friendly-captcha", "friendlyChallenge"]
|
|
22
|
+
];
|
|
23
|
+
var SENSITIVE_CONTROL = "input[type=\"password\"],input[type=\"file\"],[autocomplete^=\"cc-\"]";
|
|
24
|
+
var FILLABLE_INPUT_TYPES = new Set([
|
|
25
|
+
"",
|
|
26
|
+
"text",
|
|
27
|
+
"email",
|
|
28
|
+
"tel",
|
|
29
|
+
"url",
|
|
30
|
+
"number",
|
|
31
|
+
"search"
|
|
32
|
+
]);
|
|
33
|
+
/**
|
|
34
|
+
* The CAPTCHA on this page, if any. The whole document is checked, not only
|
|
35
|
+
* the form: an invisible reCAPTCHA v3 lives in a script and a badge, and
|
|
36
|
+
* attaches to the submit from outside the form.
|
|
37
|
+
*/
|
|
38
|
+
function detectBotProtection(doc, form) {
|
|
39
|
+
for (const [kind, selector] of PROTECTION_MARKS) try {
|
|
40
|
+
if (form?.querySelector(selector) || doc.querySelector(selector)) return kind;
|
|
41
|
+
} catch {}
|
|
42
|
+
const view = doc.defaultView;
|
|
43
|
+
for (const [kind, name] of PROTECTION_GLOBALS) if (view && view[name] !== void 0 && view[name] !== null) return kind;
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
/** Hidden by attribute or by CSS, on the control or any ancestor: a honeypot looks like this. */
|
|
47
|
+
function hidden(element) {
|
|
48
|
+
if (element.closest("[hidden],[aria-hidden=\"true\"]") !== null) return true;
|
|
49
|
+
const check = element.checkVisibility;
|
|
50
|
+
if (typeof check === "function") return !check.call(element, { checkVisibilityCSS: true });
|
|
51
|
+
const view = element.ownerDocument.defaultView;
|
|
52
|
+
for (let node = element; node !== null; node = node.parentElement) {
|
|
53
|
+
const style = view?.getComputedStyle(node);
|
|
54
|
+
if (style && (style.display === "none" || style.visibility === "hidden")) return true;
|
|
55
|
+
}
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
function setNativeValue(element, value) {
|
|
59
|
+
let proto = Object.getPrototypeOf(element);
|
|
60
|
+
while (proto !== null) {
|
|
61
|
+
const setter = Object.getOwnPropertyDescriptor(proto, "value")?.set;
|
|
62
|
+
if (setter) {
|
|
63
|
+
setter.call(element, value);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
proto = Object.getPrototypeOf(proto);
|
|
67
|
+
}
|
|
68
|
+
element.value = value;
|
|
69
|
+
}
|
|
70
|
+
function announce(element, types) {
|
|
71
|
+
const EventCtor = element.ownerDocument.defaultView?.Event ?? Event;
|
|
72
|
+
for (const type of types) element.dispatchEvent(new EventCtor(type, { bubbles: true }));
|
|
73
|
+
}
|
|
74
|
+
function fillOne(control, value) {
|
|
75
|
+
if (hidden(control)) return false;
|
|
76
|
+
const tag = control.tagName;
|
|
77
|
+
if (tag === "TEXTAREA" || tag === "INPUT") {
|
|
78
|
+
const field = control;
|
|
79
|
+
if (field.disabled || field.readOnly) return false;
|
|
80
|
+
if (tag === "INPUT" && !FILLABLE_INPUT_TYPES.has((control.getAttribute("type") ?? "").toLowerCase())) return false;
|
|
81
|
+
setNativeValue(field, value);
|
|
82
|
+
announce(field, ["input", "change"]);
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
if (tag === "SELECT") {
|
|
86
|
+
const select = control;
|
|
87
|
+
if (select.disabled) return false;
|
|
88
|
+
const wanted = value.trim().toLowerCase();
|
|
89
|
+
const index = Array.prototype.findIndex.call(select.options, (option) => option.value.toLowerCase() === wanted || option.text.trim().toLowerCase() === wanted);
|
|
90
|
+
if (index < 0) return false;
|
|
91
|
+
select.selectedIndex = index;
|
|
92
|
+
announce(select, ["input", "change"]);
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
function fillByName(form, name, value) {
|
|
98
|
+
let item;
|
|
99
|
+
try {
|
|
100
|
+
item = form.elements.namedItem(name);
|
|
101
|
+
} catch {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
if (item === null) return false;
|
|
105
|
+
if (!("tagName" in item)) {
|
|
106
|
+
for (const node of Array.from(item)) {
|
|
107
|
+
const radio = node;
|
|
108
|
+
if (radio.type !== "radio" || radio.disabled || hidden(radio)) continue;
|
|
109
|
+
if (radio.value.toLowerCase() === value.trim().toLowerCase()) {
|
|
110
|
+
radio.checked = true;
|
|
111
|
+
announce(radio, ["input", "change"]);
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
return fillOne(item, value);
|
|
118
|
+
}
|
|
119
|
+
function handBack(form) {
|
|
120
|
+
try {
|
|
121
|
+
form.scrollIntoView?.({
|
|
122
|
+
block: "center",
|
|
123
|
+
behavior: "smooth"
|
|
124
|
+
});
|
|
125
|
+
(Array.from(form.querySelectorAll("[required]")).find((el) => !hidden(el) && !el.value) ?? form.querySelector("button[type=\"submit\"],input[type=\"submit\"],button:not([type])"))?.focus?.({ preventScroll: true });
|
|
126
|
+
} catch {}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Fill the form `request` names and, when allowed, submit it.
|
|
130
|
+
*
|
|
131
|
+
* Never throws into the host. Submits with `requestSubmit`, so the browser's
|
|
132
|
+
* own validation and the page's own submit handlers run exactly as when the
|
|
133
|
+
* visitor presses the button; a browser without it is handed back rather than
|
|
134
|
+
* posting with `submit()`, which would skip both.
|
|
135
|
+
*/
|
|
136
|
+
function fillHostForm(doc, request) {
|
|
137
|
+
const none = (outcome) => ({
|
|
138
|
+
outcome,
|
|
139
|
+
filled: 0,
|
|
140
|
+
botProtection: null
|
|
141
|
+
});
|
|
142
|
+
try {
|
|
143
|
+
if (!isSafeFormSelector(request.formSelector)) return none("not-found");
|
|
144
|
+
let found = null;
|
|
145
|
+
try {
|
|
146
|
+
found = doc.querySelector(request.formSelector);
|
|
147
|
+
} catch {
|
|
148
|
+
return none("not-found");
|
|
149
|
+
}
|
|
150
|
+
if (found === null || found.tagName !== "FORM" || hidden(found)) return none("not-found");
|
|
151
|
+
const form = found;
|
|
152
|
+
if (form.closest("[role=\"search\"]") || form.querySelector(SENSITIVE_CONTROL)) return none("refused");
|
|
153
|
+
let filled = 0;
|
|
154
|
+
let wanted = 0;
|
|
155
|
+
for (const field of request.fields) {
|
|
156
|
+
if (!isSafeControlName(field.name) || !isSafeFillValue(field.value)) continue;
|
|
157
|
+
wanted += 1;
|
|
158
|
+
if (fillByName(form, field.name, field.value)) filled += 1;
|
|
159
|
+
}
|
|
160
|
+
if (filled === 0) return none("not-found");
|
|
161
|
+
form.setAttribute(FILL_FORM_MARKER_ATTRIBUTE, "");
|
|
162
|
+
const botProtection = detectBotProtection(doc, form);
|
|
163
|
+
if (botProtection !== null || !request.submit || filled < wanted || typeof form.requestSubmit !== "function") {
|
|
164
|
+
handBack(form);
|
|
165
|
+
return {
|
|
166
|
+
outcome: "needs-visitor",
|
|
167
|
+
filled,
|
|
168
|
+
botProtection
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
if (!form.checkValidity()) {
|
|
172
|
+
form.reportValidity();
|
|
173
|
+
handBack(form);
|
|
174
|
+
return {
|
|
175
|
+
outcome: "needs-visitor",
|
|
176
|
+
filled,
|
|
177
|
+
botProtection: null
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
form.requestSubmit();
|
|
181
|
+
return {
|
|
182
|
+
outcome: "submitted",
|
|
183
|
+
filled,
|
|
184
|
+
botProtection: null
|
|
185
|
+
};
|
|
186
|
+
} catch {
|
|
187
|
+
return none("not-found");
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
//#endregion
|
|
191
|
+
export { fillHostForm };
|