-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
82 lines (64 loc) · 2.6 KB
/
Copy pathscript.js
File metadata and controls
82 lines (64 loc) · 2.6 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
79
80
81
82
let htmlValue = document.getElementById('htmlValue');
let cssValue = document.getElementById('cssValue');
let jsValue = document.getElementById('jsValue');
let display = document.getElementById('output');
let textareas = document.querySelectorAll('textarea');
const array=[textareas[0],textareas[1]];
let button = document.getElementById('saveJs');
let errorDiv=document.getElementById('errorDiv');
let errorMessage=document.querySelector('#errorMessage');
let closeBtn=document.querySelector('#closeBtn');
let errorOccured=false;
window.addEventListener('load',()=>{
if(localStorage.getItem('showAlert')===false || localStorage.getItem('showAlert')===null || localStorage.getItem('showAlert')===undefined ){
localStorage.setItem('showAlert',true);
alert('In order to save the file click ctrl + s');
alert("This website uses browser's localStorage in order to save the written code and execute it after reopening or refreshing the site");
console.log('hit');
}
let htmlBefore=localStorage.getItem('Html');
let cssBefore=localStorage.getItem('Css');
let jsBefore=localStorage.getItem('Javascript');
if(htmlBefore) htmlValue.value=htmlBefore;
if(cssBefore) cssValue.value=cssBefore;
if(jsBefore) jsValue.value=jsBefore;
compileCode();
display.contentDocument.body.style.wordBreak='break-all';
})
closeBtn.addEventListener('click',()=>{
errorOccured=false;
errorDiv.style.display='none';
errorMessage.innerText='';
})
document.addEventListener('keydown', function(event) {
if ((event.ctrlKey || event.metaKey) && event.key === 's') {
event.preventDefault();
compileCode();
saveCode();
}
});
// Function to handle errors
window.onerror = function(message, source, lineno, colno, error) {
// Log the error details (for demonstration purposes)
console.error(`Error occurred: ${message} at ${source}:${lineno}:${colno}`);
onError(message,source,lineno,colno);
errorOccured=true;
};
function compileCode() {
display.contentDocument.body.innerHTML=htmlValue.value + '<style>' + cssValue.value + '</style>';
display.contentWindow.eval(jsValue.value);
if(errorOccured===true){
errorOccured=false;
errorDiv.style.display='none';
errorMessage.innerText='';
}
}
function onError(message,source,lineno,colno){
errorDiv.style.display="flex";
errorMessage.innerText=`Error occurred: ${message} at ${source}:${lineno}:${colno}`
}
function saveCode(){
localStorage.setItem('Html',htmlValue.value);
localStorage.setItem('Css',cssValue.value);
localStorage.setItem('Javascript',jsValue.value);
}