@stagepass/loader 1.1.3 → 1.2.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 +55 -0
- package/dist/loader.js +182 -133
- package/dist/loader.min.js +2 -2
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# `@stagepass/loader`
|
|
2
|
+
|
|
3
|
+
Universal browser runtime for Stagepass.
|
|
4
|
+
|
|
5
|
+
The loader reads Stagepass session state and swaps tagged production assets with local `.sp` assets during development. It also exposes a runtime API via `sp` / `stagepass`.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @stagepass/loader
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## CDN Usage
|
|
14
|
+
|
|
15
|
+
```html
|
|
16
|
+
<script src="https://cdn.jsdelivr.net/npm/@stagepass/loader@1/dist/loader.min.js"></script>
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## What it does
|
|
20
|
+
|
|
21
|
+
- Processes `script[data-stagepass]` and `link[rel="stylesheet"][data-stagepass]`.
|
|
22
|
+
- Uses `data-src` / `data-href` as source URL.
|
|
23
|
+
- Uses `data-stagepass-path` as optional local path override.
|
|
24
|
+
- Adds cache busting in local mode via `?_cb=<timestamp>`.
|
|
25
|
+
- Optional production cache busting with `data-stagepass-cachebust="true"`.
|
|
26
|
+
|
|
27
|
+
## Runtime API
|
|
28
|
+
|
|
29
|
+
Preferred global is `sp` (aliases: `stagepass`, `window.stagepass`).
|
|
30
|
+
|
|
31
|
+
- `sp.vars.isLocal`
|
|
32
|
+
- `sp.vars.env`
|
|
33
|
+
- `sp.vars.domain`
|
|
34
|
+
- `sp.vars.timestamp`
|
|
35
|
+
- `sp.vars.version`
|
|
36
|
+
- `sp.vars.client.*` (theme/browser/os/device/viewport/locale/capabilities)
|
|
37
|
+
- `sp.resolveLocalUrl(relativePath)`
|
|
38
|
+
- `sp.log(...)`, `sp.warn(...)`, `sp.error(...)`
|
|
39
|
+
|
|
40
|
+
Deprecated aliases (still available): `splog`, `spwarn`, `sperror`.
|
|
41
|
+
|
|
42
|
+
## URL Modes
|
|
43
|
+
|
|
44
|
+
- `?stagepass=my-project.sp` - activate local swapping.
|
|
45
|
+
- `?stagepass=debug` - logging only.
|
|
46
|
+
- `?stagepass=off` - deactivate session.
|
|
47
|
+
|
|
48
|
+
## Related Packages
|
|
49
|
+
|
|
50
|
+
- `@stagepass/cli` - creates and manages `.sp` infrastructure.
|
|
51
|
+
- `@stagepass/modules` - optional modules that extend the loader.
|
|
52
|
+
|
|
53
|
+
## Repository
|
|
54
|
+
|
|
55
|
+
- Full docs and examples: [stagepass repository](https://github.com/arobertherz/stagepass)
|
package/dist/loader.js
CHANGED
|
@@ -5,43 +5,30 @@ var Stagepass = (() => {
|
|
|
5
5
|
const P = "stagepass";
|
|
6
6
|
const isValidDomain = (d) => d === "debug" || d.endsWith(".sp") || d === "localhost" || d.startsWith("localhost:");
|
|
7
7
|
const p = new URLSearchParams(window.location.search);
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
let silentParam = null;
|
|
11
|
-
if (loaderScript && loaderScript.src) {
|
|
8
|
+
function getCallerLocation() {
|
|
9
|
+
var _a;
|
|
12
10
|
try {
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
silentParam = "";
|
|
11
|
+
const stack = (_a = new Error().stack) == null ? void 0 : _a.split("\n");
|
|
12
|
+
if (!stack) return "";
|
|
13
|
+
const loaderPattern = /loader(?:\.min)?\.js/i;
|
|
14
|
+
for (let i = 3; i < stack.length; i++) {
|
|
15
|
+
const line = stack[i];
|
|
16
|
+
const match = line.match(/([^/]+\.(?:js|ts))(?:[?#][^:]*)?:(\d+)/);
|
|
17
|
+
if (match) {
|
|
18
|
+
const file = match[1];
|
|
19
|
+
if (!loaderPattern.test(file)) return ` ${file}:${match[2]}`;
|
|
20
|
+
}
|
|
24
21
|
}
|
|
22
|
+
} catch (_) {
|
|
25
23
|
}
|
|
24
|
+
return "";
|
|
26
25
|
}
|
|
27
26
|
function log(level, ...args) {
|
|
28
|
-
var _a;
|
|
29
27
|
const v = localStorage.getItem(K);
|
|
30
28
|
if (!v) return;
|
|
31
29
|
const t = (/* @__PURE__ */ new Date()).toTimeString().split(" ")[0];
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const s = (_a = new Error().stack) == null ? void 0 : _a.split("\n");
|
|
35
|
-
if (s) {
|
|
36
|
-
const l = s.find((x, i) => i > 2 && !x.includes("log"));
|
|
37
|
-
if (l) {
|
|
38
|
-
const m = l.match(/:([0-9]+):/);
|
|
39
|
-
if (m) c = `:${m[1]}`;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
} catch (e) {
|
|
43
|
-
}
|
|
44
|
-
const pre = `[\u{1F3AB} ${t}${c ? " \u{1F4CD}" + c : ""}]`;
|
|
30
|
+
const loc = getCallerLocation();
|
|
31
|
+
const pre = `[\u{1F3AB} ${t}${loc ? " |" + loc : ""}]`;
|
|
45
32
|
if (level === "error") {
|
|
46
33
|
console.log(`%c${pre}`, "color: red; font-weight: bold;", ...args);
|
|
47
34
|
} else if (console[level]) {
|
|
@@ -56,6 +43,16 @@ var Stagepass = (() => {
|
|
|
56
43
|
window.splog = splog;
|
|
57
44
|
window.spwarn = spwarn;
|
|
58
45
|
window.sperror = sperror;
|
|
46
|
+
function spLog(...args) {
|
|
47
|
+
let level = "log";
|
|
48
|
+
if (args.length > 0 && (args[0] === "warn" || args[0] === "error")) {
|
|
49
|
+
level = args[0];
|
|
50
|
+
args = args.slice(1);
|
|
51
|
+
}
|
|
52
|
+
log(level, ...args);
|
|
53
|
+
}
|
|
54
|
+
const spWarn = (...args) => log("warn", ...args);
|
|
55
|
+
const spError = (...args) => log("error", ...args);
|
|
59
56
|
const clean = (d) => d ? d.replace(/^https?:\/\//, "").replace(/\/$/, "") : null;
|
|
60
57
|
let paramProcessed = false;
|
|
61
58
|
let sv = localStorage.getItem(K);
|
|
@@ -99,28 +96,165 @@ var Stagepass = (() => {
|
|
|
99
96
|
const env = isLocal ? "local" : hostname.endsWith(".webflow.io") ? "staging" : "production";
|
|
100
97
|
const hasValidSession = sv && isValidDomain(sv);
|
|
101
98
|
const shouldSuppress = !hasValidSession && !p.has(P);
|
|
102
|
-
if (shouldSuppress &&
|
|
99
|
+
if (shouldSuppress && env === "production") {
|
|
103
100
|
const noop = () => {
|
|
104
101
|
};
|
|
105
102
|
console.log = console.warn = console.error = console.info = console.debug = noop;
|
|
106
103
|
}
|
|
104
|
+
function getBrowserInfo(ua) {
|
|
105
|
+
let family = "unknown";
|
|
106
|
+
let major = null;
|
|
107
|
+
let m = null;
|
|
108
|
+
if (/Edg\/(\d+)/.test(ua)) {
|
|
109
|
+
family = "edge";
|
|
110
|
+
m = ua.match(/Edg\/(\d+)/);
|
|
111
|
+
} else if (/OPR\/(\d+)/.test(ua)) {
|
|
112
|
+
family = "opera";
|
|
113
|
+
m = ua.match(/OPR\/(\d+)/);
|
|
114
|
+
} else if (/Firefox\/(\d+)/.test(ua)) {
|
|
115
|
+
family = "firefox";
|
|
116
|
+
m = ua.match(/Firefox\/(\d+)/);
|
|
117
|
+
} else if (/Chrome\/(\d+)/.test(ua)) {
|
|
118
|
+
family = "chrome";
|
|
119
|
+
m = ua.match(/Chrome\/(\d+)/);
|
|
120
|
+
} else if (/Version\/(\d+).+Safari\//.test(ua)) {
|
|
121
|
+
family = "safari";
|
|
122
|
+
m = ua.match(/Version\/(\d+)/);
|
|
123
|
+
}
|
|
124
|
+
if (m && m[1]) major = Number(m[1]);
|
|
125
|
+
return { family, major: Number.isFinite(major) ? major : null, userAgent: ua };
|
|
126
|
+
}
|
|
127
|
+
function getOsInfo(ua) {
|
|
128
|
+
if (/Android/i.test(ua)) return { family: "android" };
|
|
129
|
+
if (/iPhone|iPad|iPod/i.test(ua)) return { family: "ios" };
|
|
130
|
+
if (/Mac OS X|Macintosh/i.test(ua)) return { family: "macos" };
|
|
131
|
+
if (/Windows NT/i.test(ua)) return { family: "windows" };
|
|
132
|
+
if (/Linux/i.test(ua)) return { family: "linux" };
|
|
133
|
+
return { family: "unknown" };
|
|
134
|
+
}
|
|
135
|
+
function getDeviceInfo(ua) {
|
|
136
|
+
const touch = "ontouchstart" in window || (navigator.maxTouchPoints || 0) > 0;
|
|
137
|
+
let type = "desktop";
|
|
138
|
+
if (/iPad|Tablet/i.test(ua)) type = "tablet";
|
|
139
|
+
else if (/Mobi|Android.+Mobile|iPhone/i.test(ua)) type = "mobile";
|
|
140
|
+
else if (/Android/i.test(ua) && touch) type = "tablet";
|
|
141
|
+
return { type, touch };
|
|
142
|
+
}
|
|
143
|
+
function getThemeInfo() {
|
|
144
|
+
let preferred = "no-preference";
|
|
145
|
+
let current = "unknown";
|
|
146
|
+
try {
|
|
147
|
+
if (window.matchMedia) {
|
|
148
|
+
if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
|
|
149
|
+
preferred = "dark";
|
|
150
|
+
current = "dark";
|
|
151
|
+
} else if (window.matchMedia("(prefers-color-scheme: light)").matches) {
|
|
152
|
+
preferred = "light";
|
|
153
|
+
current = "light";
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
} catch (_) {
|
|
157
|
+
}
|
|
158
|
+
return { preferred, current };
|
|
159
|
+
}
|
|
160
|
+
function hasStorage(type) {
|
|
161
|
+
try {
|
|
162
|
+
const storage = window[type];
|
|
163
|
+
const key = "__stagepass_test__";
|
|
164
|
+
storage.setItem(key, "1");
|
|
165
|
+
storage.removeItem(key);
|
|
166
|
+
return true;
|
|
167
|
+
} catch (_) {
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
function getClientInfo() {
|
|
172
|
+
const ua = navigator.userAgent || "";
|
|
173
|
+
let timezone = null;
|
|
174
|
+
try {
|
|
175
|
+
timezone = Intl.DateTimeFormat().resolvedOptions().timeZone || null;
|
|
176
|
+
} catch (_) {
|
|
177
|
+
}
|
|
178
|
+
return {
|
|
179
|
+
theme: getThemeInfo(),
|
|
180
|
+
browser: getBrowserInfo(ua),
|
|
181
|
+
os: getOsInfo(ua),
|
|
182
|
+
device: getDeviceInfo(ua),
|
|
183
|
+
viewport: {
|
|
184
|
+
width: window.innerWidth || 0,
|
|
185
|
+
height: window.innerHeight || 0,
|
|
186
|
+
dpr: window.devicePixelRatio || 1
|
|
187
|
+
},
|
|
188
|
+
locale: {
|
|
189
|
+
language: navigator.language || "unknown",
|
|
190
|
+
timezone
|
|
191
|
+
},
|
|
192
|
+
capabilities: {
|
|
193
|
+
cookiesEnabled: navigator.cookieEnabled === true,
|
|
194
|
+
localStorage: hasStorage("localStorage"),
|
|
195
|
+
sessionStorage: hasStorage("sessionStorage")
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
}
|
|
107
199
|
const stagepassObj = window.stagepass || {};
|
|
200
|
+
const clientInfo = getClientInfo();
|
|
108
201
|
stagepassObj.vars = Object.freeze({
|
|
109
202
|
isLocal,
|
|
110
203
|
env,
|
|
111
204
|
domain: dom || hostname,
|
|
112
205
|
timestamp: sessionStartTime,
|
|
113
|
-
version: "1.
|
|
206
|
+
version: "1.2.0",
|
|
207
|
+
client: clientInfo
|
|
114
208
|
});
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
209
|
+
stagepassObj.log = spLog;
|
|
210
|
+
stagepassObj.warn = spWarn;
|
|
211
|
+
stagepassObj.error = spError;
|
|
212
|
+
stagepassObj.resolveLocalUrl = (relativePath) => {
|
|
213
|
+
const v = stagepassObj.vars;
|
|
214
|
+
if (!v.isLocal || !v.domain) return "";
|
|
215
|
+
const clean2 = relativePath.startsWith("/") ? relativePath.substring(1) : relativePath;
|
|
216
|
+
return clean2 ? `https://${v.domain}/${clean2}?_cb=${v.timestamp}` : "";
|
|
217
|
+
};
|
|
122
218
|
window.stagepass = stagepassObj;
|
|
123
219
|
globalThis.stagepass = stagepassObj;
|
|
220
|
+
window.sp = stagepassObj;
|
|
221
|
+
globalThis.sp = stagepassObj;
|
|
222
|
+
try {
|
|
223
|
+
if (window.matchMedia) {
|
|
224
|
+
const darkMq = window.matchMedia("(prefers-color-scheme: dark)");
|
|
225
|
+
const lightMq = window.matchMedia("(prefers-color-scheme: light)");
|
|
226
|
+
const updateTheme = () => {
|
|
227
|
+
var _a, _b;
|
|
228
|
+
if (!((_b = (_a = stagepassObj.vars) == null ? void 0 : _a.client) == null ? void 0 : _b.theme)) return;
|
|
229
|
+
if (darkMq.matches) {
|
|
230
|
+
stagepassObj.vars.client.theme.current = "dark";
|
|
231
|
+
stagepassObj.vars.client.theme.preferred = "dark";
|
|
232
|
+
} else if (lightMq.matches) {
|
|
233
|
+
stagepassObj.vars.client.theme.current = "light";
|
|
234
|
+
stagepassObj.vars.client.theme.preferred = "light";
|
|
235
|
+
} else {
|
|
236
|
+
stagepassObj.vars.client.theme.current = "unknown";
|
|
237
|
+
stagepassObj.vars.client.theme.preferred = "no-preference";
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
if (typeof darkMq.addEventListener === "function") {
|
|
241
|
+
darkMq.addEventListener("change", updateTheme);
|
|
242
|
+
lightMq.addEventListener("change", updateTheme);
|
|
243
|
+
} else if (typeof darkMq.addListener === "function") {
|
|
244
|
+
darkMq.addListener(updateTheme);
|
|
245
|
+
lightMq.addListener(updateTheme);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
} catch (_) {
|
|
249
|
+
}
|
|
250
|
+
const updateViewport = () => {
|
|
251
|
+
var _a, _b;
|
|
252
|
+
if (!((_b = (_a = stagepassObj.vars) == null ? void 0 : _a.client) == null ? void 0 : _b.viewport)) return;
|
|
253
|
+
stagepassObj.vars.client.viewport.width = window.innerWidth || 0;
|
|
254
|
+
stagepassObj.vars.client.viewport.height = window.innerHeight || 0;
|
|
255
|
+
stagepassObj.vars.client.viewport.dpr = window.devicePixelRatio || 1;
|
|
256
|
+
};
|
|
257
|
+
window.addEventListener("resize", updateViewport);
|
|
124
258
|
function swapEl(sel, pathAttr, srcAttr, type, skipAttrs) {
|
|
125
259
|
document.querySelectorAll(`${sel}:not([data-stagepass-processed])`).forEach((el) => {
|
|
126
260
|
el.setAttribute("data-stagepass-processed", "true");
|
|
@@ -137,7 +271,14 @@ var Stagepass = (() => {
|
|
|
137
271
|
if (match) lp = match[1];
|
|
138
272
|
}
|
|
139
273
|
}
|
|
140
|
-
|
|
274
|
+
let url;
|
|
275
|
+
if (swap && lp && dom) {
|
|
276
|
+
url = `https://${dom}/${lp.startsWith("/") ? lp.substring(1) : lp}?_cb=${cacheBust}`;
|
|
277
|
+
} else if (el.getAttribute("data-stagepass-cachebust") === "true") {
|
|
278
|
+
url = ps + (ps.includes("?") ? "&" : "?") + "_cb=" + Date.now();
|
|
279
|
+
} else {
|
|
280
|
+
url = ps;
|
|
281
|
+
}
|
|
141
282
|
if (!url) return;
|
|
142
283
|
const n = document.createElement(type);
|
|
143
284
|
if (type === "script") {
|
|
@@ -163,102 +304,10 @@ var Stagepass = (() => {
|
|
|
163
304
|
}
|
|
164
305
|
});
|
|
165
306
|
}
|
|
166
|
-
function loadModules() {
|
|
167
|
-
if (modulesParam === null) {
|
|
168
|
-
if (readyResolve) {
|
|
169
|
-
readyResolve();
|
|
170
|
-
}
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
const modulesToLoad = modulesParam === "" ? ["all"] : modulesParam.split(",").map((m) => m.trim()).filter(Boolean);
|
|
174
|
-
if (modulesToLoad.length === 0) return;
|
|
175
|
-
let baseUrl;
|
|
176
|
-
const existingModuleScripts = document.querySelectorAll('script[src*="modules"], script[src*="all.min.js"], script[src*="inject.min.js"]');
|
|
177
|
-
let useLocalModules = false;
|
|
178
|
-
for (const script of Array.from(existingModuleScripts)) {
|
|
179
|
-
const src = script.src;
|
|
180
|
-
if (src && !src.includes("loader.min.js") && (src.includes("modules.sp") || src.includes("localhost"))) {
|
|
181
|
-
try {
|
|
182
|
-
const scriptUrl = new URL(src);
|
|
183
|
-
const pathParts = scriptUrl.pathname.split("/");
|
|
184
|
-
pathParts.pop();
|
|
185
|
-
baseUrl = `${scriptUrl.origin}${pathParts.join("/")}`;
|
|
186
|
-
useLocalModules = true;
|
|
187
|
-
if (env !== "production") {
|
|
188
|
-
splog(`\u{1F4E6} Using locally hosted modules from: ${baseUrl}`);
|
|
189
|
-
}
|
|
190
|
-
break;
|
|
191
|
-
} catch (e) {
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
if (!useLocalModules) {
|
|
196
|
-
let version = "@1";
|
|
197
|
-
if (loaderScript && loaderScript.src) {
|
|
198
|
-
try {
|
|
199
|
-
const versionMatch = loaderScript.src.match(/@stagepass\/loader(@[^\/\?]+)/);
|
|
200
|
-
if (versionMatch) {
|
|
201
|
-
version = versionMatch[1];
|
|
202
|
-
}
|
|
203
|
-
} catch (e) {
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
baseUrl = `https://cdn.jsdelivr.net/npm/@stagepass/modules${version}/dist`;
|
|
207
|
-
if (env !== "production") {
|
|
208
|
-
splog(`\u{1F4E6} Loading modules from CDN: ${baseUrl}`);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
let currentIndex = 0;
|
|
212
|
-
let hasErrors = false;
|
|
213
|
-
function loadNext() {
|
|
214
|
-
if (currentIndex >= modulesToLoad.length) {
|
|
215
|
-
if (hasErrors) {
|
|
216
|
-
if (env !== "production") {
|
|
217
|
-
sperror("\u26A0\uFE0F Some modules failed to load");
|
|
218
|
-
}
|
|
219
|
-
if (readyReject) {
|
|
220
|
-
readyReject(new Error("One or more modules failed to load"));
|
|
221
|
-
}
|
|
222
|
-
} else {
|
|
223
|
-
if (env !== "production") {
|
|
224
|
-
splog("\u2705 All modules loaded");
|
|
225
|
-
}
|
|
226
|
-
if (readyResolve) {
|
|
227
|
-
readyResolve();
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
return;
|
|
231
|
-
}
|
|
232
|
-
const moduleName = modulesToLoad[currentIndex];
|
|
233
|
-
const isLocalModule = baseUrl.includes(".sp") || baseUrl.includes("localhost");
|
|
234
|
-
const cacheBustParam = swap && isLocalModule ? `?_cb=${cacheBust}` : "";
|
|
235
|
-
const moduleUrl = `${baseUrl}/${moduleName}.min.js${cacheBustParam}`;
|
|
236
|
-
const script = document.createElement("script");
|
|
237
|
-
script.onload = () => {
|
|
238
|
-
if (env !== "production") {
|
|
239
|
-
splog(`\u{1F4E6} Module loaded: ${moduleName}`);
|
|
240
|
-
}
|
|
241
|
-
currentIndex++;
|
|
242
|
-
loadNext();
|
|
243
|
-
};
|
|
244
|
-
script.onerror = () => {
|
|
245
|
-
hasErrors = true;
|
|
246
|
-
if (env !== "production") {
|
|
247
|
-
sperror(`Failed to load module: ${moduleName} from ${moduleUrl}`);
|
|
248
|
-
}
|
|
249
|
-
currentIndex++;
|
|
250
|
-
loadNext();
|
|
251
|
-
};
|
|
252
|
-
script.src = moduleUrl;
|
|
253
|
-
document.head.appendChild(script);
|
|
254
|
-
}
|
|
255
|
-
loadNext();
|
|
256
|
-
}
|
|
257
307
|
const process = () => {
|
|
258
|
-
swapEl("script[data-stagepass]", "data-stagepass-path", "data-src", "script", ["data-src", "data-stagepass", "data-stagepass-path"]);
|
|
259
|
-
swapEl('link[rel="stylesheet"][data-stagepass]', "data-stagepass-path", "data-href", "link", ["href", "data-href", "data-stagepass", "data-stagepass-path"]);
|
|
308
|
+
swapEl("script[data-stagepass]", "data-stagepass-path", "data-src", "script", ["data-src", "data-stagepass", "data-stagepass-path", "data-stagepass-cachebust"]);
|
|
309
|
+
swapEl('link[rel="stylesheet"][data-stagepass]', "data-stagepass-path", "data-href", "link", ["href", "data-href", "data-stagepass", "data-stagepass-path", "data-stagepass-cachebust"]);
|
|
260
310
|
};
|
|
261
|
-
loadModules();
|
|
262
311
|
process();
|
|
263
312
|
if (document.readyState === "loading") {
|
|
264
313
|
document.addEventListener("DOMContentLoaded", process);
|
package/dist/loader.min.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var Stagepass=(()=>{(function(){let
|
|
2
|
-
`);if(
|
|
1
|
+
var Stagepass=(()=>{(function(){let f="stagepass_domain",S="stagepass",k=e=>e==="debug"||e.endsWith(".sp")||e==="localhost"||e.startsWith("localhost:"),m=new URLSearchParams(window.location.search);function O(){var e;try{let t=(e=new Error().stack)==null?void 0:e.split(`
|
|
2
|
+
`);if(!t)return"";let n=/loader(?:\.min)?\.js/i;for(let o=3;o<t.length;o++){let a=t[o].match(/([^/]+\.(?:js|ts))(?:[?#][^:]*)?:(\d+)/);if(a){let c=a[1];if(!n.test(c))return` ${c}:${a[2]}`}}}catch{}return""}function p(e,...t){if(!localStorage.getItem(f))return;let o=new Date().toTimeString().split(" ")[0],l=O(),a=`[\u{1F3AB} ${o}${l?" |"+l:""}]`;e==="error"?console.log(`%c${a}`,"color: red; font-weight: bold;",...t):console[e]?console[e](a,...t):console.log(a,e,...t)}let w=(...e)=>p("log",...e),E=(...e)=>p("warn",...e),L=(...e)=>p("error",...e);window.splog=w,window.spwarn=E,window.sperror=L;function D(...e){let t="log";e.length>0&&(e[0]==="warn"||e[0]==="error")&&(t=e[0],e=e.slice(1)),p(t,...e)}let W=(...e)=>p("warn",...e),j=(...e)=>p("error",...e),A=e=>e?e.replace(/^https?:\/\//,"").replace(/\/$/,""):null,C=!1,i=localStorage.getItem(f);if(m.has(S)){let e=m.get(S);if(e==="off"||e==="0"||e==="false")localStorage.removeItem(f),i=null,w("\u{1F50C} Stagepass disconnected.");else if(e){let n=e==="debug"?"debug":A(e);n&&k(n)?(localStorage.setItem(f,n),i=n,w(n==="debug"?"\u{1F50D} Stagepass debug mode enabled.":`\u{1F50C} Stagepass connected to: ${n}`),C=!0):n&&E(`Invalid domain: ${n}. Only .sp domains are allowed.`)}m.delete(S);let t=m.toString();window.history.replaceState({},"",window.location.pathname+(t?`?${t}`:"")+window.location.hash)}i&&!k(i)&&(localStorage.removeItem(f),i=null),i&&!C&&i!=="debug"&&w(`\u{1F50C} Stagepass connected to: ${i}`);let R=!!i,b=!!i&&i!=="debug",v=b?A(i):null,I=b?Date.now():0,V=I,F=I,h=window.location.hostname,N=h==="localhost"||h==="127.0.0.1"||h.startsWith("localhost:")||h.startsWith("127.0.0.1:"),T=b||N,M=T?"local":h.endsWith(".webflow.io")?"staging":"production";!(i&&k(i))&&!m.has(S)&&M==="production"&&(console.log=console.warn=console.error=console.info=console.debug=()=>{});function z(e){let t="unknown",n=null,o=null;return/Edg\/(\d+)/.test(e)?(t="edge",o=e.match(/Edg\/(\d+)/)):/OPR\/(\d+)/.test(e)?(t="opera",o=e.match(/OPR\/(\d+)/)):/Firefox\/(\d+)/.test(e)?(t="firefox",o=e.match(/Firefox\/(\d+)/)):/Chrome\/(\d+)/.test(e)?(t="chrome",o=e.match(/Chrome\/(\d+)/)):/Version\/(\d+).+Safari\//.test(e)&&(t="safari",o=e.match(/Version\/(\d+)/)),o&&o[1]&&(n=Number(o[1])),{family:t,major:Number.isFinite(n)?n:null,userAgent:e}}function B(e){return/Android/i.test(e)?{family:"android"}:/iPhone|iPad|iPod/i.test(e)?{family:"ios"}:/Mac OS X|Macintosh/i.test(e)?{family:"macos"}:/Windows NT/i.test(e)?{family:"windows"}:/Linux/i.test(e)?{family:"linux"}:{family:"unknown"}}function H(e){let t="ontouchstart"in window||(navigator.maxTouchPoints||0)>0,n="desktop";return/iPad|Tablet/i.test(e)?n="tablet":/Mobi|Android.+Mobile|iPhone/i.test(e)?n="mobile":/Android/i.test(e)&&t&&(n="tablet"),{type:n,touch:t}}function U(){let e="no-preference",t="unknown";try{window.matchMedia&&(window.matchMedia("(prefers-color-scheme: dark)").matches?(e="dark",t="dark"):window.matchMedia("(prefers-color-scheme: light)").matches&&(e="light",t="light"))}catch{}return{preferred:e,current:t}}function x(e){try{let t=window[e],n="__stagepass_test__";return t.setItem(n,"1"),t.removeItem(n),!0}catch{return!1}}function q(){let e=navigator.userAgent||"",t=null;try{t=Intl.DateTimeFormat().resolvedOptions().timeZone||null}catch{}return{theme:U(),browser:z(e),os:B(e),device:H(e),viewport:{width:window.innerWidth||0,height:window.innerHeight||0,dpr:window.devicePixelRatio||1},locale:{language:navigator.language||"unknown",timezone:t},capabilities:{cookiesEnabled:navigator.cookieEnabled===!0,localStorage:x("localStorage"),sessionStorage:x("sessionStorage")}}}let s=window.stagepass||{},G=q();s.vars=Object.freeze({isLocal:T,env:M,domain:v||h,timestamp:F,version:"1.2.0",client:G}),s.log=D,s.warn=W,s.error=j,s.resolveLocalUrl=e=>{let t=s.vars;if(!t.isLocal||!t.domain)return"";let n=e.startsWith("/")?e.substring(1):e;return n?`https://${t.domain}/${n}?_cb=${t.timestamp}`:""},window.stagepass=s,globalThis.stagepass=s,window.sp=s,globalThis.sp=s;try{if(window.matchMedia){let e=window.matchMedia("(prefers-color-scheme: dark)"),t=window.matchMedia("(prefers-color-scheme: light)"),n=()=>{var o,l;(l=(o=s.vars)==null?void 0:o.client)!=null&&l.theme&&(e.matches?(s.vars.client.theme.current="dark",s.vars.client.theme.preferred="dark"):t.matches?(s.vars.client.theme.current="light",s.vars.client.theme.preferred="light"):(s.vars.client.theme.current="unknown",s.vars.client.theme.preferred="no-preference"))};typeof e.addEventListener=="function"?(e.addEventListener("change",n),t.addEventListener("change",n)):typeof e.addListener=="function"&&(e.addListener(n),t.addListener(n))}}catch{}let K=()=>{var e,t;(t=(e=s.vars)==null?void 0:e.client)!=null&&t.viewport&&(s.vars.client.viewport.width=window.innerWidth||0,s.vars.client.viewport.height=window.innerHeight||0,s.vars.client.viewport.dpr=window.devicePixelRatio||1)};window.addEventListener("resize",K);function $(e,t,n,o,l){document.querySelectorAll(`${e}:not([data-stagepass-processed])`).forEach(a=>{a.setAttribute("data-stagepass-processed","true");let c=a.getAttribute(t),d=a.getAttribute(n);if(!d)return;if(!c&&d&&b&&v)try{let r=new URL(d).pathname.split("/");c=r[r.length-1]||r[r.length-2]}catch{let r=d.match(/\/([^\/\?#]+)(?:\?|#|$)/);r&&(c=r[1])}let u;if(b&&c&&v?u=`https://${v}/${c.startsWith("/")?c.substring(1):c}?_cb=${V}`:a.getAttribute("data-stagepass-cachebust")==="true"?u=d+(d.includes("?")?"&":"?")+"_cb="+Date.now():u=d,!u)return;let g=document.createElement(o);o==="script"?(g.src=u,Array.from(a.attributes).forEach(y=>{let r=y;l.includes(r.name)||g.setAttribute(r.name,r.value)}),!a.hasAttribute("async")&&!a.hasAttribute("defer")&&g.setAttribute("defer","")):(g.rel="stylesheet",g.href=u,Array.from(a.attributes).forEach(y=>{let r=y;l.includes(r.name)||g.setAttribute(r.name,r.value)})),a.parentNode?(a.parentNode.insertBefore(g,a),a.remove()):L(`${o} injection failed: No parent node`)})}let P=()=>{$("script[data-stagepass]","data-stagepass-path","data-src","script",["data-src","data-stagepass","data-stagepass-path","data-stagepass-cachebust"]),$('link[rel="stylesheet"][data-stagepass]',"data-stagepass-path","data-href","link",["href","data-href","data-stagepass","data-stagepass-path","data-stagepass-cachebust"])};P(),document.readyState==="loading"&&document.addEventListener("DOMContentLoaded",P);function _(){if(!R)return;let e=document.createElement("div");e.id="stagepass-flag",e.textContent=i==="debug"?"STAGEPASS DEBUG":"STAGEPASS ACTIVE",e.style.cssText='font:12px/1 -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif;font-weight:bold;color:#fff;background:#D92662;padding:4px 6px;z-index:9999999999;position:fixed;top:0;right:0;cursor:pointer;border-radius:0 0 0 4px;user-select:none;',e.addEventListener("click",()=>{localStorage.removeItem(f),w("\u{1F50C} Stagepass disconnected."),window.location.reload()}),document.body?document.body.appendChild(e):document.addEventListener("DOMContentLoaded",()=>{document.body&&document.body.appendChild(e)})}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",_):_()})();})();
|