-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
423 lines (338 loc) · 13.5 KB
/
Copy pathscript.js
File metadata and controls
423 lines (338 loc) · 13.5 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
const runButton = document.getElementById('run-button');
const outputConsole = document.getElementById('output');
const runConsole = document.getElementById('run-console');
const customCons = document.getElementById('customConsole');
const clearAllButton = document.getElementById('clearAll');
//Editor Control Code
var editor = CodeMirror.fromTextArea(document.getElementById("my-textarea"), {
mode: "javascript",
theme: "dracula",
lineNumbers: true,
autoCloseBrackets: true,
matchBrackets: true,
extraKeys: {
"Ctrl-Space": "autocomplete"
},
indentWithTabs: true, // Use tabs for indentation
indentUnit: 4, // Set the number of spaces per tab
});
///////////////////////Resizing Editor
const resizingButton = document.getElementById("panelResize");
const body = document.getElementsByTagName("body");
const toolbar = document.querySelector(".toolbar");
const navbar = document.querySelector(".navbar");
const motherContainer = document.querySelector(".mainCointainer");
const icon = document.querySelector("#editorResizeing i");
const fullpanel = document.getElementById("editorPanel");
const subPannel = document.getElementById("subPanel");
const outputContainer = document.querySelector(".output-container");
const minimizeBtn = document.getElementById("miniDiv");
let isMaximized = false;
resizingButton.addEventListener("click", function() {
// Toggle between maximizing and minimizing the container
if (!isMaximized) {
// Maximize the container
motherContainer.style.width = "100vw";
motherContainer.style.height = "100%";
motherContainer.style.overflow = "hidden";
minimizeBtn.style.display = "block";
// body.style.margin = "0";
outputContainer.style.height = "95vh";
subPannel.style.height = "100%";
fullpanel.style.height = "95vh";
navbar.style.display = "none";
toolbar.style.display = "none";
resizingButton.textContent = "Min";
} else {
// Minimize the container to its original size
motherContainer.style.width = "95%";
motherContainer.style.height = "100%";
minimizeBtn.style.display = "none";
}
isMaximized = !isMaximized;
});
//Minimize Button Script
let isMinimized = false;
minimizeBtn.addEventListener("click", function() {
// Toggle between maximizing and minimizing the container
if (!isMinimized) {
// Minimize the container to its original size
motherContainer.style.width = "100%";
motherContainer.style.height = "100%";
motherContainer.style.overflow = "visible";
minimizeBtn.style.display = "none";
fullpanel.style.height = "80vh";
subPannel.style.height = "80vh";
outputContainer.style.height = "80vh";
navbar.style.display = "block";
toolbar.style.display = "block";
resizingButton.textContent = "Max";
} else {
minimizeBtn.style.display = "block";
}
isMinimized = !isMinimized;
});
//Reload buton script
document.querySelector("#loadButton").addEventListener("click", () => {
location.reload();
});
//Wrap Unwarp Script
document.getElementById("wrapUnwrap").addEventListener("click", function() {
});
// Tool Bar All Script Start
// Upload JS File Script
const uploadJS = document.querySelector("#uploadFile");
uploadJS.addEventListener("change", function() {
const file = this.files[0];
if (file) {
// Read the contents of the uploaded file
const reader = new FileReader();
reader.onload = function(e) {
// Set the contents of the CodeMirror editor to the file's content
editor.setValue(e.target.result);
};
reader.readAsText(file);
}
});
//Wrap Editor
const wrapUnwrapButton = document.getElementById("wrapUnwrap");
let isWrapped = false;
wrapUnwrapButton.addEventListener("click", function() {
isWrapped = !isWrapped; // Toggle the state
editor.setOption("lineWrapping", isWrapped);
wrapUnwrapButton.textContent = isWrapped ? "Unwrap" : "Wrap";
});
//Download JS File Script
document.getElementById('downloadCode').addEventListener('click', function() {
// Get the content of the CodeMirror editor
var codeContent = editor.getValue();
if (codeContent.trim() === '') {
alert('Textarea is empty. Please enter some code.');
return;
}
// Create a Blob (Binary Large Object) from the code content
var blob = new Blob([codeContent], { type: 'application/javascript' });
// Create a download link
var downloadLink = document.createElement('a');
downloadLink.href = window.URL.createObjectURL(blob);
// Prompt the user for the file name
var fileName = prompt('Enter file name:', 'script.js');
if (fileName) {
// Set the download attribute and file name
downloadLink.download = fileName;
// Simulate a click on the download link to trigger the download
downloadLink.click();
}
});
// Run Code Script
// document.addEventListener("DOMContentLoaded", function() {
runButton.addEventListener('click', function() {
var codeContent = editor.getValue();
var outputIframe = document.getElementById('output');
var iframeDoc = outputIframe.contentDocument || outputIframe.contentWindow.document;
// Clear the iframe's document content
iframeDoc.open();
iframeDoc.write('');
iframeDoc.close();
try {
iframeDoc.open();
iframeDoc.write(`
<html>
<head>
<style>
body {
color: #ffffff;
}
</style>
</head>
<body>
<script>${codeContent}</script>
</body>
</html>
`);
iframeDoc.close();
console.log('Code executed successfully.');
} catch (error) {
alert('Error: ' + error.message);
}
});
const reloadIframe = function() {
var codeContent = editor.getValue();
var outputIframe = document.getElementById('output');
// Generate a random query parameter to force iframe reload
var timestamp = Date.now();
// Set the iframe src with the timestamp as a query parameter
outputIframe.src = 'about:blank?' + timestamp;
outputIframe.onload = function() {
// Execute the code within the iframe's JavaScript context
var iframeWindow = outputIframe.contentWindow;
iframeWindow.eval(codeContent);
};
}
runButton.addEventListener('click', reloadIframe);
// });
// document.addEventListener("DOMContentLoaded", function() {
// Your code here
//Run console Script
runConsole.addEventListener('click', function() {
// Clear the custom console before running code
customCons.innerHTML = ' ';
// Store the original console functions for later use
const originalConsoleLog = console.log;
const originalConsoleError = console.error;
const originalConsoleWarn = console.warn;
const originalDocumentWrite = document.write; // Store the original document.write
// Override console functions to redirect output
console.log = function() {
const args = Array.from(arguments).join(' ');
customCons.innerHTML += `<p class="log">${args}</p>`;
originalConsoleLog.apply(console, arguments);
};
console.error = function() {
const args = Array.from(arguments).join(' ');
customCons.innerHTML += `<p class="error">${args}</p>`;
originalConsoleError.apply(console, arguments);
};
console.warn = function() {
const args = Array.from(arguments).join(' ');
customCons.innerHTML += `<p class="warn">${args}</p>`;
originalConsoleWarn.apply(console, arguments);
};
// Override document.write to prevent it from replacing the entire document
document.write = function() {
// Handle the write function as needed, or simply ignore it
console.warn('document.write is disabled within the code.');
};
// Get the code from the editor and execute it
const codeToExecute = editor.getValue();
try {
// Use eval or a safer alternative to execute the code here
// Example with eval (not recommended for untrusted code)
eval(codeToExecute);
} catch (error) {
console.error(`Error: ${error}`);
}
// Restore the original console functions and document.write
console.log = originalConsoleLog;
console.error = originalConsoleError;
console.warn = originalConsoleWarn;
document.write = originalDocumentWrite;
});
// });
//Copy Code Script
document.getElementById('copyit').addEventListener('click', function() {
// Get the content of the CodeMirror editor
var codeContent = editor.getValue();
if (codeContent.trim() === '') {
alert('Textarea is empty. Please enter some code.');
return;
}
// Copy Editor Code
var textarea = document.createElement('textarea');
textarea.value = codeContent;
document.body.appendChild(textarea);
// Select the text in the textarea
textarea.select();
// Copy the selected text to the clipboard
document.execCommand('copy');
// Remove the temporary textarea
document.body.removeChild(textarea);
// Provide feedback to the user
alert('Code copied to clipboard');
});
//Clear All Button Script
clearAllButton.addEventListener('click', function() {
editor.setValue('');
customCons.innerHTML = '';
// Clear the output iframe by reloading it with an empty source
setTimeout(reloadIframe, 500)
});
///////////Insart the Object Boiler plate
const objectBoilerButton = document.getElementById('objectBoiler');
objectBoilerButton.addEventListener('click', function() {
const codeToInsert = `
// Please input the object variable in ()
// This is not allowed to execute objects directly
// Thanks for compromising with us
let jsonString = JSON.stringify();
document.write(jsonString);
console.log(jsonString);`;
// Get the current cursor position
const cursor = editor.getCursor();
// Insert the code at the cursor position
editor.replaceRange(codeToInsert, cursor);
// Move the cursor to the end of the inserted code
cursor.ch += codeToInsert.length;
editor.setCursor(cursor);
});
///////Font size changenger
const fontSizeSelect = document.getElementById('fontSizeSelect');
fontSizeSelect.addEventListener('change', function() {
const selectedFontSize = fontSizeSelect.value;
editor.getWrapperElement().style.fontSize = selectedFontSize + 'px';
// Save the selected font size to localStorage or a cookie for persistence
localStorage.setItem('selectedFontSize', selectedFontSize);
});
// Load the selected font size from localStorage if available
const savedFontSize = localStorage.getItem('selectedFontSize');
if (savedFontSize) {
fontSizeSelect.value = savedFontSize;
// Trigger the change event to apply the saved font size
fontSizeSelect.dispatchEvent(new Event('change'));
}
///////////////Change Fonts
const fontFamilySelect = document.getElementById('fontFamilySelect');
// Assuming you already have an initialized CodeMirror editor
// The 'editor' variable should be defined and initialized before this code.
fontFamilySelect.addEventListener('change', function() {
const selectedFontFamily = fontFamilySelect.value;
editor.getWrapperElement().style.fontFamily = selectedFontFamily;
// Save the selected font family to localStorage or a cookie for persistence
localStorage.setItem('selectedFontFamily', selectedFontFamily);
});
// Load the selected font family from localStorage if available
const savedFontFamily = localStorage.getItem('selectedFontFamily');
if (savedFontFamily) {
fontFamilySelect.value = savedFontFamily;
// Trigger the change event to apply the saved font family
fontFamilySelect.dispatchEvent(new Event('change'));
}
////////////////////////////////////////////////////////////////////////////////////////////////
//Advance Option JS Script
const toggleButton = document.getElementById('radyCode');
const hiddenDiv = document.getElementById('hiddenDiv');
toggleButton.addEventListener('click', function() {
// Toggle the visibility of the hiddenDiv
if (hiddenDiv.style.display === 'none') {
hiddenDiv.style.display = 'block';
} else {
hiddenDiv.style.display = 'none';
}
});
// Select all <li> elements within elements with class 'codeTemplates'
const codeTemplates = document.querySelectorAll('.codeTemplates li');
codeTemplates.forEach(function(li) {
li.addEventListener('click', function(e) {
const selectedCode = e.target.getAttribute('data-code');
if (selectedCode) {
// Remove unnecessary whitespace from the selected code
const trimmedCode = selectedCode.trim();
// Get the current cursor position
const cursor = editor.getCursor();
// Insert the trimmed code at the cursor position
editor.replaceRange(trimmedCode, cursor);
// Hide the #hiddenDiv
hiddenDiv.style.display = 'none';
}
});
});
//Shortcut Script
const shortcutBrowser = document.getElementById("browserShortcut");
const shortcutWizard = document.getElementsByClassName("hiddenWizard")[0]; // Use [0] to get the first element with the class
const shortcutWizardClose = document.getElementById("closeShortcut");
shortcutBrowser.addEventListener("click", function() {
shortcutWizard.style.display = "block";
});
shortcutWizardClose.addEventListener("click", function() {
shortcutWizard.style.display = "none";
});