UNPKG

histoire

Version:

A fast story book powered by Vite

34 lines (33 loc) • 2.4 kB
/** Self-contained function serialized by Playwright into the owned host page. */ export function requestPreviewAccessibility(options) { return new Promise((resolve) => { const state = window.__HST_MCP_PREVIEW__; const frame = document.querySelector('#histoire-mcp-preview'); const current = () => state?.active && state.ready && state.nonce === options.nonce && state.epoch === options.epoch && state.storyId === options.storyId && state.variantId === options.variantId && state.documentId === options.documentId && state.currentDocumentId() === options.documentId; let timer; /** Remove the listener before delivering a terminal response. */ function finish(value) { clearTimeout(timer); window.removeEventListener('message', receive); resolve(value); } /** Accept only the captured frame, origin, request and document tuple. */ function receive(event) { const data = event.data; if (event.source !== frame?.contentWindow || event.origin !== window.location.origin || !data?.__histoire || data.type !== options.resultType || data.requestId !== options.id) return; if (!current() || data.documentId !== options.documentId || data.storyId !== options.storyId || data.variantId !== options.variantId) return finish({ error: 'RUNTIME_CHANGED' }); if (data.error) return finish({ error: typeof data.error.code === 'string' ? data.error.code : 'INTERNAL_ERROR' }); if (!data.result || typeof data.result !== 'object' || Array.isArray(data.result)) return finish({ error: 'INTERNAL_ERROR' }); finish({ result: data.result }); } if (!current() || !frame?.contentWindow) return finish({ error: 'RUNTIME_CHANGED' }); window.addEventListener('message', receive); timer = setTimeout(() => finish({ error: 'TIMEOUT' }), options.timeoutMs); frame.contentWindow.postMessage({ __histoire: true, type: options.requestType, command: 'inspection.accessibility', inspection: true, payload: { selector: options.selector }, requestId: options.id, documentId: options.documentId, storyId: options.storyId, variantId: options.variantId, mcpNonce: options.nonce, mcpEpoch: options.epoch }, window.location.origin); }); }