Adds the boolean field consumed by the upcoming Shadow DOM mount path. Strict equality with the string "true" — anything else (absent, "false", "1", "yes", empty) yields false, so accidental opt-in is impossible. Tests cover the full parseConfig surface (required fields, server-url derivation, position default) plus the new shadow attribute parsing. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
export type WidgetConfig = {
|
|
botId: string;
|
|
apiKey: string;
|
|
serverUrl: string;
|
|
position?: 'bottom-right' | 'bottom-left';
|
|
shadow: boolean;
|
|
};
|
|
|
|
export function parseConfig(): WidgetConfig | null {
|
|
const script =
|
|
(document.currentScript as HTMLScriptElement | null) ??
|
|
(document.querySelector(
|
|
'script[data-bot-id][data-api-key]',
|
|
) as HTMLScriptElement | null);
|
|
if (!script) return null;
|
|
|
|
const botId = script.getAttribute('data-bot-id');
|
|
const apiKey = script.getAttribute('data-api-key');
|
|
const serverUrl =
|
|
script.getAttribute('data-server-url') ?? deriveServerFromSrc(script.src);
|
|
const position =
|
|
(script.getAttribute('data-position') as
|
|
| 'bottom-right'
|
|
| 'bottom-left'
|
|
| null) ?? 'bottom-right';
|
|
const shadow = script.getAttribute('data-shadow') === 'true';
|
|
|
|
if (!botId || !apiKey) return null;
|
|
|
|
return { botId, apiKey, serverUrl, position, shadow };
|
|
}
|
|
|
|
function deriveServerFromSrc(src: string): string {
|
|
try {
|
|
return new URL(src).origin;
|
|
} catch {
|
|
return 'https://messenger-bot.mind4solutions.cloud';
|
|
}
|
|
}
|