-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
38 lines (37 loc) · 1.68 KB
/
Copy pathjavascript.js
File metadata and controls
38 lines (37 loc) · 1.68 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
document.addEventListener('readystatechange', function() {
if (document.readyState === 'loading') {
document.body.style.display = 'none';
}
});
document.addEventListener('DOMContentLoaded', () => {
async function sendStylesToServer() {
if (!navigator.onLine) {
console.log('No internet connection.');
return;
}
const scriptTags = document.querySelectorAll('script[src*="cssplus.onrender.com"]');
const targetURL = scriptTags.length > 0 ? 'https://cssplus.onrender.com/compile/' : 'http://127.0.0.1:8000/compile/';
const styleElements = document.querySelectorAll('style');
for (const style of styleElements) {
const cssContent = style.textContent;
const encodedCSSContent = encodeURIComponent(cssContent);
try {
const response = await fetch(`${targetURL}?var=${encodedCSSContent}`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
let compiledCSS = await response.text();
/* console.log('Compiled CSS received:');
console.log(compiledCSS); */
compiledCSS = compiledCSS.replace(/^"|"$/g, '').replace(/{{}/g, '');
const styleElement = document.createElement('style');
styleElement.textContent = compiledCSS;
document.head.appendChild(styleElement);
} catch (error) {
console.error('Error sending style content:', error);
}
}
}
sendStylesToServer();
document.body.style.display = 'block';
});