@stagepass/loader 1.0.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/dist/loader.js +171 -0
- package/dist/loader.min.js +2 -0
- package/package.json +31 -0
package/dist/loader.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
var Stagepass = (() => {
|
|
2
|
+
// src/index.ts
|
|
3
|
+
(function() {
|
|
4
|
+
const K = "stagepass_domain";
|
|
5
|
+
const P = "stagepass";
|
|
6
|
+
const p = new URLSearchParams(window.location.search);
|
|
7
|
+
const svCheck = localStorage.getItem(K);
|
|
8
|
+
if (!svCheck && !p.has(P)) {
|
|
9
|
+
const noop = () => {
|
|
10
|
+
};
|
|
11
|
+
console.log = noop;
|
|
12
|
+
console.warn = noop;
|
|
13
|
+
console.error = noop;
|
|
14
|
+
console.info = noop;
|
|
15
|
+
console.debug = noop;
|
|
16
|
+
}
|
|
17
|
+
function log(level, ...args) {
|
|
18
|
+
var _a;
|
|
19
|
+
const v = localStorage.getItem(K);
|
|
20
|
+
if (!v || v !== "debug" && !v) return;
|
|
21
|
+
const t = (/* @__PURE__ */ new Date()).toTimeString().split(" ")[0];
|
|
22
|
+
let c = "unknown";
|
|
23
|
+
try {
|
|
24
|
+
const s = (_a = new Error().stack) == null ? void 0 : _a.split("\n");
|
|
25
|
+
if (s) {
|
|
26
|
+
const l = s.find((x, i) => i > 2 && !x.includes("log"));
|
|
27
|
+
if (l) {
|
|
28
|
+
const m = l.match(/(\/?([^\/\?:]+))(\?[^\s)]*)?:([0-9]+):[0-9]+/);
|
|
29
|
+
if (m) c = `${m[2]}:${m[4]}`;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
} catch (e) {
|
|
33
|
+
}
|
|
34
|
+
const pre = `[\u{1F3AB} ${t} \u{1F4CD} ${c}]`;
|
|
35
|
+
if (level === "error") {
|
|
36
|
+
console.log(`%c${pre}`, "color: red; font-weight: bold;", ...args);
|
|
37
|
+
} else if (console[level]) {
|
|
38
|
+
console[level](pre, ...args);
|
|
39
|
+
} else {
|
|
40
|
+
console.log(pre, level, ...args);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const splog = (...args) => log("log", ...args);
|
|
44
|
+
const spwarn = (...args) => log("warn", ...args);
|
|
45
|
+
const sperror = (...args) => log("error", ...args);
|
|
46
|
+
window.splog = splog;
|
|
47
|
+
window.spwarn = spwarn;
|
|
48
|
+
window.sperror = sperror;
|
|
49
|
+
function clean(d) {
|
|
50
|
+
return d ? d.replace(/^https?:\/\//, "").replace(/\/$/, "") : null;
|
|
51
|
+
}
|
|
52
|
+
function isValidDomain(d) {
|
|
53
|
+
return d === "debug" || d.endsWith(".sp") || d === "localhost" || d.startsWith("localhost:");
|
|
54
|
+
}
|
|
55
|
+
let paramProcessed = false;
|
|
56
|
+
if (p.has(P)) {
|
|
57
|
+
const v = p.get(P);
|
|
58
|
+
if (v === "off" || v === "0" || v === "false") {
|
|
59
|
+
localStorage.removeItem(K);
|
|
60
|
+
splog("\u{1F50C} Stagepass disconnected.");
|
|
61
|
+
} else if (v) {
|
|
62
|
+
const c = v === "debug" ? "debug" : clean(v);
|
|
63
|
+
if (c && isValidDomain(c)) {
|
|
64
|
+
localStorage.setItem(K, c);
|
|
65
|
+
splog(c === "debug" ? "\u{1F50D} Stagepass debug mode enabled." : `\u{1F50C} Stagepass connected to: ${c}`);
|
|
66
|
+
paramProcessed = true;
|
|
67
|
+
} else if (c && !isValidDomain(c)) {
|
|
68
|
+
spwarn(`Invalid domain: ${c}. Only .sp domains are allowed.`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
p.delete(P);
|
|
72
|
+
const s = p.toString();
|
|
73
|
+
window.history.replaceState({}, "", window.location.pathname + (s ? `?${s}` : "") + window.location.hash);
|
|
74
|
+
}
|
|
75
|
+
let sv = localStorage.getItem(K);
|
|
76
|
+
if (sv && !isValidDomain(sv)) {
|
|
77
|
+
localStorage.removeItem(K);
|
|
78
|
+
sv = null;
|
|
79
|
+
}
|
|
80
|
+
if (sv && !paramProcessed && sv !== "debug") {
|
|
81
|
+
splog(`\u{1F50C} Stagepass connected to: ${sv}`);
|
|
82
|
+
}
|
|
83
|
+
const dev = !!sv;
|
|
84
|
+
const swap = !!sv && sv !== "debug";
|
|
85
|
+
const dom = swap ? clean(sv) : null;
|
|
86
|
+
function swapEl(sel, pathAttr, srcAttr, type, skipAttrs) {
|
|
87
|
+
const els = document.querySelectorAll(`${sel}:not([data-stagepass-processed])`);
|
|
88
|
+
els.forEach((el) => {
|
|
89
|
+
el.setAttribute("data-stagepass-processed", "true");
|
|
90
|
+
let lp = el.getAttribute(pathAttr);
|
|
91
|
+
const ps = el.getAttribute(srcAttr);
|
|
92
|
+
if (!ps) return;
|
|
93
|
+
if (!lp && ps && swap && dom) {
|
|
94
|
+
try {
|
|
95
|
+
const urlObj = new URL(ps);
|
|
96
|
+
const pathParts = urlObj.pathname.split("/");
|
|
97
|
+
lp = pathParts[pathParts.length - 1] || pathParts[pathParts.length - 2];
|
|
98
|
+
} catch (e) {
|
|
99
|
+
const match = ps.match(/\/([^\/\?#]+)(?:\?|#|$)/);
|
|
100
|
+
if (match) lp = match[1];
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
let url = "";
|
|
104
|
+
if (swap && lp && dom) {
|
|
105
|
+
const cp = lp.startsWith("/") ? lp.substring(1) : lp;
|
|
106
|
+
url = `https://${dom}/${cp}`;
|
|
107
|
+
} else if (ps) {
|
|
108
|
+
url = ps;
|
|
109
|
+
}
|
|
110
|
+
if (url) {
|
|
111
|
+
const n = document.createElement(type);
|
|
112
|
+
if (type === "script") {
|
|
113
|
+
n.src = url;
|
|
114
|
+
Array.from(el.attributes).forEach((a) => {
|
|
115
|
+
const attr = a;
|
|
116
|
+
if (!skipAttrs.includes(attr.name)) n.setAttribute(attr.name, attr.value);
|
|
117
|
+
});
|
|
118
|
+
if (!el.hasAttribute("async") && !el.hasAttribute("defer")) {
|
|
119
|
+
n.setAttribute("defer", "");
|
|
120
|
+
}
|
|
121
|
+
} else {
|
|
122
|
+
n.rel = "stylesheet";
|
|
123
|
+
n.href = url;
|
|
124
|
+
Array.from(el.attributes).forEach((a) => {
|
|
125
|
+
const attr = a;
|
|
126
|
+
if (!skipAttrs.includes(attr.name)) n.setAttribute(attr.name, attr.value);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
if (el.parentNode) {
|
|
130
|
+
el.parentNode.insertBefore(n, el);
|
|
131
|
+
el.remove();
|
|
132
|
+
} else {
|
|
133
|
+
sperror(`${type} injection failed: No parent node`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
function process() {
|
|
139
|
+
swapEl("script[data-stagepass]", "data-stagepass-path", "data-src", "script", ["data-src", "data-stagepass", "data-stagepass-path"]);
|
|
140
|
+
swapEl('link[rel="stylesheet"][data-stagepass]', "data-stagepass-path", "data-href", "link", ["href", "data-href", "data-stagepass", "data-stagepass-path"]);
|
|
141
|
+
}
|
|
142
|
+
process();
|
|
143
|
+
if (document.readyState === "loading") {
|
|
144
|
+
document.addEventListener("DOMContentLoaded", process);
|
|
145
|
+
}
|
|
146
|
+
function flag() {
|
|
147
|
+
if (!dev) return;
|
|
148
|
+
const f = document.createElement("div");
|
|
149
|
+
f.id = "stagepass-flag";
|
|
150
|
+
f.textContent = sv === "debug" ? "STAGEPASS DEBUG" : "STAGEPASS ACTIVE";
|
|
151
|
+
f.style.cssText = `font-size: 12px; line-height: 1; font-weight: bold; color: #fff; background-color: #D92662; padding: 4px 6px; z-index: 9999999999; position: fixed; top: 0; right: 0; cursor: pointer; border-radius: 0 0 0 4px; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; user-select: none;`;
|
|
152
|
+
f.addEventListener("click", () => {
|
|
153
|
+
localStorage.removeItem(K);
|
|
154
|
+
splog("\u{1F50C} Stagepass disconnected.");
|
|
155
|
+
window.location.reload();
|
|
156
|
+
});
|
|
157
|
+
if (document.body) {
|
|
158
|
+
document.body.appendChild(f);
|
|
159
|
+
} else {
|
|
160
|
+
document.addEventListener("DOMContentLoaded", () => {
|
|
161
|
+
if (document.body) document.body.appendChild(f);
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (document.readyState === "loading") {
|
|
166
|
+
document.addEventListener("DOMContentLoaded", flag);
|
|
167
|
+
} else {
|
|
168
|
+
flag();
|
|
169
|
+
}
|
|
170
|
+
})();
|
|
171
|
+
})();
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var Stagepass=(()=>{(function(){let d="stagepass_domain",h="stagepass",f=new URLSearchParams(window.location.search);if(!localStorage.getItem(d)&&!f.has(h)){let t=()=>{};console.log=t,console.warn=t,console.error=t,console.info=t,console.debug=t}function w(t,...l){var s;let e=localStorage.getItem(d);if(!e||e!=="debug"&&!e)return;let u=new Date().toTimeString().split(" ")[0],m="unknown";try{let a=(s=new Error().stack)==null?void 0:s.split(`
|
|
2
|
+
`);if(a){let i=a.find((r,n)=>n>2&&!r.includes("log"));if(i){let r=i.match(/(\/?([^\/\?:]+))(\?[^\s)]*)?:([0-9]+):[0-9]+/);r&&(m=`${r[2]}:${r[4]}`)}}}catch{}let b=`[\u{1F3AB} ${u} \u{1F4CD} ${m}]`;t==="error"?console.log(`%c${b}`,"color: red; font-weight: bold;",...l):console[t]?console[t](b,...l):console.log(b,t,...l)}let g=(...t)=>w("log",...t),A=(...t)=>w("warn",...t),v=(...t)=>w("error",...t);window.splog=g,window.spwarn=A,window.sperror=v;function $(t){return t?t.replace(/^https?:\/\//,"").replace(/\/$/,""):null}function y(t){return t==="debug"||t.endsWith(".sp")||t==="localhost"||t.startsWith("localhost:")}let L=!1;if(f.has(h)){let t=f.get(h);if(t==="off"||t==="0"||t==="false")localStorage.removeItem(d),g("\u{1F50C} Stagepass disconnected.");else if(t){let e=t==="debug"?"debug":$(t);e&&y(e)?(localStorage.setItem(d,e),g(e==="debug"?"\u{1F50D} Stagepass debug mode enabled.":`\u{1F50C} Stagepass connected to: ${e}`),L=!0):e&&!y(e)&&A(`Invalid domain: ${e}. Only .sp domains are allowed.`)}f.delete(h);let l=f.toString();window.history.replaceState({},"",window.location.pathname+(l?`?${l}`:"")+window.location.hash)}let o=localStorage.getItem(d);o&&!y(o)&&(localStorage.removeItem(d),o=null),o&&!L&&o!=="debug"&&g(`\u{1F50C} Stagepass connected to: ${o}`);let x=!!o,S=!!o&&o!=="debug",E=S?$(o):null;function k(t,l,e,u,m){document.querySelectorAll(`${t}:not([data-stagepass-processed])`).forEach(s=>{s.setAttribute("data-stagepass-processed","true");let a=s.getAttribute(l),i=s.getAttribute(e);if(!i)return;if(!a&&i&&S&&E)try{let c=new URL(i).pathname.split("/");a=c[c.length-1]||c[c.length-2]}catch{let c=i.match(/\/([^\/\?#]+)(?:\?|#|$)/);c&&(a=c[1])}let r="";if(S&&a&&E){let n=a.startsWith("/")?a.substring(1):a;r=`https://${E}/${n}`}else i&&(r=i);if(r){let n=document.createElement(u);u==="script"?(n.src=r,Array.from(s.attributes).forEach(c=>{let p=c;m.includes(p.name)||n.setAttribute(p.name,p.value)}),!s.hasAttribute("async")&&!s.hasAttribute("defer")&&n.setAttribute("defer","")):(n.rel="stylesheet",n.href=r,Array.from(s.attributes).forEach(c=>{let p=c;m.includes(p.name)||n.setAttribute(p.name,p.value)})),s.parentNode?(s.parentNode.insertBefore(n,s),s.remove()):v(`${u} injection failed: No parent node`)}})}function C(){k("script[data-stagepass]","data-stagepass-path","data-src","script",["data-src","data-stagepass","data-stagepass-path"]),k('link[rel="stylesheet"][data-stagepass]',"data-stagepass-path","data-href","link",["href","data-href","data-stagepass","data-stagepass-path"])}C(),document.readyState==="loading"&&document.addEventListener("DOMContentLoaded",C);function I(){if(!x)return;let t=document.createElement("div");t.id="stagepass-flag",t.textContent=o==="debug"?"STAGEPASS DEBUG":"STAGEPASS ACTIVE",t.style.cssText='font-size: 12px; line-height: 1; font-weight: bold; color: #fff; background-color: #D92662; padding: 4px 6px; z-index: 9999999999; position: fixed; top: 0; right: 0; cursor: pointer; border-radius: 0 0 0 4px; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; user-select: none;',t.addEventListener("click",()=>{localStorage.removeItem(d),g("\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",I):I()})();})();
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stagepass/loader",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Universal loader for Stagepass - injects local files into Webflow projects",
|
|
5
|
+
"main": "dist/loader.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsup",
|
|
12
|
+
"dev": "tsup --watch"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"webflow",
|
|
16
|
+
"loader",
|
|
17
|
+
"stagepass",
|
|
18
|
+
"development",
|
|
19
|
+
"injection"
|
|
20
|
+
],
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/arobertherz/stagepass.git",
|
|
24
|
+
"directory": "packages/loader"
|
|
25
|
+
},
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"tsup": "^8.0.1",
|
|
29
|
+
"typescript": "^5.3.3"
|
|
30
|
+
}
|
|
31
|
+
}
|