@livx.cc/iab-escape 0.3.0 → 0.4.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
@@ -1,36 +1,22 @@
1
1
  # @livx.cc/iab-escape
2
2
 
3
- Move a user from the Facebook / Instagram in-app browser (IAB) to the device default browser. Zero dependencies, no logging, no analytics. Triggers mirror the ones tested on real devices.
3
+ Opens your page in the user's real browser when it was opened inside the Facebook or Instagram in-app browser.
4
4
 
5
- ## Proven matrix
5
+ In-app browsers lose logins, cookies, payments, app-store and passkey flows. This reopens the same URL in Safari or the default browser. Zero dependencies, no logging, no analytics.
6
6
 
7
- Tested 2026-11-16, organic links (not paid ads). The app always shows its own confirm dialog: the user taps once.
8
-
9
- | App | OS | Scheme | Trigger tested | Result |
10
- |---|---|---|---|---|
11
- | Instagram 455.0.0.35.67 | iOS 26.7.3 | `instagram://extbrowser/?url=<enc>` | `<a>`, tap, and sync `location.href` while parsing (3/3 each) | Safari |
12
- | Facebook 587.0.0.31.71 | iOS 26.7.3 | `x-safari-https://host/path` | `<a>`/tap (3/3); `setTimeout(1500)` from an inline script (3/3); sync firing while parsing fails | Safari |
13
- | Facebook 587.0.0.39.55 | Android 16 | `intent://host/path#Intent;scheme=https;end` | `<a>`/tap: 1 run each; sync `location.href` while parsing: 3/3 | Chrome (default browser) |
14
-
15
- Unproven, returned only with `includeUnproven: true`: Instagram Android (intent), Threads iOS (`barcelona://extbrowser`), Messenger iOS (`x-safari-https`).
16
-
17
- ## Usage
18
-
19
- It reopens the **same URL** in the default browser. In a normal browser it does nothing.
20
-
21
- Script tag, placed early in the page (no code):
7
+ ## Quick start
22
8
 
23
9
  ```html
24
- <script src="iab-escape.min.js" data-auto></script>
10
+ <script src="https://cdn.jsdelivr.net/npm/@livx.cc/iab-escape@0.3/dist/iab-escape.min.js" data-auto></script>
25
11
  ```
26
12
 
27
- `auto()` shows, **by default** (script tag and npm API alike), a full-screen loader ("Opening in your browser…"), then an "Open in browser" button (a real link, works if the automatic fire was ignored) and a small "Continue here". Turn it off with `data-ui="false"` or `ui: false`. No loader when the app is not matched.
28
-
29
- Re-entry: if the page comes back after it left (persisted `pageshow`, or hidden then visible), it fires again once per return, at most `maxAutoFires` (default 3) per page lifetime. If the user cancelled the app's dialog (the page never went away), it does not re-fire; the button stays. "Continue here" stops re-fires.
13
+ Place it early in the page. In any other browser it does nothing: no UI, no navigation.
30
14
 
31
- Optional attributes: `data-ui`, `data-accent`, `data-bg`, `data-fg`, `data-label-opening`, `data-label-open`, `data-label-stay`, `data-delay-ms`, `data-mark-param` (empty = off), `data-include-unproven`, `data-url`.
15
+ ## Install
32
16
 
33
- npm:
17
+ ```sh
18
+ npm i @livx.cc/iab-escape # or: bun add @livx.cc/iab-escape
19
+ ```
34
20
 
35
21
  ```ts
36
22
  import { IabEscape } from '@livx.cc/iab-escape';
@@ -40,18 +26,60 @@ esc.auto(); // once per page load
40
26
  esc.bindLink(document.querySelector('a#open')!); // fallback button/link
41
27
  ```
42
28
 
43
- The script build also exposes the global `IabEscape` class (`IabEscape.version`, `IabEscape.buildTime`). The ESM import has no side effects.
29
+ The ESM import has no side effects. The script build also exposes the global `IabEscape` class (`IabEscape.version`, `IabEscape.buildTime`).
30
+
31
+ ## What the user sees
32
+
33
+ 1. A full-screen loader ("Opening in your browser...").
34
+ 2. The app's own confirm dialog. It cannot be skipped: the user taps once.
35
+ 3. The page reopens in the browser.
36
+
37
+ If the user cancels, they stay on the page with an "Open in browser" button (a real link) and a small "Continue here". If the page comes back after it left (persisted `pageshow`, or hidden then visible), it fires again, at most `maxAutoFires` (default 3) per page lifetime. A cancelled dialog does not re-fire. "Continue here" stops re-fires.
38
+
39
+ ## Supported
40
+
41
+ | App | OS | Opens in |
42
+ |---|---|---|
43
+ | Facebook | iOS | Safari |
44
+ | Instagram | iOS | Safari |
45
+ | Facebook | Android | Default browser |
46
+
47
+ Experimental, returned only with `includeUnproven`: Instagram Android, Threads iOS, Messenger iOS.
48
+
49
+ ## Options and API
44
50
 
45
- **Call `auto()` from an inline script**, as the probes did. If the document is still `loading`, it waits for `DOMContentLoaded` (never `load`), because navigating mid-parse leaves a half-loaded page when the user comes back (seen on Android Facebook). Then delay 0 fires synchronously and a longer delay starts a timer. If the document is already `interactive`/`complete`, the delay counts from the `auto()` call. So it fires at `DOMContentLoaded` at the earliest; on iOS Facebook the 1.5 s counts from then or from the call, whichever is later.
51
+ Script-tag attributes map to the JS options.
46
52
 
47
- - `detect()` returns `{ app, os, inApp }`; `getRule()` the matrix row (with `testedOn`); `buildUrl()` the scheme URL or null.
48
- - `escape()` fires now (call inside a tap for buttons). `auto()` returns a cancel function; cancelling before the fire re-arms the once-per-page guard (flag on the window, shared by all instances).
49
- - `auto()` does nothing when the current page URL already carries the mark param (loop guard); `escape()` still works.
50
- - Options: `url` (relative resolves against the page), `userAgent`, `delayMs`, `includeUnproven`, `markParam` (default `iab_escaped`, appended raw as `=1` only if absent, query otherwise byte-identical; `null` disables), `ui`, `labels`, `theme`, `maxAutoFires`, `onState`, `onAttempt`, `onResult`, `window`. `dismiss()` stops re-fires and removes the loader.
53
+ | Option | Attribute | Default | Purpose |
54
+ |---|---|---|---|
55
+ | `url` | `data-url` | current page | Target URL (relative resolves against the page) |
56
+ | `userAgent` | | `navigator.userAgent` | Override detection input |
57
+ | `delayMs` | `data-delay-ms` | per app | Delay before firing |
58
+ | `includeUnproven` | `data-include-unproven` | `false` | Enable experimental rules |
59
+ | `markParam` | `data-mark-param` | `iab_escaped` | Loop-guard query param; `null` / empty disables |
60
+ | `ui` | `data-ui` | `true` | Built-in loader and buttons |
61
+ | `labels` | `data-label-opening`, `-open`, `-stay` | English | UI text |
62
+ | `theme` | `data-accent`, `data-bg`, `data-fg` | built-in | Colors |
63
+ | `maxAutoFires` | | `3` | Max automatic fires per page lifetime |
64
+ | `onState` | | | Phase callback for your own UI |
65
+ | `onAttempt`, `onResult` | | | Fire / outcome callbacks |
66
+ | `window` | | `window` | Window to use (tests, iframes) |
67
+
68
+ Methods:
69
+
70
+ - `detect()`: returns `{ app, os, inApp }`.
71
+ - `getRule()`: the matching rule (with `testedOn`).
72
+ - `buildUrl()`: the scheme URL, or null.
73
+ - `auto()`: fires automatically; returns a cancel function. Does nothing when the page URL already has the mark param.
74
+ - `escape()`: fires now (call inside a tap for buttons).
75
+ - `bindLink(el)`: makes a link or button open the browser.
76
+ - `dismiss()`: stops re-fires and removes the loader.
77
+
78
+ Cancelling before the fire re-arms the once-per-page guard (a flag on the window, shared by all instances).
51
79
 
52
80
  ## Customize
53
81
 
54
- Styling API: `#iab-escape` and the classes `ie-msg`, `ie-spin`, `ie-open`, `ie-stay`. CSS variables (all optional): `--iab-bg`, `--iab-fg`, `--iab-accent`, `--iab-accent-fg`, `--iab-radius`, `--iab-font`.
82
+ Styling hooks: `#iab-escape` and the classes `ie-msg`, `ie-spin`, `ie-open`, `ie-stay`. CSS variables (all optional): `--iab-bg`, `--iab-fg`, `--iab-accent`, `--iab-accent-fg`, `--iab-radius`, `--iab-font`.
55
83
 
56
84
  ```css
57
85
  #iab-escape { --iab-accent: #e33; --iab-radius: 4px; }
@@ -69,20 +97,44 @@ esc.auto();
69
97
  myUi.onContinue(() => esc.dismiss());
70
98
  ```
71
99
 
100
+ ## How it works
101
+
102
+ - Each app has its own hand-off: Instagram iOS `instagram://extbrowser/?url=<enc>`, Facebook iOS `x-safari-https://host/path` (fired after a 1.5 s delay), Facebook Android `intent://host/path#Intent;scheme=https;end`.
103
+ - If the document is still loading, `auto()` waits for `DOMContentLoaded` (never `load`), because navigating mid-parse leaves a half-loaded page on return. If it is already `interactive`/`complete`, the delay counts from the call.
104
+ - It adds `iab_escaped=1` to the target as a loop guard; the rest of the query stays byte-identical.
105
+ - It never uses meta refresh: that loops on Android when the user cancels.
106
+
107
+ ## Tested on
108
+
109
+ 2026-11-16, organic links: iOS 26.7.3 with Facebook 587 and Instagram 455, and Android 16 with Facebook 587. Each combination passed 3/3 for calls on load and for late calls (SPA-style, 0.8 s after load).
110
+
111
+ ## Declined the prompt
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()`.
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.
116
+
72
117
  ## Limits
73
118
 
74
119
  - The schemes are undocumented by Meta and an app update can break them: keep a fallback link and monitor.
75
- - `data-auto` uses the same `auto()` path (unit-tested); the device runs used the inline `auto()` call.
120
+ - A late `auto()` call (0.8 s after load) was tested 3/3 on Facebook iOS, Instagram iOS and Facebook Android. Only slow or heavy pages remain untested.
76
121
  - A bare `?iab_escaped` (no value) counts as "mark present": the URL is left untouched and `auto()` is skipped.
77
122
  - Paid-ad clicks are untested (organic links only).
78
123
  - A production domain with universal links may open the native app instead of the browser.
79
124
  - Instagram passes no `fbclid` from a DM; Facebook rewrites the `fbclid` at the hand-off.
80
- - Slow or heavy pages, and a late `auto()` call (after `load`, e.g. an SPA mount), are untested on devices.
81
125
  - Matched by the rules but untested: Instagram on iPad, Facebook Lite, `http` targets on Android (`scheme=http`) and Instagram. On iOS Facebook `http` targets return null.
82
- - Never uses meta refresh: it loops on Android when the user cancels.
83
126
  - `onResult` watches `visibilitychange`/`pagehide` for 3 s. Meta webviews do not fire these reliably, so `stayed-or-unknown` is not proof of failure.
84
127
  - The app's confirm dialog cannot be skipped.
85
128
 
86
- ## Dev
129
+ ## Development
130
+
131
+ ```sh
132
+ bun test
133
+ bun run build
134
+ ```
135
+
136
+ Open `demo/index.html` via a static server after building.
137
+
138
+ ## License
87
139
 
88
- `bun test`, `bun run build`. Open `demo/index.html` via a static server after building.
140
+ MIT
@@ -33,6 +33,14 @@ export declare class IabEscapeOptions {
33
33
  theme: Theme;
34
34
  /** Phase callback for a custom UI (also called when the built-in UI is on). */
35
35
  onState: ((state: State) => void) | null;
36
+ /** Where "Continue here" (and the auto fallback) navigates the webview. Default: `url`; none -> just dismiss. */
37
+ continueUrl: string | null;
38
+ /** 'auto': if the page did not go away after the fire, continue in the webview by itself. Device-unverified. */
39
+ fallback: 'manual' | 'auto';
40
+ /** 'auto' only: wait this long after the fire before the first check. */
41
+ fallbackMs: number;
42
+ /** 'auto' only: stop re-checking (every 500 ms while the page is unfocused or hidden) this long after the fire. */
43
+ fallbackMaxMs: number;
36
44
  /** Max automatic fires per page lifetime (the first fire + re-entry re-fires). */
37
45
  maxAutoFires: number;
38
46
  /** Injectable for tests. Default: globalThis.window. */
@@ -52,12 +60,16 @@ export declare class IabEscape {
52
60
  private wentAway;
53
61
  private locked;
54
62
  private dismissed;
63
+ private fallbackSeq;
64
+ private base;
55
65
  constructor(options?: Partial<IabEscapeOptions> | undefined);
56
66
  private get o();
57
67
  private get win();
58
68
  detect(): Env;
59
69
  getRule(): Rule | null;
60
70
  buildUrl(): string | null;
71
+ /** The page URL, captured before the first navigation (relative urls must not resolve against a scheme URL). */
72
+ private pageUrl;
61
73
  /** Resolves the target (relative `url` against the current location); the query is never re-serialized. */
62
74
  private target;
63
75
  /** Fire now. Call inside a user gesture when wired to a button. */
@@ -74,6 +86,14 @@ export declare class IabEscape {
74
86
  private state;
75
87
  /** Stops re-fires and removes the built-in loader. Wire a custom UI's "Continue here" to this. */
76
88
  dismiss(): void;
89
+ /** "Continue here": dismiss, then navigate the current webview to the destination (if there is one). */
90
+ continue(): void;
91
+ private continueTarget;
92
+ /**
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.
95
+ */
96
+ private armAutoFallback;
77
97
  private fallback;
78
98
  private delay;
79
99
  /** One automatic fire: escape(), count it, start watching for re-entry, reveal the button soon after. */
package/dist/IabEscape.js CHANGED
@@ -150,6 +150,10 @@ class IabEscapeOptions {
150
150
  labels = {};
151
151
  theme = {};
152
152
  onState = null;
153
+ continueUrl = null;
154
+ fallback = "manual";
155
+ fallbackMs = 2500;
156
+ fallbackMaxMs = 15000;
153
157
  maxAutoFires = 3;
154
158
  window = null;
155
159
  }
@@ -164,6 +168,13 @@ function optionsFromDataset(d) {
164
168
  o.includeUnproven = true;
165
169
  if (d.url)
166
170
  o.url = d.url;
171
+ if (d.continueUrl)
172
+ o.continueUrl = d.continueUrl;
173
+ if (d.fallback === "auto" || d.fallback === "manual")
174
+ o.fallback = d.fallback;
175
+ const fm = d.fallbackMs?.trim();
176
+ if (fm && Number.isFinite(Number(fm)) && Number(fm) >= 0)
177
+ o.fallbackMs = Number(fm);
167
178
  if (d.ui !== undefined)
168
179
  o.ui = d.ui !== "false";
169
180
  const theme = {};
@@ -189,19 +200,22 @@ function optionsFromDataset(d) {
189
200
  var WATCH_MS = 3000;
190
201
  var REVEAL_MS = 800;
191
202
  var REENTER_LOCK_MS = 1000;
203
+ var AUTO_RECHECK_MS = 500;
192
204
  var FIRED = "__iabEscapeFired";
193
205
  var hasParam = (url, key) => new URLSearchParams(url.split("#")[0].split("?")[1] ?? "").has(key);
194
206
 
195
207
  class IabEscape {
196
208
  options;
197
- static version = "0.3.0";
198
- static buildTime = "2026-11-16T20:42:33.752Z";
209
+ static version = "0.4.0";
210
+ static buildTime = "2026-11-17T09:35:22.763Z";
199
211
  ldr = null;
200
212
  fires = 0;
201
213
  watching = false;
202
214
  wentAway = false;
203
215
  locked = false;
204
216
  dismissed = false;
217
+ fallbackSeq = 0;
218
+ base = "";
205
219
  constructor(options) {
206
220
  this.options = options;
207
221
  this.options = { ...new IabEscapeOptions, ...options };
@@ -225,8 +239,11 @@ class IabEscape {
225
239
  const target = rule && this.target();
226
240
  return rule && target ? rule.build(target) : null;
227
241
  }
242
+ pageUrl() {
243
+ return this.base ||= this.win.location.href;
244
+ }
228
245
  target() {
229
- const base = this.win.location.href;
246
+ const base = this.pageUrl();
230
247
  const input = this.o.url ?? base;
231
248
  let resolved;
232
249
  try {
@@ -297,7 +314,7 @@ class IabEscape {
297
314
  };
298
315
  }
299
316
  loader() {
300
- return this.ldr ??= new Loader(this.win.document, { ...DEFAULT_LABELS, ...this.o.labels }, this.o.theme, () => this.dismiss());
317
+ return this.ldr ??= new Loader(this.win.document, { ...DEFAULT_LABELS, ...this.o.labels }, this.o.theme, () => this.continue());
301
318
  }
302
319
  state(phase) {
303
320
  this.o.onState?.({ phase, url: this.buildUrl() });
@@ -307,6 +324,39 @@ class IabEscape {
307
324
  this.ldr?.hide();
308
325
  this.state("dismissed");
309
326
  }
327
+ continue() {
328
+ this.dismiss();
329
+ const to = this.continueTarget();
330
+ if (to)
331
+ this.win.location.href = to;
332
+ }
333
+ continueTarget() {
334
+ const input = this.o.continueUrl ?? this.o.url;
335
+ if (!input)
336
+ return null;
337
+ try {
338
+ const u = new URL(input, this.pageUrl());
339
+ return /^https?:$/.test(u.protocol) ? /^https?:\/\//i.test(input) ? input : u.href : null;
340
+ } catch {
341
+ return null;
342
+ }
343
+ }
344
+ armAutoFallback() {
345
+ const { win } = this;
346
+ const doc = win.document;
347
+ const seq = ++this.fallbackSeq;
348
+ let waited = this.o.fallbackMs;
349
+ const check = () => {
350
+ if (seq !== this.fallbackSeq || this.dismissed || this.wentAway)
351
+ return;
352
+ if (doc.visibilityState === "visible" && (doc.hasFocus?.() ?? true))
353
+ return this.continue();
354
+ waited += AUTO_RECHECK_MS;
355
+ if (waited <= this.o.fallbackMaxMs)
356
+ win.setTimeout(check, AUTO_RECHECK_MS);
357
+ };
358
+ win.setTimeout(check, this.o.fallbackMs);
359
+ }
310
360
  fallback() {
311
361
  const url = this.buildUrl();
312
362
  if (url && this.o.ui)
@@ -327,6 +377,8 @@ class IabEscape {
327
377
  if (!this.dismissed)
328
378
  this.fallback();
329
379
  }, REVEAL_MS);
380
+ if (this.o.fallback === "auto")
381
+ this.armAutoFallback();
330
382
  return true;
331
383
  }
332
384
  watchReentry() {
@@ -1,4 +1,4 @@
1
- (()=>{var{defineProperty:d,getOwnPropertyNames:A,getOwnPropertyDescriptor:L}=Object,O=Object.prototype.hasOwnProperty;function k(e){return this[e]}var _=(e)=>{var t=(u??=new WeakMap).get(e),i;if(t)return t;if(t=d({},"__esModule",{value:!0}),e&&typeof e==="object"||typeof e==="function"){for(var n of A(e))if(!O.call(t,n))d(t,n,{get:k.bind(e,n),enumerable:!(i=L(e,n))||i.enumerable})}return u.set(e,t),t},u;var R=(e)=>e;function I(e,t){this[e]=R.bind(null,t)}var S=(e,t)=>{for(var i in t)d(e,i,{get:t[i],enumerable:!0,configurable:!0,set:I.bind(t,i)})};var N={};S(N,{boot:()=>T});var M=[["threads",/Barcelona/],["instagram",/Instagram/],["messenger",/FBAN\/MessengerForiOS|FB_IAB\/MESSENGER|Orca/],["facebook",/FBAN|FBAV|FB_IAB/]];function h(e){let t=M.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 c{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: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=`
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 w{url=null;userAgent=null;delayMs=null;includeUnproven=!1;markParam="iab_escaped";onAttempt=null;onResult=null;ui=!0;labels={};theme={};onState=null;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.ui!==void 0)t.ui=e.ui!=="false";let n={};if(e.accent)n.accent=e.accent;if(e.bg)n.bg=e.bg;if(e.fg)n.fg=e.fg;if(Object.keys(n).length)t.theme=n;let s={};if(e.labelOpening)s.opening=e.labelOpening;if(e.labelOpen)s.open=e.labelOpen;if(e.labelStay)s.stay=e.labelStay;if(Object.keys(s).length)t.labels=s;return t}var P=3000,F=800,U=1000,l="__iabEscapeFired",E=(e,t)=>new URLSearchParams(e.split("#")[0].split("?")[1]??"").has(t);class p{options;static version="0.3.0";static buildTime="2026-11-16T20:42:33.752Z";ldr=null;fires=0;watching=!1;wentAway=!1;locked=!1;dismissed=!1;constructor(e){this.options=e;this.options={...new w,...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}target(){let e=this.win.location.href,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 c(this.win.document,{...b,...this.o.labels},this.o.theme,()=>this.dismiss())}state(e){this.o.onState?.({phase:e,url:this.buildUrl()})}dismiss(){this.dismissed=!0,this.ldr?.hide(),this.state("dismissed")}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;return this.fires++,this.watchReentry(),this.win.setTimeout(()=>{if(!this.dismissed)this.fallback()},F),!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},U),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"})},P)}}function T(e,t,i={}){t.IabEscape=p;let n=e?.currentScript;if(n?.hasAttribute("data-auto"))new p({...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.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);})();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@livx.cc/iab-escape",
3
- "version": "0.3.0",
3
+ "version": "0.4.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",