@stagepass/loader 1.1.0 → 1.1.2
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 +87 -17
- package/dist/loader.min.js +2 -2
- package/package.json +1 -1
package/dist/loader.js
CHANGED
|
@@ -110,8 +110,15 @@ var Stagepass = (() => {
|
|
|
110
110
|
env,
|
|
111
111
|
domain: dom || hostname,
|
|
112
112
|
timestamp: sessionStartTime,
|
|
113
|
-
version: "1.1.
|
|
113
|
+
version: "1.1.2"
|
|
114
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;
|
|
115
122
|
window.stagepass = stagepassObj;
|
|
116
123
|
globalThis.stagepass = stagepassObj;
|
|
117
124
|
function swapEl(sel, pathAttr, srcAttr, type, skipAttrs) {
|
|
@@ -157,30 +164,93 @@ var Stagepass = (() => {
|
|
|
157
164
|
});
|
|
158
165
|
}
|
|
159
166
|
function loadModules() {
|
|
160
|
-
if (modulesParam === null)
|
|
167
|
+
if (modulesParam === null) {
|
|
168
|
+
if (readyResolve) {
|
|
169
|
+
readyResolve();
|
|
170
|
+
}
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
161
173
|
const modulesToLoad = modulesParam === "" ? ["all"] : modulesParam.split(",").map((m) => m.trim()).filter(Boolean);
|
|
162
174
|
if (modulesToLoad.length === 0) return;
|
|
163
|
-
let baseUrl
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
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
|
+
}
|
|
172
193
|
}
|
|
173
194
|
}
|
|
174
|
-
|
|
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];
|
|
175
233
|
const moduleUrl = `${baseUrl}/${moduleName}.min.js`;
|
|
176
234
|
const script = document.createElement("script");
|
|
235
|
+
script.onload = () => {
|
|
236
|
+
if (env !== "production") {
|
|
237
|
+
splog(`\u{1F4E6} Module loaded: ${moduleName}`);
|
|
238
|
+
}
|
|
239
|
+
currentIndex++;
|
|
240
|
+
loadNext();
|
|
241
|
+
};
|
|
242
|
+
script.onerror = () => {
|
|
243
|
+
hasErrors = true;
|
|
244
|
+
if (env !== "production") {
|
|
245
|
+
sperror(`Failed to load module: ${moduleName} from ${moduleUrl}`);
|
|
246
|
+
}
|
|
247
|
+
currentIndex++;
|
|
248
|
+
loadNext();
|
|
249
|
+
};
|
|
177
250
|
script.src = moduleUrl;
|
|
178
|
-
script.async = true;
|
|
179
251
|
document.head.appendChild(script);
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
}
|
|
183
|
-
});
|
|
252
|
+
}
|
|
253
|
+
loadNext();
|
|
184
254
|
}
|
|
185
255
|
const process = () => {
|
|
186
256
|
swapEl("script[data-stagepass]", "data-stagepass-path", "data-src", "script", ["data-src", "data-stagepass", "data-stagepass-path"]);
|
package/dist/loader.min.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var Stagepass=(()=>{(function(){let
|
|
2
|
-
`);if(
|
|
1
|
+
var Stagepass=(()=>{(function(){let h="stagepass_domain",v="stagepass",M=t=>t==="debug"||t.endsWith(".sp")||t==="localhost"||t.startsWith("localhost:"),y=new URLSearchParams(window.location.search),f=document.querySelector('script[src*="loader"]'),b=null,j=null;if(f&&f.src)try{let t=new URL(f.src);b=t.searchParams.get("modules"),j=t.searchParams.get("silent")}catch{let s=f.src.match(/[?&]modules(=([^&]*))?/);s&&(b=s[2]!==void 0?s[2]:""),f.src.match(/[?&]silent/)&&(j="")}function P(t,...s){var c;if(!localStorage.getItem(h))return;let g=new Date().toTimeString().split(" ")[0],d="";try{let o=(c=new Error().stack)==null?void 0:c.split(`
|
|
2
|
+
`);if(o){let n=o.find((e,u)=>u>2&&!e.includes("log"));if(n){let e=n.match(/:([0-9]+):/);e&&(d=`:${e[1]}`)}}}catch{}let a=`[\u{1F3AB} ${g}${d?" \u{1F4CD}"+d:""}]`;t==="error"?console.log(`%c${a}`,"color: red; font-weight: bold;",...s):console[t]?console[t](a,...s):console.log(a,t,...s)}let p=(...t)=>P("log",...t),k=(...t)=>P("warn",...t),E=(...t)=>P("error",...t);window.splog=p,window.spwarn=k,window.sperror=E;let x=t=>t?t.replace(/^https?:\/\//,"").replace(/\/$/,""):null,C=!1,r=localStorage.getItem(h);if(y.has(v)){let t=y.get(v);if(t==="off"||t==="0"||t==="false")localStorage.removeItem(h),r=null,p("\u{1F50C} Stagepass disconnected.");else if(t){let i=t==="debug"?"debug":x(t);i&&M(i)?(localStorage.setItem(h,i),r=i,p(i==="debug"?"\u{1F50D} Stagepass debug mode enabled.":`\u{1F50C} Stagepass connected to: ${i}`),C=!0):i&&k(`Invalid domain: ${i}. Only .sp domains are allowed.`)}y.delete(v);let s=y.toString();window.history.replaceState({},"",window.location.pathname+(s?`?${s}`:"")+window.location.hash)}r&&!M(r)&&(localStorage.removeItem(h),r=null),r&&!C&&r!=="debug"&&p(`\u{1F50C} Stagepass connected to: ${r}`);let N=!!r,S=!!r&&r!=="debug",L=S?x(r):null,U=S?Date.now():0,W=U,B=U,w=window.location.hostname,H=w==="localhost"||w==="127.0.0.1"||w.startsWith("localhost:")||w.startsWith("127.0.0.1:"),I=S||H,m=I?"local":w.endsWith(".webflow.io")?"staging":"production";!(r&&M(r))&&!y.has(v)&&(m==="production"||m==="staging"&&j!==null)&&(console.log=console.warn=console.error=console.info=console.debug=()=>{});let A=window.stagepass||{};A.vars=Object.freeze({isLocal:I,env:m,domain:L||w,timestamp:B,version:"1.1.2"});let $=null,T=null,V=new Promise((t,s)=>{$=t,T=s});A.ready=V,window.stagepass=A,globalThis.stagepass=A;function D(t,s,i,g,d){document.querySelectorAll(`${t}:not([data-stagepass-processed])`).forEach(a=>{a.setAttribute("data-stagepass-processed","true");let c=a.getAttribute(s),o=a.getAttribute(i);if(!o)return;if(!c&&o&&S&&L)try{let l=new URL(o).pathname.split("/");c=l[l.length-1]||l[l.length-2]}catch{let l=o.match(/\/([^\/\?#]+)(?:\?|#|$)/);l&&(c=l[1])}let n=S&&c&&L?`https://${L}/${c.startsWith("/")?c.substring(1):c}?_cb=${W}`:o;if(!n)return;let e=document.createElement(g);g==="script"?(e.src=n,Array.from(a.attributes).forEach(u=>{let l=u;d.includes(l.name)||e.setAttribute(l.name,l.value)}),!a.hasAttribute("async")&&!a.hasAttribute("defer")&&e.setAttribute("defer","")):(e.rel="stylesheet",e.href=n,Array.from(a.attributes).forEach(u=>{let l=u;d.includes(l.name)||e.setAttribute(l.name,l.value)})),a.parentNode?(a.parentNode.insertBefore(e,a),a.remove()):E(`${g} injection failed: No parent node`)})}function q(){if(b===null){$&&$();return}let t=b===""?["all"]:b.split(",").map(o=>o.trim()).filter(Boolean);if(t.length===0)return;let s,i=document.querySelectorAll('script[src*="modules"], script[src*="all.min.js"], script[src*="inject.min.js"]'),g=!1;for(let o of Array.from(i)){let n=o.src;if(n&&!n.includes("loader.min.js")&&(n.includes("modules.sp")||n.includes("localhost")))try{let e=new URL(n),u=e.pathname.split("/");u.pop(),s=`${e.origin}${u.join("/")}`,g=!0,m!=="production"&&p(`\u{1F4E6} Using locally hosted modules from: ${s}`);break}catch{}}if(!g){let o="@1";if(f&&f.src)try{let n=f.src.match(/@stagepass\/loader(@[^\/\?]+)/);n&&(o=n[1])}catch{}s=`https://cdn.jsdelivr.net/npm/@stagepass/modules${o}/dist`,m!=="production"&&p(`\u{1F4E6} Loading modules from CDN: ${s}`)}let d=0,a=!1;function c(){if(d>=t.length){a?(m!=="production"&&E("\u26A0\uFE0F Some modules failed to load"),T&&T(new Error("One or more modules failed to load"))):(m!=="production"&&p("\u2705 All modules loaded"),$&&$());return}let o=t[d],n=`${s}/${o}.min.js`,e=document.createElement("script");e.onload=()=>{m!=="production"&&p(`\u{1F4E6} Module loaded: ${o}`),d++,c()},e.onerror=()=>{a=!0,m!=="production"&&E(`Failed to load module: ${o} from ${n}`),d++,c()},e.src=n,document.head.appendChild(e)}c()}let O=()=>{D("script[data-stagepass]","data-stagepass-path","data-src","script",["data-src","data-stagepass","data-stagepass-path"]),D('link[rel="stylesheet"][data-stagepass]',"data-stagepass-path","data-href","link",["href","data-href","data-stagepass","data-stagepass-path"])};q(),O(),document.readyState==="loading"&&document.addEventListener("DOMContentLoaded",O);function R(){if(!N)return;let t=document.createElement("div");t.id="stagepass-flag",t.textContent=r==="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(h),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",R):R()})();})();
|