-
Notifications
You must be signed in to change notification settings - Fork 0
/
contentScript.js
369 lines (313 loc) · 9.91 KB
/
contentScript.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
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
function getCodeSlug(data) {
const start = "'value': 'cpp'";
const end = "'defaultCode': '";
const cppIndex = data.indexOf(start);
const defaultCodeIndex = data.indexOf(end, cppIndex);
const startIdx = defaultCodeIndex + end.length;
const endIdx = data.indexOf("'", startIdx);
if (endIdx === -1) return null;
let cpp = data.slice(startIdx, endIdx);
// Decode Unicode escape sequences
cpp = cpp.replace(/\\u[\dA-F]{4}/gi, (match) =>
String.fromCharCode(parseInt(match.replace(/\\u/g, ""), 16))
);
return cpp;
}
function getFuncName(data) {
const metadataMarker = "metaData: JSON.parse('";
const startIndex = data.indexOf(metadataMarker);
if (startIndex === -1) return null;
const jsonStartIndex = startIndex + metadataMarker.length;
const jsonEndIndex = data.indexOf("'", jsonStartIndex);
if (jsonEndIndex === -1) return null;
let jsonString = data.slice(jsonStartIndex, jsonEndIndex);
jsonString = jsonString.replace(/\\u[\dA-F]{4}/gi, (match) =>
String.fromCharCode(parseInt(match.replace(/\\u/g, ""), 16))
);
try {
const jsonData = JSON.parse(jsonString);
return jsonData.name || null;
} catch (error) {
return null;
}
}
function getSamples(data) {
var doc = new DOMParser().parseFromString(data, "text/html");
var exampleBlocks = doc.querySelectorAll("div.example-block");
var samples = [];
exampleBlocks.forEach((block) => {
let inputText = getText(block, "Input:");
let outputText = getText(block, "Output:");
if (inputText && outputText) {
let input = parse_input(inputText);
samples.push({ input, output: outputText });
}
});
// For debugging
console.log("Samples:", samples);
return samples;
}
function getText(block, label) {
let regex = new RegExp(label, "i");
let pElements = block.getElementsByTagName("p");
for (let p of pElements) {
if (regex.test(p.innerHTML)) {
let text = [];
let sibling = p.firstChild;
let capture = false;
while (sibling) {
if (capture) {
if (sibling.nodeType === Node.TEXT_NODE) {
text.push(sibling.textContent.trim());
} else if (sibling.nodeType === Node.ELEMENT_NODE) {
text.push(sibling.textContent.trim());
}
}
if (
sibling.nodeType === Node.TEXT_NODE &&
regex.test(sibling.textContent)
) {
capture = true;
} else if (
sibling.nodeType === Node.ELEMENT_NODE &&
regex.test(sibling.innerHTML)
) {
capture = true;
}
sibling = sibling.nextSibling;
}
return text.join(" ").trim();
}
}
return null;
}
function parse_input(input_string) {
input_string = input_string.split(", ");
var data = [];
for (let i = 0; i < input_string.length; i++) {
var varname = input_string[i].split("=")[0].trim();
var vardata = input_string[i].split("=")[1].trim();
data.push([varname, vardata]);
}
console.log("data : ", data);
return data;
}
function isInt(n) {
return Number(n) === n && n % 1 === 0;
}
function isFloat(n) {
return Number(n) === n && n % 1 !== 0;
}
function isBool(n) {
return typeof n == "boolean";
}
function isStr(n) {
return typeof n == "string";
}
function isList(a) {
return Array.isArray(a);
}
function getDatatype(data) {
if (isInt(data)) {
return "int";
} else if (isFloat(data)) {
return "float";
} else if (isBool(data)) {
return "bool";
} else if (isStr(data)) {
return "string";
} else if (isList(data)) {
var temp = getDatatype(data[0]);
temp = `vector<${temp}>`;
return temp;
} else {
console.log("Unknown Datatype : " + data);
return "None";
}
}
function jsontocpp(data, index) {
var res = "";
for (let i = 0; i < data.length; i++) {
var varname = data[i][0];
console.log("varname : ", varname);
//Console Inputs and Outputs
console.log("data : ", data[i][1]);
var datatype = getDatatype(JSON.parse(data[i][1]));
var vardata = data[i][1];
vardata = vardata.replaceAll("[", "{");
vardata = vardata.replaceAll("]", "}");
vardata = vardata.replaceAll("False", "false");
vardata = vardata.replaceAll("True", "true");
res += `${datatype} ${varname}${index} = ${vardata};`;
res += "\n\t";
}
return res;
}
function jsontocpp2(data, index) {
var res = "";
var varname = "output_";
var datatype = getDatatype(JSON.parse(data));
var vardata = data;
vardata = vardata.replaceAll("[", "{");
vardata = vardata.replaceAll("]", "}");
vardata = vardata.replaceAll("False", "false");
vardata = vardata.replaceAll("True", "true");
res += `${datatype} ${varname}${index} = ${vardata};`;
res += "\n\t";
return res;
}
function getSampleVariables(data, index) {
var res = [];
for (let i = 0; i < data.length; i++) {
var varname = data[i][0];
res.push(`${varname}${index}`);
}
return res.join(",");
}
function generateChecker(samples, funcname) {
var inputs = [];
var outputs = [];
for (let i = 0; i < samples.length; i++) {
inputs.push(samples[i].input);
outputs.push(samples[i].output);
}
//Debugging
console.log("inputs : ", inputs);
console.log("outputs : ", outputs);
var res = "";
for (let i = 0; i < inputs.length; i++) {
var x = inputs[i];
var y = outputs[i];
res += jsontocpp(x, i + 1);
res += jsontocpp2(y, i + 1);
res += `if(leetSouls.${funcname}(${getSampleVariables(x, i + 1)})==output_${
i + 1
}){\n\t\tcout << "Sample #${
i + 1
} : Accepted" << endl;\n\t}else{\n\t\tcout << "Sample #${
i + 1
} : Wrong Answer" << endl;\n\t}`;
res += "\n";
res += "\n\t";
}
return res;
}
function getTemplate() {
temp = `
// Generated By LeetSouls
#include <bits/stdc++.h>
using namespace std;
$function
int main(){
$samples
return 0;
}`;
return temp;
}
function generateCode(codeSlug, samples, funcname) {
var TEMPLATE = getTemplate();
TEMPLATE = TEMPLATE.replace("$function", codeSlug);
TEMPLATE = TEMPLATE.replace(
"$samples",
"\tSolution leetSouls;\n\t" + generateChecker(samples, funcname)
);
return TEMPLATE;
}
function copyToClipboard(text) {
if (navigator.clipboard && window.isSecureContext) {
// Navigator Clipboard API method
return navigator.clipboard
.writeText(text)
.then(() => {
showNotification("✅ " + getQuirkyMessage());
})
.catch((err) => {
console.error("Failed to copy text: ", err);
showNotification("Failed to copy", "See console for details");
});
} else {
// Fallback method
const textArea = document.createElement("textarea");
textArea.value = text;
textArea.style.position = "fixed";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
const success = document.execCommand("copy");
if (success) {
showNotification("✅ " + getQuirkyMessage());
} else {
showNotification("Failed to copy", "See console for details");
}
} catch (err) {
console.error("Fallback: Oops, unable to copy", err);
showNotification("Failed to copy", "See console for details");
}
document.body.removeChild(textArea);
}
}
function getQuirkyMessage() {
const messages = [
"LeetCode challenge extracted, Tarnished. Rise and code! 💻🌟",
"Problem copied. Prepare to compile... repeatedly. ☠️🔄",
"Challenge ahead, therefore time for coding! 🗡️⌨️",
"Hesitation is defeat. Paste the code and face your runtime fears! ⏱️😱",
"Amazing code ahead, and then praise the algorithm! 🙌🧠",
"Don't give up, skeleton! Your IDE awaits! 💀💻",
"Problem transported to clipboard. The night of the code hunt begins... 🌙🐺",
"Challenge copied. May the good code guide your way! 🕯️🛤️",
"Hidden code ahead. Time to git gud! 🕵️♂️🎮",
"Behold, LeetCode! Time for strategic coding and then victory! 🏆🔍",
"Visions of efficient algorithm... Now the real fight begins! 👁️⚔️",
"Problem acquired, Ashen One. Link the code to appease the LeetCode Lords! 🔥👑",
"Challenge beckons, Good Hunter. A programmer must code! 🩸🖋️",
];
return messages[Math.floor(Math.random() * messages.length)];
}
function showNotification(message) {
const notification = document.createElement("div");
notification.style.position = "fixed";
notification.style.bottom = "0px";
notification.style.left = "50%";
notification.style.transform = "translateX(-50%) translateY(100%)";
notification.style.backgroundColor = "#1A1A1A";
notification.style.color = "#ffffff";
notification.style.padding = "8px 16px";
notification.style.borderRadius = "20px";
notification.style.zIndex = "9999";
notification.style.fontFamily = "Arial, sans-serif";
notification.style.fontSize = "14px";
notification.style.boxShadow = "0 4px 12px rgba(0, 0, 0, 0.5)";
notification.style.display = "flex";
notification.style.alignItems = "center";
notification.style.opacity = "0";
notification.style.transition = "opacity 0.5s, transform 0.5s";
notification.innerHTML = `${message}`;
document.body.appendChild(notification);
// Fade in
setTimeout(() => {
notification.style.opacity = "1";
notification.style.transform = "translateX(-50%) translateY(0)";
notification.style.bottom = "50px";
}, 50);
// Fade out
setTimeout(() => {
notification.style.opacity = "0";
notification.style.transform = "translateX(-50%) translateY(100%)";
setTimeout(() => {
document.body.removeChild(notification);
}, 500);
}, 2500);
}
function run() {
let data = "";
data += document.documentElement.outerHTML + "\n\n";
var codeSlug = getCodeSlug(data);
var funcname = getFuncName(data);
var samples = getSamples(data);
let tmp = generateCode(codeSlug, samples, funcname);
copyToClipboard(tmp);
}
document.addEventListener("generateLocalTester", run);
console.log("LeetSouls content script loaded");