-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
131 lines (123 loc) · 3.88 KB
/
script.js
File metadata and controls
131 lines (123 loc) · 3.88 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
let editor;
let languages = document.querySelector("#languages")
let fntSize = document.querySelector("#fnt-size")
let runBtn = document.querySelector(".btn")
let output = document.querySelector(".output")
const PYTHON = "0"
const JS = "4"
const C = "7"
const CPP = "77"
const JAVA = "8"
let langID;
let codeID;
let interval;
fntSize.addEventListener('change',changeFont)
languages.addEventListener('change',changeLanguage)
runBtn.addEventListener('click',executeCode)
const boilerPlate = {
c : "#include<stdio.h>\nint main(){\n\t//Your code here...\n\n\treturn 0;\n}",
cpp : "#include<bits/stdc++.h>\nusing namespace std;\nint main(){\n\t//Your code here..\n\n\treturn 0;\n}",
java : "import java.util.*;\n//Other imports go here..\n//Do not change the class name\nclass Main{\n\tpublic static void main(String args[]){\n\t\t//Your code here...\n\t\t\n\t}\n}",
python :"#Your code here...\n",
js :"//Your code here...\n"
}
window.onload = function ()
{
editor=ace.edit("editor")
editor.session.setMode("ace/mode/java")
langID=JAVA;
editor.session.setUseWrapMode(true);
editor.setValue(boilerPlate['java'],1)
}
function changeLanguage()
{
let lang = languages.value
if(lang == 'c'){
editor.session.setMode('ace/mode/c_cpp')
editor.setValue(boilerPlate['c'],1)
langID=C
}
else if (lang =='cpp'){
editor.session.setMode('ace/mode/c_cpp')
editor.setValue(boilerPlate['cpp'],1)
langID=CPP
}
else if(lang=='java'){
editor.session.setMode('ace/mode/java')
editor.setValue(boilerPlate['java'],1)
langID=JAVA
}
else if(lang=='python'){
editor.session.setMode('ace/mode/python')
editor.setValue(boilerPlate['python'],1)
langID=PYTHON
}
else{
editor.session.setMode('ace/mode/javascript')
editor.setValue(boilerPlate['js'],1)
langID=JS
}
//console.log(langID)
}
function changeFont()
{
let val = fntSize.value;
document.getElementById('editor').style.fontSize = val;
}
function executeCode()
{
let code = editor.getSession().getValue();
output.innerHTML=""
let lang = languages.value;
let xhr = new XMLHttpRequest()
xhr.open('POST','https://codequotient.com/api/executeCode',true)
let obj= new Object()
obj.code = code
obj.langId=langID
//console.log(obj);
xhr.setRequestHeader('content-type','application/json')
xhr.onprogress = function(){
console.log("Sending...")
output.innerHTML="Compiling...."
}
let response;
xhr.onload = function(){
if(this.status === 200)
{
//console.log("Response Text ",this.responseText)
response=JSON.parse(this.responseText)
//console.log(response)
if(response.hasOwnProperty('error'))
{
output.innerHTML = response.error
}
else{
codeID=response.codeId
console.log(codeID)
interval = setTimeout(fetchOutput.bind(this),8000)
}
}
else
console.log("Some error occurred")
}
params = JSON.stringify(obj)
console.log(params)
xhr.send(params)
}
function fetchOutput()
{
const xhr = new XMLHttpRequest()
xhr.open('GET',`https://codequotient.com/api/codeResult/${codeID}`,true)
xhr.onprogress = function(){
console.log("Fetching...")
}
xhr.send()
xhr.onload = function(){
//output.innerHTML = this.responseText;
let response = JSON.parse(this.responseText)
response.data=JSON.parse(response.data)
console.log(response)
console.log(response.data.output)
output.innerText = response.data.errors ? response.data.errors : response.data.output
}
}