@livx.cc/iab-escape 0.1.0 → 0.3.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 +26 -2
- package/dist/IabEscape.d.ts +36 -1
- package/dist/IabEscape.js +209 -6
- package/dist/iab-escape.min.js +11 -1
- package/dist/loader.d.ts +35 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -24,7 +24,11 @@ Script tag, placed early in the page (no code):
|
|
|
24
24
|
<script src="iab-escape.min.js" data-auto></script>
|
|
25
25
|
```
|
|
26
26
|
|
|
27
|
-
|
|
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.
|
|
30
|
+
|
|
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`.
|
|
28
32
|
|
|
29
33
|
npm:
|
|
30
34
|
|
|
@@ -43,7 +47,27 @@ The script build also exposes the global `IabEscape` class (`IabEscape.version`,
|
|
|
43
47
|
- `detect()` returns `{ app, os, inApp }`; `getRule()` the matrix row (with `testedOn`); `buildUrl()` the scheme URL or null.
|
|
44
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).
|
|
45
49
|
- `auto()` does nothing when the current page URL already carries the mark param (loop guard); `escape()` still works.
|
|
46
|
-
- 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), `onAttempt`, `onResult`, `window`.
|
|
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.
|
|
51
|
+
|
|
52
|
+
## Customize
|
|
53
|
+
|
|
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`.
|
|
55
|
+
|
|
56
|
+
```css
|
|
57
|
+
#iab-escape { --iab-accent: #e33; --iab-radius: 4px; }
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
new IabEscape({ theme: { accent: '#e33', bg: '#000' }, labels: { opening: 'One moment…', open: 'Open', stay: 'Stay' } }).auto();
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Own UI: turn the built-in one off and render from `onState` (`phase`: `opening`, `fallback`, `reentry`, `dismissed`; `url` is the scheme URL for the fallback button).
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
const esc = new IabEscape({ ui: false, onState: ({ phase, url }) => myUi.render(phase, url) });
|
|
68
|
+
esc.auto();
|
|
69
|
+
myUi.onContinue(() => esc.dismiss());
|
|
70
|
+
```
|
|
47
71
|
|
|
48
72
|
## Limits
|
|
49
73
|
|
package/dist/IabEscape.d.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { type Env } from './detect';
|
|
2
|
+
import { type Labels, type Theme } from './loader';
|
|
2
3
|
import { type Rule } from './matrix';
|
|
3
|
-
export type { Env, Rule };
|
|
4
|
+
export type { Env, Labels, Rule, Theme };
|
|
5
|
+
export interface State {
|
|
6
|
+
phase: 'opening' | 'fallback' | 'dismissed' | 'reentry';
|
|
7
|
+
url: string | null;
|
|
8
|
+
}
|
|
4
9
|
export interface AttemptInfo {
|
|
5
10
|
rule: Rule;
|
|
6
11
|
url: string;
|
|
@@ -21,6 +26,15 @@ export declare class IabEscapeOptions {
|
|
|
21
26
|
markParam: string | null;
|
|
22
27
|
onAttempt: ((info: AttemptInfo) => void) | null;
|
|
23
28
|
onResult: ((info: ResultInfo) => void) | null;
|
|
29
|
+
/** Built-in full-screen loader (spinner, then an "Open in browser" button) while auto() runs. false = off. */
|
|
30
|
+
ui: boolean;
|
|
31
|
+
labels: Partial<Labels>;
|
|
32
|
+
/** Sets the `--iab-*` CSS variables inline on the overlay. */
|
|
33
|
+
theme: Theme;
|
|
34
|
+
/** Phase callback for a custom UI (also called when the built-in UI is on). */
|
|
35
|
+
onState: ((state: State) => void) | null;
|
|
36
|
+
/** Max automatic fires per page lifetime (the first fire + re-entry re-fires). */
|
|
37
|
+
maxAutoFires: number;
|
|
24
38
|
/** Injectable for tests. Default: globalThis.window. */
|
|
25
39
|
window: Window | null;
|
|
26
40
|
}
|
|
@@ -32,6 +46,12 @@ export declare class IabEscape {
|
|
|
32
46
|
static readonly version: string;
|
|
33
47
|
/** ISO build time (injected at build time), or null. */
|
|
34
48
|
static readonly buildTime: string | null;
|
|
49
|
+
private ldr;
|
|
50
|
+
private fires;
|
|
51
|
+
private watching;
|
|
52
|
+
private wentAway;
|
|
53
|
+
private locked;
|
|
54
|
+
private dismissed;
|
|
35
55
|
constructor(options?: Partial<IabEscapeOptions> | undefined);
|
|
36
56
|
private get o();
|
|
37
57
|
private get win();
|
|
@@ -50,6 +70,21 @@ export declare class IabEscape {
|
|
|
50
70
|
* Returns a cancel fn; cancelling before the fire re-arms the guard (StrictMode-safe).
|
|
51
71
|
*/
|
|
52
72
|
auto(): () => void;
|
|
73
|
+
private loader;
|
|
74
|
+
private state;
|
|
75
|
+
/** Stops re-fires and removes the built-in loader. Wire a custom UI's "Continue here" to this. */
|
|
76
|
+
dismiss(): void;
|
|
77
|
+
private fallback;
|
|
78
|
+
private delay;
|
|
79
|
+
/** One automatic fire: escape(), count it, start watching for re-entry, reveal the button soon after. */
|
|
80
|
+
private fireNow;
|
|
81
|
+
/**
|
|
82
|
+
* Re-entry: the page is shown again after a fire that left (pagehide/hidden seen, or a persisted pageshow,
|
|
83
|
+
* i.e. bfcache / restored webview). Re-fires ONCE per re-entry, up to maxAutoFires per page lifetime.
|
|
84
|
+
* A page that never went away (user pressed Cancel) is not re-fired; the button stays.
|
|
85
|
+
*/
|
|
86
|
+
private watchReentry;
|
|
87
|
+
private reenter;
|
|
53
88
|
/** Plain `<a>` tap path (proven on all three). Leaves the element untouched when there is no rule. */
|
|
54
89
|
bindLink(a: HTMLAnchorElement): boolean;
|
|
55
90
|
private watch;
|
package/dist/IabEscape.js
CHANGED
|
@@ -11,6 +11,97 @@ function detectEnv(ua) {
|
|
|
11
11
|
return { app, os, inApp: app !== null };
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
// src/loader.ts
|
|
15
|
+
var DEFAULT_LABELS = { opening: "Opening in your browser…", open: "Open in browser", stay: "Continue here" };
|
|
16
|
+
var VARS = {
|
|
17
|
+
bg: "--iab-bg",
|
|
18
|
+
fg: "--iab-fg",
|
|
19
|
+
accent: "--iab-accent",
|
|
20
|
+
accentFg: "--iab-accent-fg",
|
|
21
|
+
radius: "--iab-radius",
|
|
22
|
+
font: "--iab-font"
|
|
23
|
+
};
|
|
24
|
+
var CSS = `
|
|
25
|
+
#iab-escape{position:fixed;inset:0;z-index:2147483647;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:18px;
|
|
26
|
+
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));
|
|
27
|
+
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}
|
|
28
|
+
#iab-escape .ie-spin{width:36px;height:36px;border:4px solid #8884;border-top-color:var(--iab-accent,#07f);border-radius:50%;animation:ie-r 1s linear infinite}
|
|
29
|
+
#iab-escape a.ie-open{display:block;padding:14px 28px;border-radius:var(--iab-radius,12px);background:var(--iab-accent,#07f);color:var(--iab-accent-fg,#fff);text-decoration:none;font-weight:600}
|
|
30
|
+
#iab-escape a.ie-stay{font-size:14px;color:inherit;opacity:.7}
|
|
31
|
+
#iab-escape [hidden]{display:none}
|
|
32
|
+
@keyframes ie-r{to{transform:rotate(360deg)}}
|
|
33
|
+
@media (prefers-color-scheme:dark){#iab-escape{background:var(--iab-bg,#111);color:var(--iab-fg,#f2f2f2)}}
|
|
34
|
+
@media (prefers-reduced-motion:reduce){#iab-escape .ie-spin{animation:none}}`;
|
|
35
|
+
|
|
36
|
+
class Loader {
|
|
37
|
+
doc;
|
|
38
|
+
labels;
|
|
39
|
+
theme;
|
|
40
|
+
onStay;
|
|
41
|
+
root = null;
|
|
42
|
+
open;
|
|
43
|
+
stay;
|
|
44
|
+
spin;
|
|
45
|
+
constructor(doc, labels, theme = {}, onStay) {
|
|
46
|
+
this.doc = doc;
|
|
47
|
+
this.labels = labels;
|
|
48
|
+
this.theme = theme;
|
|
49
|
+
this.onStay = onStay;
|
|
50
|
+
}
|
|
51
|
+
get visible() {
|
|
52
|
+
return this.root !== null;
|
|
53
|
+
}
|
|
54
|
+
show() {
|
|
55
|
+
if (!this.root)
|
|
56
|
+
this.build();
|
|
57
|
+
this.spin.hidden = false;
|
|
58
|
+
this.open.hidden = true;
|
|
59
|
+
this.stay.hidden = true;
|
|
60
|
+
}
|
|
61
|
+
reveal(href) {
|
|
62
|
+
if (!this.root)
|
|
63
|
+
this.build();
|
|
64
|
+
this.open.href = href;
|
|
65
|
+
this.open.hidden = false;
|
|
66
|
+
this.stay.hidden = false;
|
|
67
|
+
}
|
|
68
|
+
hide() {
|
|
69
|
+
this.root?.remove();
|
|
70
|
+
this.root = null;
|
|
71
|
+
}
|
|
72
|
+
el(tag, cls, text) {
|
|
73
|
+
const e = this.doc.createElement(tag);
|
|
74
|
+
e.className = cls;
|
|
75
|
+
if (text)
|
|
76
|
+
e.textContent = text;
|
|
77
|
+
return e;
|
|
78
|
+
}
|
|
79
|
+
build() {
|
|
80
|
+
const root = this.el("div", "");
|
|
81
|
+
root.id = "iab-escape";
|
|
82
|
+
root.setAttribute("role", "status");
|
|
83
|
+
const inline = Object.keys(VARS).filter((k) => this.theme[k]).map((k) => `${VARS[k]}:${this.theme[k]}`).join(";");
|
|
84
|
+
if (inline)
|
|
85
|
+
root.setAttribute("style", inline);
|
|
86
|
+
const style = this.doc.createElement("style");
|
|
87
|
+
style.textContent = CSS;
|
|
88
|
+
this.spin = this.el("div", "ie-spin");
|
|
89
|
+
const msg = this.el("div", "ie-msg", this.labels.opening);
|
|
90
|
+
this.open = this.el("a", "ie-open", this.labels.open);
|
|
91
|
+
this.stay = this.el("a", "ie-stay", this.labels.stay);
|
|
92
|
+
this.stay.href = "#";
|
|
93
|
+
this.stay.addEventListener("click", (ev) => {
|
|
94
|
+
ev.preventDefault();
|
|
95
|
+
this.hide();
|
|
96
|
+
this.onStay?.();
|
|
97
|
+
});
|
|
98
|
+
for (const c of [style, this.spin, msg, this.open, this.stay])
|
|
99
|
+
root.appendChild(c);
|
|
100
|
+
(this.doc.body ?? this.doc.documentElement).appendChild(root);
|
|
101
|
+
this.root = root;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
14
105
|
// src/matrix.ts
|
|
15
106
|
var xSafari = (t) => t.protocol === "https:" ? "x-safari-" + t.href : null;
|
|
16
107
|
var extbrowser = (scheme) => (t) => `${scheme}://extbrowser/?url=${encodeURIComponent(t.href)}`;
|
|
@@ -55,6 +146,11 @@ class IabEscapeOptions {
|
|
|
55
146
|
markParam = "iab_escaped";
|
|
56
147
|
onAttempt = null;
|
|
57
148
|
onResult = null;
|
|
149
|
+
ui = true;
|
|
150
|
+
labels = {};
|
|
151
|
+
theme = {};
|
|
152
|
+
onState = null;
|
|
153
|
+
maxAutoFires = 3;
|
|
58
154
|
window = null;
|
|
59
155
|
}
|
|
60
156
|
function optionsFromDataset(d) {
|
|
@@ -68,16 +164,44 @@ function optionsFromDataset(d) {
|
|
|
68
164
|
o.includeUnproven = true;
|
|
69
165
|
if (d.url)
|
|
70
166
|
o.url = d.url;
|
|
167
|
+
if (d.ui !== undefined)
|
|
168
|
+
o.ui = d.ui !== "false";
|
|
169
|
+
const theme = {};
|
|
170
|
+
if (d.accent)
|
|
171
|
+
theme.accent = d.accent;
|
|
172
|
+
if (d.bg)
|
|
173
|
+
theme.bg = d.bg;
|
|
174
|
+
if (d.fg)
|
|
175
|
+
theme.fg = d.fg;
|
|
176
|
+
if (Object.keys(theme).length)
|
|
177
|
+
o.theme = theme;
|
|
178
|
+
const labels = {};
|
|
179
|
+
if (d.labelOpening)
|
|
180
|
+
labels.opening = d.labelOpening;
|
|
181
|
+
if (d.labelOpen)
|
|
182
|
+
labels.open = d.labelOpen;
|
|
183
|
+
if (d.labelStay)
|
|
184
|
+
labels.stay = d.labelStay;
|
|
185
|
+
if (Object.keys(labels).length)
|
|
186
|
+
o.labels = labels;
|
|
71
187
|
return o;
|
|
72
188
|
}
|
|
73
189
|
var WATCH_MS = 3000;
|
|
190
|
+
var REVEAL_MS = 800;
|
|
191
|
+
var REENTER_LOCK_MS = 1000;
|
|
74
192
|
var FIRED = "__iabEscapeFired";
|
|
75
193
|
var hasParam = (url, key) => new URLSearchParams(url.split("#")[0].split("?")[1] ?? "").has(key);
|
|
76
194
|
|
|
77
195
|
class IabEscape {
|
|
78
196
|
options;
|
|
79
|
-
static version = "0.
|
|
80
|
-
static buildTime = "2026-11-
|
|
197
|
+
static version = "0.3.0";
|
|
198
|
+
static buildTime = "2026-11-16T20:42:33.752Z";
|
|
199
|
+
ldr = null;
|
|
200
|
+
fires = 0;
|
|
201
|
+
watching = false;
|
|
202
|
+
wentAway = false;
|
|
203
|
+
locked = false;
|
|
204
|
+
dismissed = false;
|
|
81
205
|
constructor(options) {
|
|
82
206
|
this.options = options;
|
|
83
207
|
this.options = { ...new IabEscapeOptions, ...options };
|
|
@@ -140,20 +264,23 @@ class IabEscape {
|
|
|
140
264
|
return () => {};
|
|
141
265
|
win[FIRED] = true;
|
|
142
266
|
const { document: doc } = win;
|
|
143
|
-
|
|
267
|
+
this.dismissed = false;
|
|
268
|
+
if (this.o.ui)
|
|
269
|
+
this.loader().show();
|
|
270
|
+
this.state("opening");
|
|
144
271
|
let done = false;
|
|
145
272
|
let id;
|
|
146
273
|
const fire = () => {
|
|
147
274
|
done = true;
|
|
148
|
-
if (!this.
|
|
275
|
+
if (!this.fireNow())
|
|
149
276
|
win[FIRED] = false;
|
|
150
277
|
};
|
|
151
278
|
const start = () => {
|
|
152
279
|
doc.removeEventListener("DOMContentLoaded", start);
|
|
153
|
-
if (delay <= 0)
|
|
280
|
+
if (this.delay(rule) <= 0)
|
|
154
281
|
fire();
|
|
155
282
|
else
|
|
156
|
-
id = win.setTimeout(fire, delay);
|
|
283
|
+
id = win.setTimeout(fire, this.delay(rule));
|
|
157
284
|
};
|
|
158
285
|
if (doc.readyState === "loading")
|
|
159
286
|
doc.addEventListener("DOMContentLoaded", start);
|
|
@@ -166,8 +293,84 @@ class IabEscape {
|
|
|
166
293
|
if (id !== undefined)
|
|
167
294
|
win.clearTimeout(id);
|
|
168
295
|
win[FIRED] = false;
|
|
296
|
+
this.ldr?.hide();
|
|
169
297
|
};
|
|
170
298
|
}
|
|
299
|
+
loader() {
|
|
300
|
+
return this.ldr ??= new Loader(this.win.document, { ...DEFAULT_LABELS, ...this.o.labels }, this.o.theme, () => this.dismiss());
|
|
301
|
+
}
|
|
302
|
+
state(phase) {
|
|
303
|
+
this.o.onState?.({ phase, url: this.buildUrl() });
|
|
304
|
+
}
|
|
305
|
+
dismiss() {
|
|
306
|
+
this.dismissed = true;
|
|
307
|
+
this.ldr?.hide();
|
|
308
|
+
this.state("dismissed");
|
|
309
|
+
}
|
|
310
|
+
fallback() {
|
|
311
|
+
const url = this.buildUrl();
|
|
312
|
+
if (url && this.o.ui)
|
|
313
|
+
this.loader().reveal(url);
|
|
314
|
+
this.state("fallback");
|
|
315
|
+
}
|
|
316
|
+
delay(rule) {
|
|
317
|
+
return this.o.delayMs ?? rule.delayMs;
|
|
318
|
+
}
|
|
319
|
+
fireNow() {
|
|
320
|
+
if (!this.escape()) {
|
|
321
|
+
this.ldr?.hide();
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
324
|
+
this.fires++;
|
|
325
|
+
this.watchReentry();
|
|
326
|
+
this.win.setTimeout(() => {
|
|
327
|
+
if (!this.dismissed)
|
|
328
|
+
this.fallback();
|
|
329
|
+
}, REVEAL_MS);
|
|
330
|
+
return true;
|
|
331
|
+
}
|
|
332
|
+
watchReentry() {
|
|
333
|
+
if (this.watching)
|
|
334
|
+
return;
|
|
335
|
+
this.watching = true;
|
|
336
|
+
const { win } = this;
|
|
337
|
+
const doc = win.document;
|
|
338
|
+
win.addEventListener("pagehide", () => {
|
|
339
|
+
this.wentAway = true;
|
|
340
|
+
});
|
|
341
|
+
doc.addEventListener("visibilitychange", () => {
|
|
342
|
+
if (doc.visibilityState === "hidden")
|
|
343
|
+
this.wentAway = true;
|
|
344
|
+
else
|
|
345
|
+
this.reenter();
|
|
346
|
+
});
|
|
347
|
+
win.addEventListener("pageshow", (e) => {
|
|
348
|
+
if (e.persisted) {
|
|
349
|
+
this.wentAway = true;
|
|
350
|
+
this.reenter();
|
|
351
|
+
}
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
reenter() {
|
|
355
|
+
const { win } = this;
|
|
356
|
+
const rule = this.getRule();
|
|
357
|
+
if (!this.wentAway || this.locked || this.dismissed || !rule)
|
|
358
|
+
return;
|
|
359
|
+
this.wentAway = false;
|
|
360
|
+
this.locked = true;
|
|
361
|
+
win.setTimeout(() => {
|
|
362
|
+
this.locked = false;
|
|
363
|
+
}, REENTER_LOCK_MS);
|
|
364
|
+
if (this.fires >= this.o.maxAutoFires)
|
|
365
|
+
return this.fallback();
|
|
366
|
+
if (this.o.ui)
|
|
367
|
+
this.loader().show();
|
|
368
|
+
this.state("reentry");
|
|
369
|
+
if (this.delay(rule) <= 0)
|
|
370
|
+
this.fireNow();
|
|
371
|
+
else
|
|
372
|
+
win.setTimeout(() => this.fireNow(), this.delay(rule));
|
|
373
|
+
}
|
|
171
374
|
bindLink(a) {
|
|
172
375
|
const url = this.buildUrl();
|
|
173
376
|
if (!url)
|
package/dist/iab-escape.min.js
CHANGED
|
@@ -1 +1,11 @@
|
|
|
1
|
-
(()=>{var{defineProperty:d,getOwnPropertyNames:
|
|
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=`
|
|
2
|
+
#iab-escape{position:fixed;inset:0;z-index:2147483647;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:18px;
|
|
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
|
+
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}
|
|
5
|
+
#iab-escape .ie-spin{width:36px;height:36px;border:4px solid #8884;border-top-color:var(--iab-accent,#07f);border-radius:50%;animation:ie-r 1s linear infinite}
|
|
6
|
+
#iab-escape a.ie-open{display:block;padding:14px 28px;border-radius:var(--iab-radius,12px);background:var(--iab-accent,#07f);color:var(--iab-accent-fg,#fff);text-decoration:none;font-weight:600}
|
|
7
|
+
#iab-escape a.ie-stay{font-size:14px;color:inherit;opacity:.7}
|
|
8
|
+
#iab-escape [hidden]{display:none}
|
|
9
|
+
@keyframes ie-r{to{transform:rotate(360deg)}}
|
|
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);})();
|
package/dist/loader.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export interface Labels {
|
|
2
|
+
opening: string;
|
|
3
|
+
open: string;
|
|
4
|
+
stay: string;
|
|
5
|
+
}
|
|
6
|
+
export declare const DEFAULT_LABELS: Labels;
|
|
7
|
+
/** Styling API: the `#iab-escape` root, the classes below, and these CSS variables (all optional). */
|
|
8
|
+
export interface Theme {
|
|
9
|
+
bg?: string;
|
|
10
|
+
fg?: string;
|
|
11
|
+
accent?: string;
|
|
12
|
+
accentFg?: string;
|
|
13
|
+
radius?: string;
|
|
14
|
+
font?: string;
|
|
15
|
+
}
|
|
16
|
+
/** Full-screen "opening in your browser" overlay. No deps; built with createElement only. */
|
|
17
|
+
export declare class Loader {
|
|
18
|
+
private doc;
|
|
19
|
+
private labels;
|
|
20
|
+
private theme;
|
|
21
|
+
private onStay?;
|
|
22
|
+
private root;
|
|
23
|
+
private open;
|
|
24
|
+
private stay;
|
|
25
|
+
private spin;
|
|
26
|
+
constructor(doc: Document, labels: Labels, theme?: Theme, onStay?: (() => void) | undefined);
|
|
27
|
+
get visible(): boolean;
|
|
28
|
+
/** Spinner state (also resets a previously revealed button). */
|
|
29
|
+
show(): void;
|
|
30
|
+
/** Reveals the real `<a>` (so a tap works if the automatic fire was ignored) and the "stay" link. */
|
|
31
|
+
reveal(href: string): void;
|
|
32
|
+
hide(): void;
|
|
33
|
+
private el;
|
|
34
|
+
private build;
|
|
35
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@livx.cc/iab-escape",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.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",
|