@livx.cc/iab-escape 0.4.0 → 0.6.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 CHANGED
@@ -110,9 +110,14 @@ myUi.onContinue(() => esc.dismiss());
110
110
 
111
111
  ## Declined the prompt
112
112
 
113
- "Continue here" navigates the webview to the destination (`continueUrl` / `data-continue-url`, default: the `url` / `data-url` target; with no url it only removes the overlay). A custom UI calls `esc.continue()`.
113
+ "Continue here" navigates the webview to the destination (`continueUrl` / `data-continue-url`, default: the `url` / `data-url` target; with no url it only removes the overlay). The loader is kept (text `labels.continuing`, "Opening…") until the page unloads, so no bare host page shows in between. A custom UI calls `esc.continue()`.
114
114
 
115
- `fallback: 'auto'` (`data-fallback="auto"`, default `'manual'`) continues by itself when the page is still here `fallbackMs` (default 2500) after the fire. A native confirm may leave the page visible with timers running, so it only continues when `document.visibilityState` is `visible` and `document.hasFocus()` is true, re-checking every 500 ms up to `fallbackMaxMs` (15000). This focus check is device-unverified. No re-fire happens after it navigates.
115
+ `fallback` (`data-fallback`), default `'manual'`:
116
+
117
+ - `'parallel'` (**recommended**): fires the scheme and navigates to the destination `parallelDelayMs` (`data-parallel-delay-ms`, default 0) later. A navigation issued while the app's native prompt is open can be dropped (seen on Facebook iOS), so it is re-issued when the user is back on the page: on `focus`, `visibilitychange` to visible, `pageshow`, or any `pointerdown`/`touchstart` (one per ~300 ms burst), plus one safety retry 4 s after the fire. Retries stop on `pagehide` and while a `beforeunload` says a navigation is in flight.
118
+ - `'auto'`: continues once the page is visible AND focused (`document.hasFocus()`), triggered by the same events and by a 150 ms poll starting `fallbackMs` (default 2500) after the fire, up to `fallbackMaxMs` (15000).
119
+
120
+ No re-fire happens after either navigates.
116
121
 
117
122
  ## Limits
118
123
 
@@ -3,7 +3,7 @@ import { type Labels, type Theme } from './loader';
3
3
  import { type Rule } from './matrix';
4
4
  export type { Env, Labels, Rule, Theme };
5
5
  export interface State {
6
- phase: 'opening' | 'fallback' | 'dismissed' | 'reentry';
6
+ phase: 'opening' | 'fallback' | 'continuing' | 'dismissed' | 'reentry';
7
7
  url: string | null;
8
8
  }
9
9
  export interface AttemptInfo {
@@ -36,9 +36,11 @@ export declare class IabEscapeOptions {
36
36
  /** Where "Continue here" (and the auto fallback) navigates the webview. Default: `url`; none -> just dismiss. */
37
37
  continueUrl: string | null;
38
38
  /** 'auto': if the page did not go away after the fire, continue in the webview by itself. Device-unverified. */
39
- fallback: 'manual' | 'auto';
39
+ fallback: 'manual' | 'auto' | 'parallel';
40
40
  /** 'auto' only: wait this long after the fire before the first check. */
41
41
  fallbackMs: number;
42
+ /** 'parallel' only: navigate to the destination this long after the scheme fires, whatever the confirm answer. */
43
+ parallelDelayMs: number;
42
44
  /** 'auto' only: stop re-checking (every 500 ms while the page is unfocused or hidden) this long after the fire. */
43
45
  fallbackMaxMs: number;
44
46
  /** Max automatic fires per page lifetime (the first fire + re-entry re-fires). */
@@ -60,7 +62,8 @@ export declare class IabEscape {
60
62
  private wentAway;
61
63
  private locked;
62
64
  private dismissed;
63
- private fallbackSeq;
65
+ private continuing;
66
+ private contArmed;
64
67
  private base;
65
68
  constructor(options?: Partial<IabEscapeOptions> | undefined);
66
69
  private get o();
@@ -86,14 +89,19 @@ export declare class IabEscape {
86
89
  private state;
87
90
  /** Stops re-fires and removes the built-in loader. Wire a custom UI's "Continue here" to this. */
88
91
  dismiss(): void;
89
- /** "Continue here": dismiss, then navigate the current webview to the destination (if there is one). */
92
+ /** "Continue here": navigate the current webview to the destination, keeping the loader (no destination: just dismiss). */
90
93
  continue(): void;
94
+ /** Navigates the webview. The overlay stays until the page unloads: no bare host page in between. */
95
+ private go;
91
96
  private continueTarget;
92
97
  /**
93
- * 'auto' fallback: if the page is still here after the fire, continue in the webview. A native confirm may leave
94
- * the page 'visible' with timers running, so only continue when it is visible AND focused; else re-check.
98
+ * Continue in the webview after the scheme fired ('parallel': at once / after parallelDelayMs; 'auto': once the
99
+ * page is visible AND focused). A location.href issued while the app's native prompt is open is dropped, so the
100
+ * navigation is re-issued when the user is back on the page (focus, visible, pageshow, pointer/touch; debounced),
101
+ * plus one safety retry at SAFETY_MS ('parallel'). All retries stop on pagehide; none while a beforeunload
102
+ * says a navigation is in flight. 'auto' also polls every AUTO_RECHECK_MS as a backstop.
95
103
  */
96
- private armAutoFallback;
104
+ private armContinue;
97
105
  private fallback;
98
106
  private delay;
99
107
  /** One automatic fire: escape(), count it, start watching for re-entry, reveal the button soon after. */
package/dist/IabEscape.js CHANGED
@@ -12,7 +12,7 @@ function detectEnv(ua) {
12
12
  }
13
13
 
14
14
  // src/loader.ts
15
- var DEFAULT_LABELS = { opening: "Opening in your browser…", open: "Open in browser", stay: "Continue here" };
15
+ var DEFAULT_LABELS = { opening: "Opening in your browser…", open: "Open in browser", stay: "Continue here", continuing: "Opening…" };
16
16
  var VARS = {
17
17
  bg: "--iab-bg",
18
18
  fg: "--iab-fg",
@@ -42,6 +42,7 @@ class Loader {
42
42
  open;
43
43
  stay;
44
44
  spin;
45
+ msg;
45
46
  constructor(doc, labels, theme = {}, onStay) {
46
47
  this.doc = doc;
47
48
  this.labels = labels;
@@ -65,6 +66,14 @@ class Loader {
65
66
  this.open.hidden = false;
66
67
  this.stay.hidden = false;
67
68
  }
69
+ continuing() {
70
+ if (!this.root)
71
+ this.build();
72
+ this.spin.hidden = false;
73
+ this.open.hidden = true;
74
+ this.stay.hidden = true;
75
+ this.msg.textContent = this.labels.continuing;
76
+ }
68
77
  hide() {
69
78
  this.root?.remove();
70
79
  this.root = null;
@@ -86,7 +95,7 @@ class Loader {
86
95
  const style = this.doc.createElement("style");
87
96
  style.textContent = CSS;
88
97
  this.spin = this.el("div", "ie-spin");
89
- const msg = this.el("div", "ie-msg", this.labels.opening);
98
+ const msg = this.msg = this.el("div", "ie-msg", this.labels.opening);
90
99
  this.open = this.el("a", "ie-open", this.labels.open);
91
100
  this.stay = this.el("a", "ie-stay", this.labels.stay);
92
101
  this.stay.href = "#";
@@ -153,6 +162,7 @@ class IabEscapeOptions {
153
162
  continueUrl = null;
154
163
  fallback = "manual";
155
164
  fallbackMs = 2500;
165
+ parallelDelayMs = 0;
156
166
  fallbackMaxMs = 15000;
157
167
  maxAutoFires = 3;
158
168
  window = null;
@@ -170,8 +180,11 @@ function optionsFromDataset(d) {
170
180
  o.url = d.url;
171
181
  if (d.continueUrl)
172
182
  o.continueUrl = d.continueUrl;
173
- if (d.fallback === "auto" || d.fallback === "manual")
183
+ if (d.fallback === "auto" || d.fallback === "manual" || d.fallback === "parallel")
174
184
  o.fallback = d.fallback;
185
+ const pd = d.parallelDelayMs?.trim();
186
+ if (pd && Number.isFinite(Number(pd)) && Number(pd) >= 0)
187
+ o.parallelDelayMs = Number(pd);
175
188
  const fm = d.fallbackMs?.trim();
176
189
  if (fm && Number.isFinite(Number(fm)) && Number(fm) >= 0)
177
190
  o.fallbackMs = Number(fm);
@@ -193,6 +206,8 @@ function optionsFromDataset(d) {
193
206
  labels.open = d.labelOpen;
194
207
  if (d.labelStay)
195
208
  labels.stay = d.labelStay;
209
+ if (d.labelContinuing)
210
+ labels.continuing = d.labelContinuing;
196
211
  if (Object.keys(labels).length)
197
212
  o.labels = labels;
198
213
  return o;
@@ -200,21 +215,24 @@ function optionsFromDataset(d) {
200
215
  var WATCH_MS = 3000;
201
216
  var REVEAL_MS = 800;
202
217
  var REENTER_LOCK_MS = 1000;
203
- var AUTO_RECHECK_MS = 500;
218
+ var AUTO_RECHECK_MS = 150;
219
+ var DEBOUNCE_MS = 300;
220
+ var SAFETY_MS = 4000;
204
221
  var FIRED = "__iabEscapeFired";
205
222
  var hasParam = (url, key) => new URLSearchParams(url.split("#")[0].split("?")[1] ?? "").has(key);
206
223
 
207
224
  class IabEscape {
208
225
  options;
209
- static version = "0.4.0";
210
- static buildTime = "2026-11-17T09:35:22.763Z";
226
+ static version = "0.6.0";
227
+ static buildTime = "2026-11-17T11:15:12.535Z";
211
228
  ldr = null;
212
229
  fires = 0;
213
230
  watching = false;
214
231
  wentAway = false;
215
232
  locked = false;
216
233
  dismissed = false;
217
- fallbackSeq = 0;
234
+ continuing = false;
235
+ contArmed = false;
218
236
  base = "";
219
237
  constructor(options) {
220
238
  this.options = options;
@@ -325,10 +343,20 @@ class IabEscape {
325
343
  this.state("dismissed");
326
344
  }
327
345
  continue() {
328
- this.dismiss();
329
346
  const to = this.continueTarget();
330
- if (to)
331
- this.win.location.href = to;
347
+ if (!to)
348
+ return this.dismiss();
349
+ this.go(to);
350
+ }
351
+ go(to) {
352
+ if (!this.continuing) {
353
+ this.continuing = true;
354
+ this.dismissed = true;
355
+ if (this.o.ui)
356
+ this.loader().continuing();
357
+ this.state("continuing");
358
+ }
359
+ this.win.location.href = to;
332
360
  }
333
361
  continueTarget() {
334
362
  const input = this.o.continueUrl ?? this.o.url;
@@ -341,16 +369,62 @@ class IabEscape {
341
369
  return null;
342
370
  }
343
371
  }
344
- armAutoFallback() {
372
+ armContinue(mode) {
373
+ const to = this.continueTarget();
374
+ if (this.contArmed || !to)
375
+ return;
376
+ this.contArmed = true;
345
377
  const { win } = this;
346
378
  const doc = win.document;
347
- const seq = ++this.fallbackSeq;
379
+ const focused = () => doc.visibilityState === "visible" && (doc.hasFocus?.() ?? true);
380
+ let started = false, gone = false, flight = false, quiet = false;
381
+ const stopped = () => gone || flight || this.dismissed && !this.continuing;
382
+ const go = () => {
383
+ started = true;
384
+ this.go(to);
385
+ };
386
+ const retry = () => {
387
+ if (stopped() || quiet || mode === "parallel" && !started)
388
+ return;
389
+ if (mode === "auto" && (this.wentAway || !focused()))
390
+ return;
391
+ quiet = true;
392
+ win.setTimeout(() => {
393
+ quiet = false;
394
+ }, DEBOUNCE_MS);
395
+ go();
396
+ };
397
+ win.addEventListener("focus", retry);
398
+ win.addEventListener("pageshow", retry);
399
+ doc.addEventListener("visibilitychange", () => {
400
+ if (doc.visibilityState === "visible")
401
+ retry();
402
+ });
403
+ doc.addEventListener("pointerdown", retry, true);
404
+ doc.addEventListener("touchstart", retry, true);
405
+ win.addEventListener("pagehide", () => {
406
+ gone = true;
407
+ });
408
+ win.addEventListener("beforeunload", () => {
409
+ flight = true;
410
+ });
411
+ if (mode === "parallel") {
412
+ win.setTimeout(() => {
413
+ if (!stopped())
414
+ go();
415
+ }, this.o.parallelDelayMs);
416
+ win.setTimeout(() => {
417
+ if (started && !stopped())
418
+ go();
419
+ }, SAFETY_MS);
420
+ return;
421
+ }
348
422
  let waited = this.o.fallbackMs;
349
423
  const check = () => {
350
- if (seq !== this.fallbackSeq || this.dismissed || this.wentAway)
424
+ if (stopped() || started || this.wentAway)
351
425
  return;
352
- if (doc.visibilityState === "visible" && (doc.hasFocus?.() ?? true))
353
- return this.continue();
426
+ if (focused())
427
+ return go();
354
428
  waited += AUTO_RECHECK_MS;
355
429
  if (waited <= this.o.fallbackMaxMs)
356
430
  win.setTimeout(check, AUTO_RECHECK_MS);
@@ -377,8 +451,8 @@ class IabEscape {
377
451
  if (!this.dismissed)
378
452
  this.fallback();
379
453
  }, REVEAL_MS);
380
- if (this.o.fallback === "auto")
381
- this.armAutoFallback();
454
+ if (this.o.fallback === "auto" || this.o.fallback === "parallel")
455
+ this.armContinue(this.o.fallback);
382
456
  return true;
383
457
  }
384
458
  watchReentry() {
@@ -1,4 +1,4 @@
1
- (()=>{var{defineProperty:u,getOwnPropertyNames:A,getOwnPropertyDescriptor:L}=Object,O=Object.prototype.hasOwnProperty;function M(e){return this[e]}var S=(e)=>{var t=(d??=new WeakMap).get(e),i;if(t)return t;if(t=u({},"__esModule",{value:!0}),e&&typeof e==="object"||typeof e==="function"){for(var n of A(e))if(!O.call(t,n))u(t,n,{get:M.bind(e,n),enumerable:!(i=L(e,n))||i.enumerable})}return d.set(e,t),t},d;var _=(e)=>e;function R(e,t){this[e]=_.bind(null,t)}var I=(e,t)=>{for(var i in t)u(e,i,{get:t[i],enumerable:!0,configurable:!0,set:R.bind(t,i)})};var C={};I(C,{boot:()=>T});var U=[["threads",/Barcelona/],["instagram",/Instagram/],["messenger",/FBAN\/MessengerForiOS|FB_IAB\/MESSENGER|Orca/],["facebook",/FBAN|FBAV|FB_IAB/]];function h(e){let t=U.find(([,n])=>n.test(e))?.[0]??null,i=/iPhone|iPad|iPod/.test(e)?"ios":/Android/.test(e)?"android":null;return{app:t,os:i,inApp:t!==null}}var b={opening:"Opening in your browser…",open:"Open in browser",stay:"Continue here"},f={bg:"--iab-bg",fg:"--iab-fg",accent:"--iab-accent",accentFg:"--iab-accent-fg",radius:"--iab-radius",font:"--iab-font"};class p{doc;labels;theme;onStay;root=null;open;stay;spin;constructor(e,t,i={},n){this.doc=e;this.labels=t;this.theme=i;this.onStay=n}get visible(){return this.root!==null}show(){if(!this.root)this.build();this.spin.hidden=!1,this.open.hidden=!0,this.stay.hidden=!0}reveal(e){if(!this.root)this.build();this.open.href=e,this.open.hidden=!1,this.stay.hidden=!1}hide(){this.root?.remove(),this.root=null}el(e,t,i){let n=this.doc.createElement(e);if(n.className=t,i)n.textContent=i;return n}build(){let e=this.el("div","");e.id="iab-escape",e.setAttribute("role","status");let t=Object.keys(f).filter((s)=>this.theme[s]).map((s)=>`${f[s]}:${this.theme[s]}`).join(";");if(t)e.setAttribute("style",t);let i=this.doc.createElement("style");i.textContent=`
1
+ (()=>{var{defineProperty:f,getOwnPropertyNames:R,getOwnPropertyDescriptor:I}=Object,U=Object.prototype.hasOwnProperty;function N(e){return this[e]}var F=(e)=>{var t=(v??=new WeakMap).get(e),i;if(t)return t;if(t=f({},"__esModule",{value:!0}),e&&typeof e==="object"||typeof e==="function"){for(var n of R(e))if(!U.call(t,n))f(t,n,{get:N.bind(e,n),enumerable:!(i=I(e,n))||i.enumerable})}return v.set(e,t),t},v;var C=(e)=>e;function P(e,t){this[e]=C.bind(null,t)}var D=(e,t)=>{for(var i in t)f(e,i,{get:t[i],enumerable:!0,configurable:!0,set:P.bind(t,i)})};var K={};D(K,{boot:()=>S});var B=[["threads",/Barcelona/],["instagram",/Instagram/],["messenger",/FBAN\/MessengerForiOS|FB_IAB\/MESSENGER|Orca/],["facebook",/FBAN|FBAV|FB_IAB/]];function y(e){let t=B.find(([,n])=>n.test(e))?.[0]??null,i=/iPhone|iPad|iPod/.test(e)?"ios":/Android/.test(e)?"android":null;return{app:t,os:i,inApp:t!==null}}var w={opening:"Opening in your browser…",open:"Open in browser",stay:"Continue here",continuing:"Opening…"},E={bg:"--iab-bg",fg:"--iab-fg",accent:"--iab-accent",accentFg:"--iab-accent-fg",radius:"--iab-radius",font:"--iab-font"};class b{doc;labels;theme;onStay;root=null;open;stay;spin;msg;constructor(e,t,i={},n){this.doc=e;this.labels=t;this.theme=i;this.onStay=n}get visible(){return this.root!==null}show(){if(!this.root)this.build();this.spin.hidden=!1,this.open.hidden=!0,this.stay.hidden=!0}reveal(e){if(!this.root)this.build();this.open.href=e,this.open.hidden=!1,this.stay.hidden=!1}continuing(){if(!this.root)this.build();this.spin.hidden=!1,this.open.hidden=!0,this.stay.hidden=!0,this.msg.textContent=this.labels.continuing}hide(){this.root?.remove(),this.root=null}el(e,t,i){let n=this.doc.createElement(e);if(n.className=t,i)n.textContent=i;return n}build(){let e=this.el("div","");e.id="iab-escape",e.setAttribute("role","status");let t=Object.keys(E).filter((s)=>this.theme[s]).map((s)=>`${E[s]}:${this.theme[s]}`).join(";");if(t)e.setAttribute("style",t);let i=this.doc.createElement("style");i.textContent=`
2
2
  #iab-escape{position:fixed;inset:0;z-index:2147483647;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:18px;
3
3
  padding:max(24px,env(safe-area-inset-top)) max(24px,env(safe-area-inset-right)) max(24px,env(safe-area-inset-bottom)) max(24px,env(safe-area-inset-left));
4
4
  background:var(--iab-bg,#fff);color:var(--iab-fg,#111);font:var(--iab-font,18px/1.35 -apple-system,system-ui,sans-serif);text-align:center}
@@ -8,4 +8,4 @@ background:var(--iab-bg,#fff);color:var(--iab-fg,#111);font:var(--iab-font,18px/
8
8
  #iab-escape [hidden]{display:none}
9
9
  @keyframes ie-r{to{transform:rotate(360deg)}}
10
10
  @media (prefers-color-scheme:dark){#iab-escape{background:var(--iab-bg,#111);color:var(--iab-fg,#f2f2f2)}}
11
- @media (prefers-reduced-motion:reduce){#iab-escape .ie-spin{animation:none}}`,this.spin=this.el("div","ie-spin");let n=this.el("div","ie-msg",this.labels.opening);this.open=this.el("a","ie-open",this.labels.open),this.stay=this.el("a","ie-stay",this.labels.stay),this.stay.href="#",this.stay.addEventListener("click",(s)=>{s.preventDefault(),this.hide(),this.onStay?.()});for(let s of[i,this.spin,n,this.open,this.stay])e.appendChild(s);(this.doc.body??this.doc.documentElement).appendChild(e),this.root=e}}var m=(e)=>e.protocol==="https:"?"x-safari-"+e.href:null,g=(e)=>(t)=>`${e}://extbrowser/?url=${encodeURIComponent(t.href)}`,v=(e)=>`intent://${e.noHash.replace(/^https?:\/\//i,"")}#Intent;scheme=${e.protocol.slice(0,-1)};end`;var y=[{app:"instagram",os:"ios",proven:!0,delayMs:0,build:g("instagram"),testedOn:{date:"2026-11-16",os:"iOS 26.7.3",app:"Instagram 455.0.0.35.67"}},{app:"facebook",os:"ios",proven:!0,delayMs:1500,build:m,testedOn:{date:"2026-11-16",os:"iOS 26.7.3",app:"Facebook 587.0.0.31.71"}},{app:"facebook",os:"android",proven:!0,delayMs:0,build:v,testedOn:{date:"2026-11-16",os:"Android 16",app:"Facebook 587.0.0.39.55"}},{app:"instagram",os:"android",proven:!1,delayMs:0,build:v,testedOn:null},{app:"threads",os:"ios",proven:!1,delayMs:0,build:g("barcelona"),testedOn:null},{app:"messenger",os:"ios",proven:!1,delayMs:1500,build:m,testedOn:null}];class k{url=null;userAgent=null;delayMs=null;includeUnproven=!1;markParam="iab_escaped";onAttempt=null;onResult=null;ui=!0;labels={};theme={};onState=null;continueUrl=null;fallback="manual";fallbackMs=2500;fallbackMaxMs=15000;maxAutoFires=3;window=null}function x(e){let t={},i=e.delayMs?.trim();if(i&&Number.isFinite(Number(i))&&Number(i)>=0)t.delayMs=Number(i);if(e.markParam!==void 0)t.markParam=e.markParam===""?null:e.markParam;if(e.includeUnproven!==void 0)t.includeUnproven=!0;if(e.url)t.url=e.url;if(e.continueUrl)t.continueUrl=e.continueUrl;if(e.fallback==="auto"||e.fallback==="manual")t.fallback=e.fallback;let n=e.fallbackMs?.trim();if(n&&Number.isFinite(Number(n))&&Number(n)>=0)t.fallbackMs=Number(n);if(e.ui!==void 0)t.ui=e.ui!=="false";let s={};if(e.accent)s.accent=e.accent;if(e.bg)s.bg=e.bg;if(e.fg)s.fg=e.fg;if(Object.keys(s).length)t.theme=s;let a={};if(e.labelOpening)a.opening=e.labelOpening;if(e.labelOpen)a.open=e.labelOpen;if(e.labelStay)a.stay=e.labelStay;if(Object.keys(a).length)t.labels=a;return t}var F=3000,P=800,N=1000,w=500,l="__iabEscapeFired",E=(e,t)=>new URLSearchParams(e.split("#")[0].split("?")[1]??"").has(t);class c{options;static version="0.4.0";static buildTime="2026-11-17T09:35:22.763Z";ldr=null;fires=0;watching=!1;wentAway=!1;locked=!1;dismissed=!1;fallbackSeq=0;base="";constructor(e){this.options=e;this.options={...new k,...e}}get o(){return this.options}get win(){return this.o.window??globalThis.window}detect(){return h(this.o.userAgent??this.win.navigator.userAgent)}getRule(){let{app:e,os:t}=this.detect(),i=y.find((n)=>n.app===e&&n.os===t);return i&&(i.proven||this.o.includeUnproven)?i:null}buildUrl(){let e=this.getRule(),t=e&&this.target();return e&&t?e.build(t):null}pageUrl(){return this.base||=this.win.location.href}target(){let e=this.pageUrl(),t=this.o.url??e,i;try{i=new URL(t,e)}catch{return null}if(!/^https?:$/.test(i.protocol))return null;let n=/^https?:\/\//i.test(t)?t:i.href,s=n.indexOf("#"),a=s<0?n:n.slice(0,s),o=s<0?"":n.slice(s),r=this.o.markParam;if(r&&!E(a,r))a+=(a.includes("?")?/[?&]$/.test(a)?"":"&":"?")+r+"=1";return{href:a+o,noHash:a,protocol:i.protocol}}escape(){let e=this.getRule(),t=this.buildUrl();if(!e||!t)return!1;return this.o.onAttempt?.({rule:e,url:t}),this.win.location.href=t,this.watch({rule:e,url:t}),!0}auto(){let{win:e}=this,t=this.o.markParam,i=this.getRule();if(!i||e[l]||t&&E(e.location.href,t))return()=>{};e[l]=!0;let{document:n}=e;if(this.dismissed=!1,this.o.ui)this.loader().show();this.state("opening");let s=!1,a,o=()=>{if(s=!0,!this.fireNow())e[l]=!1},r=()=>{if(n.removeEventListener("DOMContentLoaded",r),this.delay(i)<=0)o();else a=e.setTimeout(o,this.delay(i))};if(n.readyState==="loading")n.addEventListener("DOMContentLoaded",r);else r();return()=>{if(s)return;if(n.removeEventListener("DOMContentLoaded",r),a!==void 0)e.clearTimeout(a);e[l]=!1,this.ldr?.hide()}}loader(){return this.ldr??=new p(this.win.document,{...b,...this.o.labels},this.o.theme,()=>this.continue())}state(e){this.o.onState?.({phase:e,url:this.buildUrl()})}dismiss(){this.dismissed=!0,this.ldr?.hide(),this.state("dismissed")}continue(){this.dismiss();let e=this.continueTarget();if(e)this.win.location.href=e}continueTarget(){let e=this.o.continueUrl??this.o.url;if(!e)return null;try{let t=new URL(e,this.pageUrl());return/^https?:$/.test(t.protocol)?/^https?:\/\//i.test(e)?e:t.href:null}catch{return null}}armAutoFallback(){let{win:e}=this,t=e.document,i=++this.fallbackSeq,n=this.o.fallbackMs,s=()=>{if(i!==this.fallbackSeq||this.dismissed||this.wentAway)return;if(t.visibilityState==="visible"&&(t.hasFocus?.()??!0))return this.continue();if(n+=w,n<=this.o.fallbackMaxMs)e.setTimeout(s,w)};e.setTimeout(s,this.o.fallbackMs)}fallback(){let e=this.buildUrl();if(e&&this.o.ui)this.loader().reveal(e);this.state("fallback")}delay(e){return this.o.delayMs??e.delayMs}fireNow(){if(!this.escape())return this.ldr?.hide(),!1;if(this.fires++,this.watchReentry(),this.win.setTimeout(()=>{if(!this.dismissed)this.fallback()},P),this.o.fallback==="auto")this.armAutoFallback();return!0}watchReentry(){if(this.watching)return;this.watching=!0;let{win:e}=this,t=e.document;e.addEventListener("pagehide",()=>{this.wentAway=!0}),t.addEventListener("visibilitychange",()=>{if(t.visibilityState==="hidden")this.wentAway=!0;else this.reenter()}),e.addEventListener("pageshow",(i)=>{if(i.persisted)this.wentAway=!0,this.reenter()})}reenter(){let{win:e}=this,t=this.getRule();if(!this.wentAway||this.locked||this.dismissed||!t)return;if(this.wentAway=!1,this.locked=!0,e.setTimeout(()=>{this.locked=!1},N),this.fires>=this.o.maxAutoFires)return this.fallback();if(this.o.ui)this.loader().show();if(this.state("reentry"),this.delay(t)<=0)this.fireNow();else e.setTimeout(()=>this.fireNow(),this.delay(t))}bindLink(e){let t=this.buildUrl();if(!t)return!1;return e.href=t,!0}watch(e){let{win:t}=this,i=!1,n=()=>{i=!0},s=()=>{if(t.document.visibilityState==="hidden")n()};t.addEventListener("pagehide",n),t.document.addEventListener("visibilitychange",s),t.setTimeout(()=>{t.removeEventListener("pagehide",n),t.document.removeEventListener("visibilitychange",s),this.o.onResult?.({...e,outcome:i?"left":"stayed-or-unknown"})},F)}}function T(e,t,i={}){t.IabEscape=c;let n=e?.currentScript;if(n?.hasAttribute("data-auto"))new c({...x(n.dataset),...i}).auto()}if(typeof document<"u")T(document,globalThis);})();
11
+ @media (prefers-reduced-motion:reduce){#iab-escape .ie-spin{animation:none}}`,this.spin=this.el("div","ie-spin");let n=this.msg=this.el("div","ie-msg",this.labels.opening);this.open=this.el("a","ie-open",this.labels.open),this.stay=this.el("a","ie-stay",this.labels.stay),this.stay.href="#",this.stay.addEventListener("click",(s)=>{s.preventDefault(),this.hide(),this.onStay?.()});for(let s of[i,this.spin,n,this.open,this.stay])e.appendChild(s);(this.doc.body??this.doc.documentElement).appendChild(e),this.root=e}}var T=(e)=>e.protocol==="https:"?"x-safari-"+e.href:null,x=(e)=>(t)=>`${e}://extbrowser/?url=${encodeURIComponent(t.href)}`,k=(e)=>`intent://${e.noHash.replace(/^https?:\/\//i,"")}#Intent;scheme=${e.protocol.slice(0,-1)};end`;var L=[{app:"instagram",os:"ios",proven:!0,delayMs:0,build:x("instagram"),testedOn:{date:"2026-11-16",os:"iOS 26.7.3",app:"Instagram 455.0.0.35.67"}},{app:"facebook",os:"ios",proven:!0,delayMs:1500,build:T,testedOn:{date:"2026-11-16",os:"iOS 26.7.3",app:"Facebook 587.0.0.31.71"}},{app:"facebook",os:"android",proven:!0,delayMs:0,build:k,testedOn:{date:"2026-11-16",os:"Android 16",app:"Facebook 587.0.0.39.55"}},{app:"instagram",os:"android",proven:!1,delayMs:0,build:k,testedOn:null},{app:"threads",os:"ios",proven:!1,delayMs:0,build:x("barcelona"),testedOn:null},{app:"messenger",os:"ios",proven:!1,delayMs:1500,build:T,testedOn:null}];class O{url=null;userAgent=null;delayMs=null;includeUnproven=!1;markParam="iab_escaped";onAttempt=null;onResult=null;ui=!0;labels={};theme={};onState=null;continueUrl=null;fallback="manual";fallbackMs=2500;parallelDelayMs=0;fallbackMaxMs=15000;maxAutoFires=3;window=null}function _(e){let t={},i=e.delayMs?.trim();if(i&&Number.isFinite(Number(i))&&Number(i)>=0)t.delayMs=Number(i);if(e.markParam!==void 0)t.markParam=e.markParam===""?null:e.markParam;if(e.includeUnproven!==void 0)t.includeUnproven=!0;if(e.url)t.url=e.url;if(e.continueUrl)t.continueUrl=e.continueUrl;if(e.fallback==="auto"||e.fallback==="manual"||e.fallback==="parallel")t.fallback=e.fallback;let n=e.parallelDelayMs?.trim();if(n&&Number.isFinite(Number(n))&&Number(n)>=0)t.parallelDelayMs=Number(n);let s=e.fallbackMs?.trim();if(s&&Number.isFinite(Number(s))&&Number(s)>=0)t.fallbackMs=Number(s);if(e.ui!==void 0)t.ui=e.ui!=="false";let a={};if(e.accent)a.accent=e.accent;if(e.bg)a.bg=e.bg;if(e.fg)a.fg=e.fg;if(Object.keys(a).length)t.theme=a;let r={};if(e.labelOpening)r.opening=e.labelOpening;if(e.labelOpen)r.open=e.labelOpen;if(e.labelStay)r.stay=e.labelStay;if(e.labelContinuing)r.continuing=e.labelContinuing;if(Object.keys(r).length)t.labels=r;return t}var H=3000,V=800,j=1000,A=150,W=300,z=4000,p="__iabEscapeFired",M=(e,t)=>new URLSearchParams(e.split("#")[0].split("?")[1]??"").has(t);class d{options;static version="0.6.0";static buildTime="2026-11-17T11:15:12.535Z";ldr=null;fires=0;watching=!1;wentAway=!1;locked=!1;dismissed=!1;continuing=!1;contArmed=!1;base="";constructor(e){this.options=e;this.options={...new O,...e}}get o(){return this.options}get win(){return this.o.window??globalThis.window}detect(){return y(this.o.userAgent??this.win.navigator.userAgent)}getRule(){let{app:e,os:t}=this.detect(),i=L.find((n)=>n.app===e&&n.os===t);return i&&(i.proven||this.o.includeUnproven)?i:null}buildUrl(){let e=this.getRule(),t=e&&this.target();return e&&t?e.build(t):null}pageUrl(){return this.base||=this.win.location.href}target(){let e=this.pageUrl(),t=this.o.url??e,i;try{i=new URL(t,e)}catch{return null}if(!/^https?:$/.test(i.protocol))return null;let n=/^https?:\/\//i.test(t)?t:i.href,s=n.indexOf("#"),a=s<0?n:n.slice(0,s),r=s<0?"":n.slice(s),o=this.o.markParam;if(o&&!M(a,o))a+=(a.includes("?")?/[?&]$/.test(a)?"":"&":"?")+o+"=1";return{href:a+r,noHash:a,protocol:i.protocol}}escape(){let e=this.getRule(),t=this.buildUrl();if(!e||!t)return!1;return this.o.onAttempt?.({rule:e,url:t}),this.win.location.href=t,this.watch({rule:e,url:t}),!0}auto(){let{win:e}=this,t=this.o.markParam,i=this.getRule();if(!i||e[p]||t&&M(e.location.href,t))return()=>{};e[p]=!0;let{document:n}=e;if(this.dismissed=!1,this.o.ui)this.loader().show();this.state("opening");let s=!1,a,r=()=>{if(s=!0,!this.fireNow())e[p]=!1},o=()=>{if(n.removeEventListener("DOMContentLoaded",o),this.delay(i)<=0)r();else a=e.setTimeout(r,this.delay(i))};if(n.readyState==="loading")n.addEventListener("DOMContentLoaded",o);else o();return()=>{if(s)return;if(n.removeEventListener("DOMContentLoaded",o),a!==void 0)e.clearTimeout(a);e[p]=!1,this.ldr?.hide()}}loader(){return this.ldr??=new b(this.win.document,{...w,...this.o.labels},this.o.theme,()=>this.continue())}state(e){this.o.onState?.({phase:e,url:this.buildUrl()})}dismiss(){this.dismissed=!0,this.ldr?.hide(),this.state("dismissed")}continue(){let e=this.continueTarget();if(!e)return this.dismiss();this.go(e)}go(e){if(!this.continuing){if(this.continuing=!0,this.dismissed=!0,this.o.ui)this.loader().continuing();this.state("continuing")}this.win.location.href=e}continueTarget(){let e=this.o.continueUrl??this.o.url;if(!e)return null;try{let t=new URL(e,this.pageUrl());return/^https?:$/.test(t.protocol)?/^https?:\/\//i.test(e)?e:t.href:null}catch{return null}}armContinue(e){let t=this.continueTarget();if(this.contArmed||!t)return;this.contArmed=!0;let{win:i}=this,n=i.document,s=()=>n.visibilityState==="visible"&&(n.hasFocus?.()??!0),a=!1,r=!1,o=!1,h=!1,u=()=>r||o||this.dismissed&&!this.continuing,c=()=>{a=!0,this.go(t)},l=()=>{if(u()||h||e==="parallel"&&!a)return;if(e==="auto"&&(this.wentAway||!s()))return;h=!0,i.setTimeout(()=>{h=!1},W),c()};if(i.addEventListener("focus",l),i.addEventListener("pageshow",l),n.addEventListener("visibilitychange",()=>{if(n.visibilityState==="visible")l()}),n.addEventListener("pointerdown",l,!0),n.addEventListener("touchstart",l,!0),i.addEventListener("pagehide",()=>{r=!0}),i.addEventListener("beforeunload",()=>{o=!0}),e==="parallel"){i.setTimeout(()=>{if(!u())c()},this.o.parallelDelayMs),i.setTimeout(()=>{if(a&&!u())c()},z);return}let g=this.o.fallbackMs,m=()=>{if(u()||a||this.wentAway)return;if(s())return c();if(g+=A,g<=this.o.fallbackMaxMs)i.setTimeout(m,A)};i.setTimeout(m,this.o.fallbackMs)}fallback(){let e=this.buildUrl();if(e&&this.o.ui)this.loader().reveal(e);this.state("fallback")}delay(e){return this.o.delayMs??e.delayMs}fireNow(){if(!this.escape())return this.ldr?.hide(),!1;if(this.fires++,this.watchReentry(),this.win.setTimeout(()=>{if(!this.dismissed)this.fallback()},V),this.o.fallback==="auto"||this.o.fallback==="parallel")this.armContinue(this.o.fallback);return!0}watchReentry(){if(this.watching)return;this.watching=!0;let{win:e}=this,t=e.document;e.addEventListener("pagehide",()=>{this.wentAway=!0}),t.addEventListener("visibilitychange",()=>{if(t.visibilityState==="hidden")this.wentAway=!0;else this.reenter()}),e.addEventListener("pageshow",(i)=>{if(i.persisted)this.wentAway=!0,this.reenter()})}reenter(){let{win:e}=this,t=this.getRule();if(!this.wentAway||this.locked||this.dismissed||!t)return;if(this.wentAway=!1,this.locked=!0,e.setTimeout(()=>{this.locked=!1},j),this.fires>=this.o.maxAutoFires)return this.fallback();if(this.o.ui)this.loader().show();if(this.state("reentry"),this.delay(t)<=0)this.fireNow();else e.setTimeout(()=>this.fireNow(),this.delay(t))}bindLink(e){let t=this.buildUrl();if(!t)return!1;return e.href=t,!0}watch(e){let{win:t}=this,i=!1,n=()=>{i=!0},s=()=>{if(t.document.visibilityState==="hidden")n()};t.addEventListener("pagehide",n),t.document.addEventListener("visibilitychange",s),t.setTimeout(()=>{t.removeEventListener("pagehide",n),t.document.removeEventListener("visibilitychange",s),this.o.onResult?.({...e,outcome:i?"left":"stayed-or-unknown"})},H)}}function S(e,t,i={}){t.IabEscape=d;let n=e?.currentScript;if(n?.hasAttribute("data-auto"))new d({..._(n.dataset),...i}).auto()}if(typeof document<"u")S(document,globalThis);})();
package/dist/loader.d.ts CHANGED
@@ -2,6 +2,7 @@ export interface Labels {
2
2
  opening: string;
3
3
  open: string;
4
4
  stay: string;
5
+ continuing: string;
5
6
  }
6
7
  export declare const DEFAULT_LABELS: Labels;
7
8
  /** Styling API: the `#iab-escape` root, the classes below, and these CSS variables (all optional). */
@@ -23,12 +24,15 @@ export declare class Loader {
23
24
  private open;
24
25
  private stay;
25
26
  private spin;
27
+ private msg;
26
28
  constructor(doc: Document, labels: Labels, theme?: Theme, onStay?: (() => void) | undefined);
27
29
  get visible(): boolean;
28
30
  /** Spinner state (also resets a previously revealed button). */
29
31
  show(): void;
30
32
  /** Reveals the real `<a>` (so a tap works if the automatic fire was ignored) and the "stay" link. */
31
33
  reveal(href: string): void;
34
+ /** A continue navigation is under way: keep the overlay (no bare page in between), spinner + short text. */
35
+ continuing(): void;
32
36
  hide(): void;
33
37
  private el;
34
38
  private build;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@livx.cc/iab-escape",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "type": "module",
5
5
  "description": "Move users from the Facebook / Instagram in-app browser to the device default browser, using only trigger methods proven on real devices.",
6
6
  "license": "MIT",