@qaiddev/quests-embed 1.10.1 → 1.11.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/README.md +56 -8
- package/dist/embed-loader.d.ts +6 -0
- package/dist/embed.js +1 -1
- package/dist/embed.js.map +1 -1
- package/dist/qaid-quests.js +183 -146
- package/dist/qaid-quests.js.map +1 -1
- package/dist/qaid-quests.umd.cjs +3 -3
- package/dist/qaid-quests.umd.cjs.map +1 -1
- package/dist/svg.d.ts +18 -0
- package/dist/trusted-types.d.ts +29 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -49,16 +49,16 @@ The embed auto-initializes when it detects a `data-endpoint` attribute on its sc
|
|
|
49
49
|
|
|
50
50
|
### What loads when
|
|
51
51
|
|
|
52
|
-
Sizes are gzipped, for 1.10.
|
|
52
|
+
Sizes are gzipped, for 1.10.
|
|
53
53
|
|
|
54
|
-
| When | Size | What it brings |
|
|
55
|
-
|
|
56
|
-
| Before paint | 0 KB | nothing; the tag waits (`defer`) |
|
|
57
|
-
| After paint | 0.8 KB | the tag (`embed.js`) |
|
|
58
|
-
| Quest near the screen, or browser idle | 16 KB | the quest code |
|
|
59
|
-
| Same moment | +1 KB | the questions and theme, in one request |
|
|
54
|
+
| When | Size | What it brings |
|
|
55
|
+
|------|------|----------------|
|
|
56
|
+
| Before paint | 0 KB | nothing; the tag waits (`defer`) |
|
|
57
|
+
| After paint | 0.8 KB | the tag (`embed.js`) |
|
|
58
|
+
| Quest near the screen, or browser idle | 16 KB | the quest code |
|
|
59
|
+
| Same moment | +1 KB | the questions and theme, in one request |
|
|
60
60
|
|
|
61
|
-
The
|
|
61
|
+
The questions come from your server, so they finish a little after the code even though they start together.
|
|
62
62
|
|
|
63
63
|
### JSON Config (Script Tag)
|
|
64
64
|
|
|
@@ -92,6 +92,54 @@ For complex configurations or inline questionnaires, use a separate JSON config
|
|
|
92
92
|
<script src="https://unpkg.com/@qaiddev/quests-embed/dist/embed.js" defer></script>
|
|
93
93
|
```
|
|
94
94
|
|
|
95
|
+
## Security: pinning, integrity and CSP
|
|
96
|
+
|
|
97
|
+
The script runs on your page with your page's rights, so treat it like any other third-party script: pin it, hash it, and fence it in.
|
|
98
|
+
|
|
99
|
+
### Pin a version and check its hash
|
|
100
|
+
|
|
101
|
+
```html
|
|
102
|
+
<script
|
|
103
|
+
src="https://unpkg.com/@qaiddev/quests-embed@1.11.0/dist/embed.js"
|
|
104
|
+
integrity="sha384-DkDosPq0xXgb/fPj+/Cq4KGo1onTdfUBLuKeqCR9BYFQupqNLQlzlQKoHLXUqbsU"
|
|
105
|
+
crossorigin="anonymous"
|
|
106
|
+
defer
|
|
107
|
+
data-endpoint="https://qaid.dev/api/responses"
|
|
108
|
+
data-api-key="YOUR_API_KEY"
|
|
109
|
+
data-config-url="https://qaid.dev/api/quests/QUEST_ID/definition"
|
|
110
|
+
data-container="#form"
|
|
111
|
+
></script>
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
- The `@1.11.0` pins the release. Without it unpkg serves the newest release, and a hash can never match for long.
|
|
115
|
+
- `integrity` is the sha384 of `dist/embed.js` for that release. It is printed in each release's notes; to work it out yourself: `curl -s https://unpkg.com/@qaiddev/quests-embed@1.11.0/dist/embed.js | openssl dgst -sha384 -binary | openssl base64 -A`.
|
|
116
|
+
- When the tag carries `integrity`, `embed.js` gives the bundle it loads (`qaid-quests.umd.cjs`) that file's own sha384, baked in when the release was built. Your one hash covers every byte of the embed's JavaScript: if either file on the CDN changes, the browser refuses to run it.
|
|
117
|
+
- `crossorigin="anonymous"` is required for the browser to check a hash on a file from another origin.
|
|
118
|
+
|
|
119
|
+
### Content Security Policy
|
|
120
|
+
|
|
121
|
+
A policy that lets the embed work and nothing else from its side:
|
|
122
|
+
|
|
123
|
+
```
|
|
124
|
+
script-src 'self' https://unpkg.com/@qaiddev/quests-embed@1.11.0/;
|
|
125
|
+
style-src 'self' 'unsafe-inline';
|
|
126
|
+
connect-src 'self' https://qaid.dev;
|
|
127
|
+
img-src 'self' data: https:;
|
|
128
|
+
trusted-types qaid-embed;
|
|
129
|
+
require-trusted-types-for 'script';
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
- `script-src`: the exact versioned folder, not all of unpkg. Self-host and `'self'` is enough.
|
|
133
|
+
- `style-src 'unsafe-inline'`: the embed styles itself with `<style>` elements inside its shadow root, which CSP treats as inline styles.
|
|
134
|
+
- `connect-src`: the origin of `data-endpoint` and `data-config-url` (and `data-theme-url`, if you use one).
|
|
135
|
+
- `img-src`: option images in a questionnaire load from wherever their URLs point; narrow `https:` to your image hosts if you can.
|
|
136
|
+
- `trusted-types qaid-embed`: on a page that sets `require-trusted-types-for 'script'`, the embed sets the bundle's `src` through one Trusted Types policy named `qaid-embed`, which signs only the URL the embed worked out itself. List it in your `trusted-types` directive, or the embed prints a warning and does not load. thumbs-embed uses the same policy name, so one entry covers both.
|
|
137
|
+
- The embed writes no HTML strings (`innerHTML`): its icons are built as DOM nodes.
|
|
138
|
+
|
|
139
|
+
### Self-hosting the files
|
|
140
|
+
|
|
141
|
+
Copy `dist/embed.js` and `dist/qaid-quests.umd.cjs` from the npm package into one folder on your own origin and point the tag at that `embed.js`. It loads the bundle from the same folder. The integrity rules above still apply, with your own URLs.
|
|
142
|
+
|
|
95
143
|
## How It Works
|
|
96
144
|
|
|
97
145
|
1. The embed loads the questionnaire — either inline via `questionnaire`, or fetched from `configUrl`
|
package/dist/embed-loader.d.ts
CHANGED
|
@@ -27,7 +27,13 @@
|
|
|
27
27
|
* data-endpoint="/api/responses" data-config-url="/forms/intake.json"
|
|
28
28
|
* data-container="#quest-mount"></script>
|
|
29
29
|
*
|
|
30
|
+
* Pinned with Subresource Integrity (`integrity="sha384-…"` on this tag), it
|
|
31
|
+
* gives the bundle its own sha384, baked in at build time
|
|
32
|
+
* (vite.config.embed.ts), so the customer's one hash covers every byte of
|
|
33
|
+
* the embed's JavaScript.
|
|
34
|
+
*
|
|
30
35
|
* IMPORTANT: this file must stay ES5-clean and dependency-free, like the
|
|
31
36
|
* thumbs-embed loader it mirrors: it is the first thing to run on every
|
|
32
37
|
* browser. Written with var/function on purpose.
|
|
33
38
|
*/
|
|
39
|
+
export {};
|
package/dist/embed.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
(function(){"use strict";(function(){if(typeof document=="undefined")return;function
|
|
1
|
+
(function(){"use strict";var m="qaid-embed",l="__qaidEmbedTrustedTypes";function p(){if(typeof window=="undefined")return null;var c=window,n=c.trustedTypes;if(!n||typeof n.createPolicy!="function")return null;var a=c[l];if(a)return a;var f=Object.create(null),d=null;try{d=n.createPolicy(m,{createScriptURL:function(u){if(f[u]===!0)return u;throw new TypeError("qaid-embed: refused script URL "+u)}})}catch(u){d=null}var o={policy:d,allowed:f};try{Object.defineProperty(c,l,{value:o,configurable:!0})}catch(u){}return o}function y(c){var n=p();return!n||!n.policy?c:(n.allowed[c]=!0,n.policy.createScriptURL(c))}(function(){if(typeof document=="undefined")return;function c(){var e=document.currentScript;if(e&&e.src)return e;for(var t=document.getElementsByTagName("script"),r=t.length-1;r>=0;r--){var i=t[r].src||"";if(i.indexOf("quests-embed")!==-1&&/\/embed(\.min)?\.js(\?|#|$)/.test(i))return t[r]}return null}var n=c();if(!n||!n.src)return;var a=n,f=a.src.substring(0,a.src.lastIndexOf("/")+1),d=!1;function o(){if(!d){d=!0;for(var e=document.createElement("script"),t=a.attributes,r=0;r<t.length;r++){var i=t[r];i.name.indexOf("data-")===0&&e.setAttribute(i.name,i.value)}var s=f+"qaid-quests.umd.cjs",g="sha384-zPrwGUtfrEt32yl5R+bdmpEVs4ZDzSt13UUACLbAwOZRM4sAmIlibldMbyj6mPA2";a.getAttribute("integrity")&&(e.integrity=g,e.crossOrigin="anonymous");try{e.src=y(s)}catch(w){typeof console!="undefined"&&console.warn("[quests-embed] Trusted Types blocked "+s+". Add `trusted-types qaid-embed` to the page's CSP.");return}(document.head||document.documentElement).appendChild(e),u(a.getAttribute("data-config-url")),u(a.getAttribute("data-theme-url"))}}function u(e){if(!(!e||typeof fetch!="function")){var t=window,r=t.__qaidQuestsPrefetch||(t.__qaidQuestsPrefetch={});if(!r[e]){var i=fetch(e,{headers:{Accept:"application/json"}});i.catch(function(){}),r[e]=i}}}function b(){function e(){var t=window;typeof t.requestIdleCallback=="function"?t.requestIdleCallback(o,{timeout:3e3}):setTimeout(o,200)}document.readyState==="complete"?e():window.addEventListener("load",e)}function v(){var e=a.getAttribute("data-container"),t=e?document.querySelector(e):null;if(!t||typeof IntersectionObserver=="undefined"){o();return}var r=new IntersectionObserver(function(i){for(var s=0;s<i.length;s++)if(i[s].isIntersecting){r.disconnect(),o();return}},{rootMargin:"100% 0px"});r.observe(t),b()}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",v):v()})()})();
|
|
2
2
|
//# sourceMappingURL=embed.js.map
|
package/dist/embed.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"embed.js","sources":["../src/embed-loader.ts"],"sourcesContent":["/**\n * Single-tag embed loader (`dist/embed.js`).\n *\n * A tiny classic `<script>` that holds back the quest code (the UMD bundle,\n * `qaid-quests.umd.cjs`) until it is needed, so a quest further down the page\n * costs the page nothing while it loads. The bundle is injected when the first\n * of these happens:\n *\n * - the quest's container comes within a screen's height of the viewport;\n * - the page has finished loading and the browser is idle, so the quest is\n * ready before the visitor scrolls to it;\n * - right away, when there is no container to watch (the modal install) or\n * the browser has no IntersectionObserver.\n *\n * When it injects the bundle it also starts fetching the quest's questions\n * (`data-config-url`) and theme (`data-theme-url`), so they download beside\n * the code instead of after it. The bundle picks the requests up from\n * `window.__qaidQuestsPrefetch` (see prefetch.ts).\n *\n * The injected bundle initializes itself from the `data-*` attributes copied\n * across from this tag (or from a `<script type=\"application/json\">` config\n * block already on the page), exactly as if the page had loaded it directly.\n *\n * Usage:\n * <div id=\"quest-mount\"></div>\n * <script src=\"https://unpkg.com/@qaiddev/quests-embed/dist/embed.js\" defer\n * data-endpoint=\"/api/responses\" data-config-url=\"/forms/intake.json\"\n * data-container=\"#quest-mount\"></script>\n *\n * IMPORTANT: this file must stay ES5-clean and dependency-free, like the\n * thumbs-embed loader it mirrors: it is the first thing to run on every\n * browser. Written with var/function on purpose.\n */\n\n(function () {\n if (typeof document === \"undefined\") return;\n\n // Locate this script tag: prefer document.currentScript (valid for a plain\n // classic script), else find the last <script> whose src looks like embed.js.\n function findSelf(): HTMLScriptElement | null {\n var cur = document.currentScript as HTMLScriptElement | null;\n if (cur && cur.src) return cur;\n var scripts = document.getElementsByTagName(\"script\");\n for (var i = scripts.length - 1; i >= 0; i--) {\n var src = scripts[i].src || \"\";\n if (src.indexOf(\"quests-embed\") !== -1 && /\\/embed(\\.min)?\\.js(\\?|#|$)/.test(src)) {\n return scripts[i];\n }\n }\n return null;\n }\n\n var self = findSelf();\n if (!self || !self.src) return;\n var tag = self;\n\n // Base = the directory that holds embed.js, e.g.\n // \"https://unpkg.com/@qaiddev/quests-embed@1.9.0/dist/\", so the bundle comes\n // from the same (CDN-versioned) place.\n var base = tag.src.substring(0, tag.src.lastIndexOf(\"/\") + 1);\n\n var loaded = false;\n function load(): void {\n if (loaded) return;\n loaded = true;\n var entry = document.createElement(\"script\");\n // Carry the config across so the bundle's auto-init sees it.\n var attrs = tag.attributes;\n for (var j = 0; j < attrs.length; j++) {\n var a = attrs[j];\n if (a.name.indexOf(\"data-\") === 0) entry.setAttribute(a.name, a.value);\n }\n entry.src = base + \"qaid-quests.umd.cjs\";\n (document.head || document.documentElement).appendChild(entry);\n prefetch(tag.getAttribute(\"data-config-url\"));\n prefetch(tag.getAttribute(\"data-theme-url\"));\n }\n\n // Must match PREFETCH_KEY and the request in prefetch.ts.\n function prefetch(url: string | null): void {\n if (!url || typeof fetch !== \"function\") return;\n var w = window as unknown as Record<string, Record<string, Promise<Response> | undefined> | undefined>;\n var store = w.__qaidQuestsPrefetch || (w.__qaidQuestsPrefetch = {});\n if (store[url]) return;\n var request = fetch(url, { headers: { Accept: \"application/json\" } });\n // The embed handles a failure (it retries); this only stops a request\n // nobody picks up from reporting an unhandled rejection.\n request.catch(function () {});\n store[url] = request;\n }\n\n function whenIdleAfterLoad(): void {\n function idle(): void {\n var w = window as Window & {\n requestIdleCallback?: (cb: () => void, opts?: { timeout: number }) => number;\n };\n if (typeof w.requestIdleCallback === \"function\") w.requestIdleCallback(load, { timeout: 3000 });\n else setTimeout(load, 200);\n }\n if (document.readyState === \"complete\") idle();\n else window.addEventListener(\"load\", idle);\n }\n\n function start(): void {\n var selector = tag.getAttribute(\"data-container\");\n var container = selector ? document.querySelector(selector) : null;\n if (!container || typeof IntersectionObserver === \"undefined\") {\n load();\n return;\n }\n var observer = new IntersectionObserver(\n function (entries) {\n for (var k = 0; k < entries.length; k++) {\n if (entries[k].isIntersecting) {\n observer.disconnect();\n load();\n return;\n }\n }\n },\n { rootMargin: \"100% 0px\" },\n );\n observer.observe(container);\n whenIdleAfterLoad();\n }\n\n // The container may come after the tag in the page, so look once it's parsed.\n if (document.readyState === \"loading\") document.addEventListener(\"DOMContentLoaded\", start);\n else start();\n})();\n"],"names":["findSelf","cur","scripts","i","src","self","tag","base","loaded","load","entry","attrs","j","a","prefetch","url","w","store","request","whenIdleAfterLoad","idle","start","selector","container","observer","entries","k"],"mappings":"0BAkCC,UAAY,CACX,GAAI,OAAO,UAAa,YAAa,OAIrC,SAASA,GAAqC,CAC5C,IAAIC,EAAM,SAAS,cACnB,GAAIA,GAAOA,EAAI,IAAK,OAAOA,EAE3B,QADIC,EAAU,SAAS,qBAAqB,QAAQ,EAC3CC,EAAID,EAAQ,OAAS,EAAGC,GAAK,EAAGA,IAAK,CAC5C,IAAIC,EAAMF,EAAQC,CAAC,EAAE,KAAO,GAC5B,GAAIC,EAAI,QAAQ,cAAc,IAAM,IAAM,8BAA8B,KAAKA,CAAG,EAC9E,OAAOF,EAAQC,CAAC,CAEpB,CACA,OAAO,IACT,CAEA,IAAIE,EAAOL,EAAA,EACX,G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AcA,CAAQ,EAAI,KAC9D,GAAI,CAACC,GAAa,OAAO,sBAAyB,YAAa,CAC7Dd,EAAA,EACA,MACF,CACA,IAAIe,EAAW,IAAI,qBACjB,SAAUC,EAAS,CACjB,QAASC,EAAI,EAAGA,EAAID,EAAQ,OAAQC,IAClC,GAAID,EAAQC,CAAC,EAAE,eAAgB,CAC7BF,EAAS,WAAA,EACTf,EAAA,EACA,MACF,CAEJ,EACA,CAAE,WAAY,UAAA,CAAW,EAE3Be,EAAS,QAAQD,CAAS,EAC1BJ,EAAA,CACF,CAGI,SAAS,aAAe,UAAW,SAAS,iBAAiB,mBAAoBE,CAAK,EACrFA,EAAA,CACP,GAAA"}
|
|
1
|
+
{"version":3,"file":"embed.js","sources":["../src/trusted-types.ts","../src/embed-loader.ts"],"sourcesContent":["/**\n * The embed's one Trusted Types policy, `qaid-embed`.\n *\n * On a page that sets `require-trusted-types-for 'script'`, assigning a plain\n * string to `script.src` throws. The embed computes every script URL it sets\n * itself (its own stage-two file, the quests module), so it passes each one\n * through this policy, which only signs URLs the embed registered a moment\n * before. A partner allows it with `trusted-types qaid-embed`.\n *\n * The policy is created once per page and kept on `window`, because embed.js,\n * the bundle it loads, and thumbs-embed (which keeps its own copy of this\n * file) all want the same name, and a page's `trusted-types` directive\n * refuses a second policy of one name unless it says `'allow-duplicates'`.\n *\n * When the browser has no Trusted Types, or the page's directive does not\n * list `qaid-embed`, the URL comes back as a plain string. The assignment\n * then works or throws exactly as it would have; callers catch it.\n *\n * ES5-clean on purpose: embed.js, which runs on the oldest browsers we\n * support, bundles this file too.\n */\n\ninterface ScriptUrlPolicy {\n createScriptURL(url: string): unknown;\n}\n\ninterface SharedPolicy {\n policy: ScriptUrlPolicy | null;\n allowed: Record<string, boolean>;\n}\n\ninterface TrustedTypesFactory {\n createPolicy(\n name: string,\n rules: { createScriptURL: (url: string) => string }\n ): ScriptUrlPolicy;\n}\n\nexport var POLICY_NAME = \"qaid-embed\";\nvar KEY = \"__qaidEmbedTrustedTypes\";\n\nfunction shared(): SharedPolicy | null {\n if (typeof window === \"undefined\") return null;\n var w = window as unknown as Record<string, unknown>;\n var tt = w.trustedTypes as TrustedTypesFactory | undefined;\n if (!tt || typeof tt.createPolicy !== \"function\") return null;\n var existing = w[KEY] as SharedPolicy | undefined;\n if (existing) return existing;\n\n var allowed: Record<string, boolean> = Object.create(null);\n var policy: ScriptUrlPolicy | null = null;\n try {\n policy = tt.createPolicy(POLICY_NAME, {\n createScriptURL: function (url: string): string {\n if (allowed[url] === true) return url;\n throw new TypeError(\"qaid-embed: refused script URL \" + url);\n },\n });\n } catch (e) {\n // The page's trusted-types directive does not list qaid-embed.\n policy = null;\n }\n var state: SharedPolicy = { policy: policy, allowed: allowed };\n try {\n Object.defineProperty(w, KEY, { value: state, configurable: true });\n } catch (e) {\n /* frozen window: keep it local */\n }\n return state;\n}\n\n/**\n * A value for `script.src`: a TrustedScriptURL from the `qaid-embed` policy\n * when one exists, else the string itself.\n */\nexport function trustedScriptUrl(url: string): string {\n var s = shared();\n if (!s || !s.policy) return url;\n s.allowed[url] = true;\n return s.policy.createScriptURL(url) as string;\n}\n\n/** @internal Test seam: forget the page's policy. */\nexport function _resetTrustedTypes(): void {\n if (typeof window === \"undefined\") return;\n try {\n delete (window as unknown as Record<string, unknown>)[KEY];\n } catch (e) {\n /* nothing to forget */\n }\n}\n","/**\n * Single-tag embed loader (`dist/embed.js`).\n *\n * A tiny classic `<script>` that holds back the quest code (the UMD bundle,\n * `qaid-quests.umd.cjs`) until it is needed, so a quest further down the page\n * costs the page nothing while it loads. The bundle is injected when the first\n * of these happens:\n *\n * - the quest's container comes within a screen's height of the viewport;\n * - the page has finished loading and the browser is idle, so the quest is\n * ready before the visitor scrolls to it;\n * - right away, when there is no container to watch (the modal install) or\n * the browser has no IntersectionObserver.\n *\n * When it injects the bundle it also starts fetching the quest's questions\n * (`data-config-url`) and theme (`data-theme-url`), so they download beside\n * the code instead of after it. The bundle picks the requests up from\n * `window.__qaidQuestsPrefetch` (see prefetch.ts).\n *\n * The injected bundle initializes itself from the `data-*` attributes copied\n * across from this tag (or from a `<script type=\"application/json\">` config\n * block already on the page), exactly as if the page had loaded it directly.\n *\n * Usage:\n * <div id=\"quest-mount\"></div>\n * <script src=\"https://unpkg.com/@qaiddev/quests-embed/dist/embed.js\" defer\n * data-endpoint=\"/api/responses\" data-config-url=\"/forms/intake.json\"\n * data-container=\"#quest-mount\"></script>\n *\n * Pinned with Subresource Integrity (`integrity=\"sha384-…\"` on this tag), it\n * gives the bundle its own sha384, baked in at build time\n * (vite.config.embed.ts), so the customer's one hash covers every byte of\n * the embed's JavaScript.\n *\n * IMPORTANT: this file must stay ES5-clean and dependency-free, like the\n * thumbs-embed loader it mirrors: it is the first thing to run on every\n * browser. Written with var/function on purpose.\n */\n\nimport { trustedScriptUrl } from \"./trusted-types\";\n\n/** sha384 of qaid-quests.umd.cjs, set by vite.config.embed.ts. Empty in tests. */\ndeclare const __QAID_UMD_INTEGRITY__: string | undefined;\n\n(function () {\n if (typeof document === \"undefined\") return;\n\n // Locate this script tag: prefer document.currentScript (valid for a plain\n // classic script), else find the last <script> whose src looks like embed.js.\n function findSelf(): HTMLScriptElement | null {\n var cur = document.currentScript as HTMLScriptElement | null;\n if (cur && cur.src) return cur;\n var scripts = document.getElementsByTagName(\"script\");\n for (var i = scripts.length - 1; i >= 0; i--) {\n var src = scripts[i].src || \"\";\n if (src.indexOf(\"quests-embed\") !== -1 && /\\/embed(\\.min)?\\.js(\\?|#|$)/.test(src)) {\n return scripts[i];\n }\n }\n return null;\n }\n\n var self = findSelf();\n if (!self || !self.src) return;\n var tag = self;\n\n // Base = the directory that holds embed.js, e.g.\n // \"https://unpkg.com/@qaiddev/quests-embed@1.9.0/dist/\", so the bundle comes\n // from the same (CDN-versioned) place.\n var base = tag.src.substring(0, tag.src.lastIndexOf(\"/\") + 1);\n\n var loaded = false;\n function load(): void {\n if (loaded) return;\n loaded = true;\n var entry = document.createElement(\"script\");\n // Carry the config across so the bundle's auto-init sees it.\n var attrs = tag.attributes;\n for (var j = 0; j < attrs.length; j++) {\n var a = attrs[j];\n if (a.name.indexOf(\"data-\") === 0) entry.setAttribute(a.name, a.value);\n }\n var url = base + \"qaid-quests.umd.cjs\";\n var umdIntegrity = typeof __QAID_UMD_INTEGRITY__ === \"string\" ? __QAID_UMD_INTEGRITY__ : \"\";\n if (umdIntegrity && tag.getAttribute(\"integrity\")) {\n entry.integrity = umdIntegrity;\n entry.crossOrigin = \"anonymous\";\n }\n try {\n entry.src = trustedScriptUrl(url);\n } catch (e) {\n // A Trusted Types page that doesn't allow the qaid-embed policy.\n /* v8 ignore start -- the guard is for old browsers, which have no console while devtools are shut */\n if (typeof console !== \"undefined\") {\n console.warn(\"[quests-embed] Trusted Types blocked \" + url + \". Add `trusted-types qaid-embed` to the page's CSP.\");\n }\n /* v8 ignore stop */\n return;\n }\n (document.head || document.documentElement).appendChild(entry);\n prefetch(tag.getAttribute(\"data-config-url\"));\n prefetch(tag.getAttribute(\"data-theme-url\"));\n }\n\n // Must match PREFETCH_KEY and the request in prefetch.ts.\n function prefetch(url: string | null): void {\n if (!url || typeof fetch !== \"function\") return;\n var w = window as unknown as Record<string, Record<string, Promise<Response> | undefined> | undefined>;\n var store = w.__qaidQuestsPrefetch || (w.__qaidQuestsPrefetch = {});\n if (store[url]) return;\n var request = fetch(url, { headers: { Accept: \"application/json\" } });\n // The embed handles a failure (it retries); this only stops a request\n // nobody picks up from reporting an unhandled rejection.\n request.catch(function () {});\n store[url] = request;\n }\n\n function whenIdleAfterLoad(): void {\n function idle(): void {\n var w = window as Window & {\n requestIdleCallback?: (cb: () => void, opts?: { timeout: number }) => number;\n };\n if (typeof w.requestIdleCallback === \"function\") w.requestIdleCallback(load, { timeout: 3000 });\n else setTimeout(load, 200);\n }\n if (document.readyState === \"complete\") idle();\n else window.addEventListener(\"load\", idle);\n }\n\n function start(): void {\n var selector = tag.getAttribute(\"data-container\");\n var container = selector ? document.querySelector(selector) : null;\n if (!container || typeof IntersectionObserver === \"undefined\") {\n load();\n return;\n }\n var observer = new IntersectionObserver(\n function (entries) {\n for (var k = 0; k < entries.length; k++) {\n if (entries[k].isIntersecting) {\n observer.disconnect();\n load();\n return;\n }\n }\n },\n { rootMargin: \"100% 0px\" },\n );\n observer.observe(container);\n whenIdleAfterLoad();\n }\n\n // The container may come after the tag in the page, so look once it's parsed.\n if (document.readyState === \"loading\") document.addEventListener(\"DOMContentLoaded\", start);\n else start();\n})();\n"],"names":["POLICY_NAME","KEY","shared","w","tt","existing","allowed","policy","url","e","state","trustedScriptUrl","s","findSelf","cur","scripts","i","src","self","tag","base","loaded","load","entry","attrs","j","a","umdIntegrity","prefetch","store","request","whenIdleAfterLoad","idle","start","selector","container","observer","entries","k"],"mappings":"yBAsCO,IAAIA,EAAc,aACrBC,EAAM,0BAEV,SAASC,GAA8B,CACrC,GAAI,OAAO,QAAW,YAAa,OAAO,KAC1C,IAAIC,EAAI,OACJC,EAAKD,EAAE,aACX,GAAI,CAACC,GAAM,OAAOA,EAAG,cAAiB,WAAY,OAAO,KACzD,IAAIC,EAAWF,EAAEF,CAAG,EACpB,GAAII,EAAU,OAAOA,EAErB,IAAIC,EAAmC,OAAO,OAAO,IAAI,EACrDC,EAAiC,KACrC,GAAI,CACFA,EAASH,EAAG,aAAaJ,EAAa,CACpC,gBAAiB,SAAUQ,EAAqB,CAC9C,GAAIF,EAAQE,CAAG,IAAM,GAAM,OAAOA,EAClC,MAAM,IAAI,UAAU,kCAAoCA,CAAG,CAC7D,CAAA,CACD,CACH,OAASC,EAAG,CAEVF,EAAS,IACX,CACA,IAAIG,EAAsB,CAAE,OAAAH,EAAgB,QAAAD,CAAA,EAC5C,GAAI,CACF,OAAO,eAAeH,EAAGF,EAAK,CAAE,MAAOS,EAAO,aAAc,GAAM,CACpE,OAASD,EAAG,CAEZ,CACA,OAAOC,CACT,CAMO,SAASC,EAAiBH,EAAqB,CACpD,IAAII,EAAIV,EAAA,EACR,MAAI,CAAC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