-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
202 lines (158 loc) · 7.38 KB
/
script.js
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
//-------------------------------------//
//-- Jquery to javascript converter --//
function convertJqueryToJS(jqueryCode) {
// Replace $ with document.querySelector
jqueryCode = jqueryCode.replace(/\$\(/g, 'document.querySelector(');
// Replace .text with .innerText
jqueryCode = jqueryCode.replace(/\.text\((.*?)\)/g, ".innerText = $1");
// Replace .html with .innerHTML
jqueryCode = jqueryCode.replace(/\.html\((.*?)\)/g, ".innerHTML = $1");
// Replace .val with .value
jqueryCode = jqueryCode.replace(/\.val\(/g, '.value =');
// Replace .click with .addEventListener("click", function()
jqueryCode = jqueryCode.replace(/\.click\(/g, '.addEventListener("click", function(');
// Replace .append with .insertAdjacentHTML("beforeend",
jqueryCode = jqueryCode.replace(/\.append\(/g, '.insertAdjacentHTML("beforeend",');
// Replace .prepend with .insertAdjacentHTML("afterbegin",
jqueryCode = jqueryCode.replace(/\.prepend\(/g, '.insertAdjacentHTML("afterbegin",');
// Replace .hide with .style.display = "none";
jqueryCode = jqueryCode.replace(/\.hide\(\)/g, '.style.display = "none";');
// Replace .show with .style.display = "block";
jqueryCode = jqueryCode.replace(/\.show\(\)/g, '.style.display = "block";');
// Replace .fadeIn with .style.display = "block"; and add transition effect
jqueryCode = jqueryCode.replace(/\.fadeIn\(/g, '.style.display = "block";\n' +
' setTimeout(function () {\n' +
' this.style.transition = "opacity 0.5s ease-in-out";\n' +
' this.style.opacity = 1;\n' +
' }.bind(this), 20);');
// Replace .fadeOut with .style.display = "none"; and add transition effect
jqueryCode = jqueryCode.replace(/\.fadeOut\(/g, '.style.opacity = 0;\n' +
' setTimeout(function () {\n' +
' this.style.display = "none";\n' +
' }.bind(this), 500);');
// Replace .addClass with .classList.add
jqueryCode = jqueryCode.replace(/\.addClass\(/g, '.classList.add(');
// Replace .removeClass with .classList.remove
jqueryCode = jqueryCode.replace(/\.removeClass\(/g, '.classList.remove(');
// Replace .toggleClass with .classList.toggle
jqueryCode = jqueryCode.replace(/\.toggleClass\(/g, '.classList.toggle(');
// Replace .attr with .getAttribute
jqueryCode = jqueryCode.replace(/\.attr\(/g, '.getAttribute(');
// Replace .attr with .setAttribute
jqueryCode = jqueryCode.replace(/\.attr\(\s*['"](\w+)['"],\s*['"](.*)['"]\s*\)/g,
(match, p1, p2) => {
return '.setAttribute("' + p1 + '", "' + p2 + '");';
});
// Replace .css with .style
jqueryCode = jqueryCode.replace(/\.css\(/g, '.style.');
// Replace .each with forEach
jqueryCode = jqueryCode.replace(/\.each\(/g, '.forEach(');
// Replace .on with addEventListener
jqueryCode = jqueryCode.replace(/\.on\(/g, '.addEventListener(');
// Replace .ready with window.addEventListener("load",
jqueryCode = jqueryCode.replace(/\$\(document\)\.ready\(/g, 'window.addEventListener("load",');
// Replace .append with .innerHTML +=
jqueryCode = jqueryCode.replace(/\.append\(/g, '.innerHTML += ');
// Replace .addClass with classList.add
jqueryCode = jqueryCode.replace(/\.addClass\(/g, '.classList.add(');
// Replace .removeClass with classList.remove
jqueryCode = jqueryCode.replace(/\.removeClass\(/g, '.classList.remove(');
// Replace .toggleClass with classList.toggle
jqueryCode = jqueryCode.replace(/\.toggleClass\(/g, '.classList.toggle(');
// Replace .fadeIn with classList.add and opacity transition
jqueryCode = jqueryCode.replace(/\.fadeIn\(/g, (match) => {
return '.style.transition = "opacity 0.5s", .style.opacity = "1", .style.display = "", ';
});
// Replace .fadeOut with classList.add and opacity transition
jqueryCode = jqueryCode.replace(/\.fadeOut\(/g, (match) => {
return '.style.transition = "opacity 0.5s", .style.opacity = "0", setTimeout(() => {.style.display = "none";}, 500), ';
});
// Replace .hide with style.display = "none"
jqueryCode = jqueryCode.replace(/\.hide\(/g, '.style.display = "none",');
// Replace .show with style.display = ""
jqueryCode = jqueryCode.replace(/\.show\(/g, '.style.display = "",');
// Replace .slideDown with height transition
jqueryCode = jqueryCode.replace(/\.slideDown\(/g, (match) => {
return '.style.display = "", .style.height = "0", .style.transition = "height 0.5s", setTimeout(() => {.style.height = "";}, 0), ';
});
// Replace .slideUp with height transition
jqueryCode = jqueryCode.replace(/\.slideUp\(/g, (match) => {
return '.style.height = "", .style.transition = "height 0.5s", setTimeout(() => {.style.display = "none", .style.height = "0";}, 500), ';
});
// Replace .slideToggle with height transition
jqueryCode = jqueryCode.replace(/\.slideToggle\(/g, (match) => {
return '.style.height === "0" || .style.display === "none" ? .style.display = "", .style.height = "0", .style.transition = "height 0.5s", setTimeout(() => {.style.height = "";}, 0) : .style.height = "", .style.transition = "height 0.5s", setTimeout(() => {.style.display = "none", .style.height = "0";}, 500), ';
});
// Replace .delay with setTimeout
jqueryCode = jqueryCode.replace(/\.delay\(([^\)]+)\)/g, (match, p1) => {
return 'setTimeout(() => {}, ' + p1 + '),';
});
return jqueryCode;
}
//-------------------------------------//
// ** Format Javascript code ** //
const beautify = (code) => {
let beautifiedCode = '';
let indentationLevel = 0;
let inString = false;
let currentChar;
let previousChar;
for (let i = 0; i < code.length; i++) {
currentChar = code[i];
previousChar = code[i - 1];
if (currentChar === '{' && !inString) {
beautifiedCode += currentChar + '\n';
indentationLevel++;
beautifiedCode += ' '.repeat(indentationLevel * 2);
} else if (currentChar === '}' && !inString) {
beautifiedCode = beautifiedCode.trimRight();
beautifiedCode += '\n';
indentationLevel--;
beautifiedCode += ' '.repeat(indentationLevel * 2);
beautifiedCode += currentChar + '\n';
beautifiedCode += ' '.repeat(indentationLevel * 2);
} else if (currentChar === '"' && previousChar !== '\\') {
inString = !inString;
beautifiedCode += currentChar;
} else {
beautifiedCode += currentChar;
}
}
return beautifiedCode.trim();
};
// Target all elements
var jquerybox = document.querySelector('#jquery-code');
var javascript = document.querySelector('#js-code');
// Convert on input in jquery textarea
jquerybox.addEventListener("input", function() {
var jqueryCode = jquerybox.value;
var jsCode = convertJqueryToJS(jqueryCode);
const beautifiedCode = beautify(jsCode);
javascript.innerText = beautifiedCode;
javascript.style.color = "#1E2235";
});
// clear all Jquery and javascript code
function cleartext() {
jquerybox.value = '';
javascript.innerText = 'Get Javascript Code Here';
javascript.style.color = "#757575";
}
// Copy to clipboard
function copyJS() {
// Select the text field
var javascript = document.querySelector("pre");
var copy = document.querySelector("#copy-btn");
// Select the text
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents(javascript);
selection.removeAllRanges();
selection.addRange(range);
// Copy the text to the clipboard
document.execCommand('copy');
// Replace inner text of copy button
copy.innerText = "Copied";
setTimeout(() => {
copy.innerText = "Copy";
}, 1000);
}