-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnexus8.js
More file actions
78 lines (68 loc) · 1.93 KB
/
nexus8.js
File metadata and controls
78 lines (68 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
(function() {
'use strict';
function toKebabCase(str) {
return str.replace(/[A-Z]/g, m => '-' + m.toLowerCase());
}
function varNameToSelector(name) {
if (name.startsWith('$')) return '#' + name.slice(1);
if (name.startsWith('_')) return '.' + name.slice(1);
return name;
}
function objectToCss(selector, obj) {
let css = `${selector} {\n`;
for (const key in obj) {
css += ` ${toKebabCase(key)}: ${obj[key]};\n`;
}
css += `}\n`;
return css;
}
function processJStyle(code) {
const scope = new Proxy({}, {
has(target, key) {
return true;
},
get(target, key) {
if (!(key in target)) {
target[key] = {};
}
return target[key];
},
set(target, key, value) {
target[key] = value;
return true;
}
});
try {
new Function("with(this) { " + code + " }").call(scope);
} catch (err) {
console.error("[JStyle] Error evaluating code:", err);
return "";
}
let finalCss = "";
for (const key in scope) {
if (typeof scope[key] === "object") {
finalCss += objectToCss(varNameToSelector(key), scope[key]);
}
}
return finalCss;
}
async function loadJStyle() {
const inlineTags = document.querySelectorAll('jstyle');
let cssOutput = "";
inlineTags.forEach(tag => {
cssOutput += processJStyle(tag.textContent);
tag.remove();
});
if (cssOutput.trim()) {
const styleEl = document.createElement("style");
styleEl.textContent = cssOutput;
document.head.appendChild(styleEl);
}
}
if (document.readyState !== "loading") {
loadJStyle();
console.log("Thank you for using Nexus8")
} else {
document.addEventListener("DOMContentLoaded", loadJStyle);
}
})();