@stagepass/loader 1.0.2 → 1.1.1
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/dist/loader.js +139 -60
- package/dist/loader.min.js +2 -2
- package/package.json +1 -1
package/dist/loader.js
CHANGED
|
@@ -3,40 +3,45 @@ var Stagepass = (() => {
|
|
|
3
3
|
(function() {
|
|
4
4
|
const K = "stagepass_domain";
|
|
5
5
|
const P = "stagepass";
|
|
6
|
-
|
|
7
|
-
return d === "debug" || d.endsWith(".sp") || d === "localhost" || d.startsWith("localhost:");
|
|
8
|
-
}
|
|
6
|
+
const isValidDomain = (d) => d === "debug" || d.endsWith(".sp") || d === "localhost" || d.startsWith("localhost:");
|
|
9
7
|
const p = new URLSearchParams(window.location.search);
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
if (
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
8
|
+
const loaderScript = document.querySelector('script[src*="loader"]');
|
|
9
|
+
let modulesParam = null;
|
|
10
|
+
let silentParam = null;
|
|
11
|
+
if (loaderScript && loaderScript.src) {
|
|
12
|
+
try {
|
|
13
|
+
const scriptUrl = new URL(loaderScript.src);
|
|
14
|
+
modulesParam = scriptUrl.searchParams.get("modules");
|
|
15
|
+
silentParam = scriptUrl.searchParams.get("silent");
|
|
16
|
+
} catch (e) {
|
|
17
|
+
const modulesMatch = loaderScript.src.match(/[?&]modules(=([^&]*))?/);
|
|
18
|
+
if (modulesMatch) {
|
|
19
|
+
modulesParam = modulesMatch[2] !== void 0 ? modulesMatch[2] : "";
|
|
20
|
+
}
|
|
21
|
+
const silentMatch = loaderScript.src.match(/[?&]silent/);
|
|
22
|
+
if (silentMatch) {
|
|
23
|
+
silentParam = "";
|
|
24
|
+
}
|
|
25
|
+
}
|
|
21
26
|
}
|
|
22
27
|
function log(level, ...args) {
|
|
23
28
|
var _a;
|
|
24
29
|
const v = localStorage.getItem(K);
|
|
25
|
-
if (!v
|
|
30
|
+
if (!v) return;
|
|
26
31
|
const t = (/* @__PURE__ */ new Date()).toTimeString().split(" ")[0];
|
|
27
|
-
let c = "
|
|
32
|
+
let c = "";
|
|
28
33
|
try {
|
|
29
34
|
const s = (_a = new Error().stack) == null ? void 0 : _a.split("\n");
|
|
30
35
|
if (s) {
|
|
31
36
|
const l = s.find((x, i) => i > 2 && !x.includes("log"));
|
|
32
37
|
if (l) {
|
|
33
|
-
const m = l.match(
|
|
34
|
-
if (m) c =
|
|
38
|
+
const m = l.match(/:([0-9]+):/);
|
|
39
|
+
if (m) c = `:${m[1]}`;
|
|
35
40
|
}
|
|
36
41
|
}
|
|
37
42
|
} catch (e) {
|
|
38
43
|
}
|
|
39
|
-
const pre = `[\u{1F3AB} ${t} \u{1F4CD}
|
|
44
|
+
const pre = `[\u{1F3AB} ${t}${c ? " \u{1F4CD}" + c : ""}]`;
|
|
40
45
|
if (level === "error") {
|
|
41
46
|
console.log(`%c${pre}`, "color: red; font-weight: bold;", ...args);
|
|
42
47
|
} else if (console[level]) {
|
|
@@ -51,22 +56,23 @@ var Stagepass = (() => {
|
|
|
51
56
|
window.splog = splog;
|
|
52
57
|
window.spwarn = spwarn;
|
|
53
58
|
window.sperror = sperror;
|
|
54
|
-
|
|
55
|
-
return d ? d.replace(/^https?:\/\//, "").replace(/\/$/, "") : null;
|
|
56
|
-
}
|
|
59
|
+
const clean = (d) => d ? d.replace(/^https?:\/\//, "").replace(/\/$/, "") : null;
|
|
57
60
|
let paramProcessed = false;
|
|
61
|
+
let sv = localStorage.getItem(K);
|
|
58
62
|
if (p.has(P)) {
|
|
59
63
|
const v = p.get(P);
|
|
60
64
|
if (v === "off" || v === "0" || v === "false") {
|
|
61
65
|
localStorage.removeItem(K);
|
|
66
|
+
sv = null;
|
|
62
67
|
splog("\u{1F50C} Stagepass disconnected.");
|
|
63
68
|
} else if (v) {
|
|
64
69
|
const c = v === "debug" ? "debug" : clean(v);
|
|
65
70
|
if (c && isValidDomain(c)) {
|
|
66
71
|
localStorage.setItem(K, c);
|
|
72
|
+
sv = c;
|
|
67
73
|
splog(c === "debug" ? "\u{1F50D} Stagepass debug mode enabled." : `\u{1F50C} Stagepass connected to: ${c}`);
|
|
68
74
|
paramProcessed = true;
|
|
69
|
-
} else if (c
|
|
75
|
+
} else if (c) {
|
|
70
76
|
spwarn(`Invalid domain: ${c}. Only .sp domains are allowed.`);
|
|
71
77
|
}
|
|
72
78
|
}
|
|
@@ -74,7 +80,6 @@ var Stagepass = (() => {
|
|
|
74
80
|
const s = p.toString();
|
|
75
81
|
window.history.replaceState({}, "", window.location.pathname + (s ? `?${s}` : "") + window.location.hash);
|
|
76
82
|
}
|
|
77
|
-
let sv = localStorage.getItem(K);
|
|
78
83
|
if (sv && !isValidDomain(sv)) {
|
|
79
84
|
localStorage.removeItem(K);
|
|
80
85
|
sv = null;
|
|
@@ -85,10 +90,39 @@ var Stagepass = (() => {
|
|
|
85
90
|
const dev = !!sv;
|
|
86
91
|
const swap = !!sv && sv !== "debug";
|
|
87
92
|
const dom = swap ? clean(sv) : null;
|
|
88
|
-
const
|
|
93
|
+
const now = swap ? Date.now() : 0;
|
|
94
|
+
const cacheBust = now;
|
|
95
|
+
const sessionStartTime = now;
|
|
96
|
+
const hostname = window.location.hostname;
|
|
97
|
+
const isLocalhost = hostname === "localhost" || hostname === "127.0.0.1" || hostname.startsWith("localhost:") || hostname.startsWith("127.0.0.1:");
|
|
98
|
+
const isLocal = swap || isLocalhost;
|
|
99
|
+
const env = isLocal ? "local" : hostname.endsWith(".webflow.io") ? "staging" : "production";
|
|
100
|
+
const hasValidSession = sv && isValidDomain(sv);
|
|
101
|
+
const shouldSuppress = !hasValidSession && !p.has(P);
|
|
102
|
+
if (shouldSuppress && (env === "production" || env === "staging" && silentParam !== null)) {
|
|
103
|
+
const noop = () => {
|
|
104
|
+
};
|
|
105
|
+
console.log = console.warn = console.error = console.info = console.debug = noop;
|
|
106
|
+
}
|
|
107
|
+
const stagepassObj = window.stagepass || {};
|
|
108
|
+
stagepassObj.vars = Object.freeze({
|
|
109
|
+
isLocal,
|
|
110
|
+
env,
|
|
111
|
+
domain: dom || hostname,
|
|
112
|
+
timestamp: sessionStartTime,
|
|
113
|
+
version: "1.1.0"
|
|
114
|
+
});
|
|
115
|
+
let readyResolve = null;
|
|
116
|
+
let readyReject = null;
|
|
117
|
+
const readyPromise = new Promise((resolve, reject) => {
|
|
118
|
+
readyResolve = resolve;
|
|
119
|
+
readyReject = reject;
|
|
120
|
+
});
|
|
121
|
+
stagepassObj.ready = readyPromise;
|
|
122
|
+
window.stagepass = stagepassObj;
|
|
123
|
+
globalThis.stagepass = stagepassObj;
|
|
89
124
|
function swapEl(sel, pathAttr, srcAttr, type, skipAttrs) {
|
|
90
|
-
|
|
91
|
-
els.forEach((el) => {
|
|
125
|
+
document.querySelectorAll(`${sel}:not([data-stagepass-processed])`).forEach((el) => {
|
|
92
126
|
el.setAttribute("data-stagepass-processed", "true");
|
|
93
127
|
let lp = el.getAttribute(pathAttr);
|
|
94
128
|
const ps = el.getAttribute(srcAttr);
|
|
@@ -103,45 +137,90 @@ var Stagepass = (() => {
|
|
|
103
137
|
if (match) lp = match[1];
|
|
104
138
|
}
|
|
105
139
|
}
|
|
106
|
-
|
|
107
|
-
if (
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
140
|
+
const url = swap && lp && dom ? `https://${dom}/${lp.startsWith("/") ? lp.substring(1) : lp}?_cb=${cacheBust}` : ps;
|
|
141
|
+
if (!url) return;
|
|
142
|
+
const n = document.createElement(type);
|
|
143
|
+
if (type === "script") {
|
|
144
|
+
n.src = url;
|
|
145
|
+
Array.from(el.attributes).forEach((a) => {
|
|
146
|
+
const attr = a;
|
|
147
|
+
if (!skipAttrs.includes(attr.name)) n.setAttribute(attr.name, attr.value);
|
|
148
|
+
});
|
|
149
|
+
if (!el.hasAttribute("async") && !el.hasAttribute("defer")) n.setAttribute("defer", "");
|
|
150
|
+
} else {
|
|
151
|
+
n.rel = "stylesheet";
|
|
152
|
+
n.href = url;
|
|
153
|
+
Array.from(el.attributes).forEach((a) => {
|
|
154
|
+
const attr = a;
|
|
155
|
+
if (!skipAttrs.includes(attr.name)) n.setAttribute(attr.name, attr.value);
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
if (el.parentNode) {
|
|
159
|
+
el.parentNode.insertBefore(n, el);
|
|
160
|
+
el.remove();
|
|
161
|
+
} else {
|
|
162
|
+
sperror(`${type} injection failed: No parent node`);
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
}
|
|
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 = window.location.origin;
|
|
176
|
+
if (loaderScript && loaderScript.src) {
|
|
177
|
+
try {
|
|
178
|
+
const scriptUrl = new URL(loaderScript.src);
|
|
179
|
+
const pathParts = scriptUrl.pathname.split("/");
|
|
180
|
+
pathParts.pop();
|
|
181
|
+
baseUrl = `${scriptUrl.origin}${pathParts.join("/")}`;
|
|
182
|
+
} catch (e) {
|
|
183
|
+
baseUrl = loaderScript.src.replace(/\/[^\/\?]+(\?.*)?$/, "");
|
|
112
184
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
if (!skipAttrs.includes(attr.name)) n.setAttribute(attr.name, attr.value);
|
|
120
|
-
});
|
|
121
|
-
if (!el.hasAttribute("async") && !el.hasAttribute("defer")) {
|
|
122
|
-
n.setAttribute("defer", "");
|
|
123
|
-
}
|
|
124
|
-
} else {
|
|
125
|
-
n.rel = "stylesheet";
|
|
126
|
-
n.href = url;
|
|
127
|
-
Array.from(el.attributes).forEach((a) => {
|
|
128
|
-
const attr = a;
|
|
129
|
-
if (!skipAttrs.includes(attr.name)) n.setAttribute(attr.name, attr.value);
|
|
130
|
-
});
|
|
185
|
+
}
|
|
186
|
+
let currentIndex = 0;
|
|
187
|
+
function loadNext() {
|
|
188
|
+
if (currentIndex >= modulesToLoad.length) {
|
|
189
|
+
if (env !== "production") {
|
|
190
|
+
splog("\u2705 All modules loaded");
|
|
131
191
|
}
|
|
132
|
-
if (
|
|
133
|
-
|
|
134
|
-
el.remove();
|
|
135
|
-
} else {
|
|
136
|
-
sperror(`${type} injection failed: No parent node`);
|
|
192
|
+
if (readyResolve) {
|
|
193
|
+
readyResolve();
|
|
137
194
|
}
|
|
195
|
+
return;
|
|
138
196
|
}
|
|
139
|
-
|
|
197
|
+
const moduleName = modulesToLoad[currentIndex];
|
|
198
|
+
const moduleUrl = `${baseUrl}/${moduleName}.min.js`;
|
|
199
|
+
const script = document.createElement("script");
|
|
200
|
+
script.onload = () => {
|
|
201
|
+
if (env !== "production") {
|
|
202
|
+
splog(`\u{1F4E6} Module loaded: ${moduleName}`);
|
|
203
|
+
}
|
|
204
|
+
currentIndex++;
|
|
205
|
+
loadNext();
|
|
206
|
+
};
|
|
207
|
+
script.onerror = () => {
|
|
208
|
+
if (env !== "production") {
|
|
209
|
+
sperror(`Failed to load module: ${moduleName}`);
|
|
210
|
+
}
|
|
211
|
+
currentIndex++;
|
|
212
|
+
loadNext();
|
|
213
|
+
};
|
|
214
|
+
script.src = moduleUrl;
|
|
215
|
+
document.head.appendChild(script);
|
|
216
|
+
}
|
|
217
|
+
loadNext();
|
|
140
218
|
}
|
|
141
|
-
|
|
219
|
+
const process = () => {
|
|
142
220
|
swapEl("script[data-stagepass]", "data-stagepass-path", "data-src", "script", ["data-src", "data-stagepass", "data-stagepass-path"]);
|
|
143
221
|
swapEl('link[rel="stylesheet"][data-stagepass]', "data-stagepass-path", "data-href", "link", ["href", "data-href", "data-stagepass", "data-stagepass-path"]);
|
|
144
|
-
}
|
|
222
|
+
};
|
|
223
|
+
loadModules();
|
|
145
224
|
process();
|
|
146
225
|
if (document.readyState === "loading") {
|
|
147
226
|
document.addEventListener("DOMContentLoaded", process);
|
|
@@ -151,7 +230,7 @@ var Stagepass = (() => {
|
|
|
151
230
|
const f = document.createElement("div");
|
|
152
231
|
f.id = "stagepass-flag";
|
|
153
232
|
f.textContent = sv === "debug" ? "STAGEPASS DEBUG" : "STAGEPASS ACTIVE";
|
|
154
|
-
f.style.cssText =
|
|
233
|
+
f.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;';
|
|
155
234
|
f.addEventListener("click", () => {
|
|
156
235
|
localStorage.removeItem(K);
|
|
157
236
|
splog("\u{1F50C} Stagepass disconnected.");
|
package/dist/loader.min.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var Stagepass=(()=>{(function(){let
|
|
2
|
-
`);if(
|
|
1
|
+
var Stagepass=(()=>{(function(){let m="stagepass_domain",v="stagepass",A=t=>t==="debug"||t.endsWith(".sp")||t==="localhost"||t.startsWith("localhost:"),b=new URLSearchParams(window.location.search),l=document.querySelector('script[src*="loader"]'),y=null,M=null;if(l&&l.src)try{let t=new URL(l.src);y=t.searchParams.get("modules"),M=t.searchParams.get("silent")}catch{let s=l.src.match(/[?&]modules(=([^&]*))?/);s&&(y=s[2]!==void 0?s[2]:""),l.src.match(/[?&]silent/)&&(M="")}function P(t,...s){var r;if(!localStorage.getItem(m))return;let d=new Date().toTimeString().split(" ")[0],n="";try{let u=(r=new Error().stack)==null?void 0:r.split(`
|
|
2
|
+
`);if(u){let w=u.find((i,g)=>g>2&&!i.includes("log"));if(w){let i=w.match(/:([0-9]+):/);i&&(n=`:${i[1]}`)}}}catch{}let e=`[\u{1F3AB} ${d}${n?" \u{1F4CD}"+n:""}]`;t==="error"?console.log(`%c${e}`,"color: red; font-weight: bold;",...s):console[t]?console[t](e,...s):console.log(e,t,...s)}let p=(...t)=>P("log",...t),k=(...t)=>P("warn",...t),T=(...t)=>P("error",...t);window.splog=p,window.spwarn=k,window.sperror=T;let x=t=>t?t.replace(/^https?:\/\//,"").replace(/\/$/,""):null,C=!1,a=localStorage.getItem(m);if(b.has(v)){let t=b.get(v);if(t==="off"||t==="0"||t==="false")localStorage.removeItem(m),a=null,p("\u{1F50C} Stagepass disconnected.");else if(t){let o=t==="debug"?"debug":x(t);o&&A(o)?(localStorage.setItem(m,o),a=o,p(o==="debug"?"\u{1F50D} Stagepass debug mode enabled.":`\u{1F50C} Stagepass connected to: ${o}`),C=!0):o&&k(`Invalid domain: ${o}. Only .sp domains are allowed.`)}b.delete(v);let s=b.toString();window.history.replaceState({},"",window.location.pathname+(s?`?${s}`:"")+window.location.hash)}a&&!A(a)&&(localStorage.removeItem(m),a=null),a&&!C&&a!=="debug"&&p(`\u{1F50C} Stagepass connected to: ${a}`);let R=!!a,S=!!a&&a!=="debug",E=S?x(a):null,I=S?Date.now():0,W=I,B=I,f=window.location.hostname,N=f==="localhost"||f==="127.0.0.1"||f.startsWith("localhost:")||f.startsWith("127.0.0.1:"),U=S||N,h=U?"local":f.endsWith(".webflow.io")?"staging":"production";!(a&&A(a))&&!b.has(v)&&(h==="production"||h==="staging"&&M!==null)&&(console.log=console.warn=console.error=console.info=console.debug=()=>{});let L=window.stagepass||{};L.vars=Object.freeze({isLocal:U,env:h,domain:E||f,timestamp:B,version:"1.1.0"});let $=null,H=null,V=new Promise((t,s)=>{$=t,H=s});L.ready=V,window.stagepass=L,globalThis.stagepass=L;function j(t,s,o,d,n){document.querySelectorAll(`${t}:not([data-stagepass-processed])`).forEach(e=>{e.setAttribute("data-stagepass-processed","true");let r=e.getAttribute(s),u=e.getAttribute(o);if(!u)return;if(!r&&u&&S&&E)try{let c=new URL(u).pathname.split("/");r=c[c.length-1]||c[c.length-2]}catch{let c=u.match(/\/([^\/\?#]+)(?:\?|#|$)/);c&&(r=c[1])}let w=S&&r&&E?`https://${E}/${r.startsWith("/")?r.substring(1):r}?_cb=${W}`:u;if(!w)return;let i=document.createElement(d);d==="script"?(i.src=w,Array.from(e.attributes).forEach(g=>{let c=g;n.includes(c.name)||i.setAttribute(c.name,c.value)}),!e.hasAttribute("async")&&!e.hasAttribute("defer")&&i.setAttribute("defer","")):(i.rel="stylesheet",i.href=w,Array.from(e.attributes).forEach(g=>{let c=g;n.includes(c.name)||i.setAttribute(c.name,c.value)})),e.parentNode?(e.parentNode.insertBefore(i,e),e.remove()):T(`${d} injection failed: No parent node`)})}function F(){if(y===null){$&&$();return}let t=y===""?["all"]:y.split(",").map(n=>n.trim()).filter(Boolean);if(t.length===0)return;let s=window.location.origin;if(l&&l.src)try{let n=new URL(l.src),e=n.pathname.split("/");e.pop(),s=`${n.origin}${e.join("/")}`}catch{s=l.src.replace(/\/[^\/\?]+(\?.*)?$/,"")}let o=0;function d(){if(o>=t.length){h!=="production"&&p("\u2705 All modules loaded"),$&&$();return}let n=t[o],e=`${s}/${n}.min.js`,r=document.createElement("script");r.onload=()=>{h!=="production"&&p(`\u{1F4E6} Module loaded: ${n}`),o++,d()},r.onerror=()=>{h!=="production"&&T(`Failed to load module: ${n}`),o++,d()},r.src=e,document.head.appendChild(r)}d()}let D=()=>{j("script[data-stagepass]","data-stagepass-path","data-src","script",["data-src","data-stagepass","data-stagepass-path"]),j('link[rel="stylesheet"][data-stagepass]',"data-stagepass-path","data-href","link",["href","data-href","data-stagepass","data-stagepass-path"])};F(),D(),document.readyState==="loading"&&document.addEventListener("DOMContentLoaded",D);function O(){if(!R)return;let t=document.createElement("div");t.id="stagepass-flag",t.textContent=a==="debug"?"STAGEPASS DEBUG":"STAGEPASS ACTIVE",t.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;',t.addEventListener("click",()=>{localStorage.removeItem(m),p("\u{1F50C} Stagepass disconnected."),window.location.reload()}),document.body?document.body.appendChild(t):document.addEventListener("DOMContentLoaded",()=>{document.body&&document.body.appendChild(t)})}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",O):O()})();})();
|