-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathleetcode-pdf.js
493 lines (448 loc) · 15.4 KB
/
leetcode-pdf.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
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
// Credits :
Inspired from - https://github.com/world177/Leetcode-Downloader-for-Submissions
User - https://github.com/world177
*/
LeetCodeSubmissionDownloader.LEETCODE_SUBMISSIONS_API =
"https://leetcode.com/api/submissions";
LeetCodeSubmissionDownloader.BASE_PROBLEM_ADDRESS =
"https://leetcode.com/problems/";
LeetCodeSubmissionDownloader.INCREASE_LAST_BY = 20;
LeetCodeSubmissionDownloader.WAIT_BETWEEN_REQUESTS = 2500; // milliseconds
LeetCodeSubmissionDownloader.INCREASE_WAIT_BY_ON_ERROR = 2;
LeetCodeSubmissionDownloader.questionMap = {};
LeetCodeSubmissionDownloader.last = 0;
LeetCodeSubmissionDownloader.processed = 0;
LeetCodeSubmissionDownloader.waitUsedBetweenRequests =
LeetCodeSubmissionDownloader.WAIT_BETWEEN_REQUESTS;
LeetCodeSubmissionDownloader.MAX_ATTEMPT = 5;
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
// Function to send a message to the popup script
function sendMessageToPopup(message) {
chrome.runtime.sendMessage({ type: "setUpdates", ...message });
}
function fetchUsername() {
return new Promise((resolve, reject) => {
const query = `
query globalData {
userStatus {
username
}
}
`;
const body = JSON.stringify({
query: query,
variables: {},
operationName: "globalData",
});
const xhr = new XMLHttpRequest();
xhr.open("POST", "https://leetcode.com/graphql/");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onload = function () {
if (xhr.status >= 200 && xhr.status < 300) {
const data = JSON.parse(xhr.responseText);
if (data && data.data && data.data.userStatus) {
const username = data.data.userStatus.username;
resolve(username);
} else {
console.error("Failed to fetch username");
reject("Failed to fetch username");
}
} else {
console.error("Failed to fetch username");
reject("Failed to fetch username");
}
};
xhr.onerror = function () {
console.error("Request failed");
reject("Request failed");
};
xhr.send(body);
});
}
function fetchQuestionContent(titleSlug) {
return new Promise((resolve, reject) => {
setTimeout(() => {
const query = `
query questionContent($titleSlug: String!) {
question(titleSlug: $titleSlug) {
content
}
}
`;
const variables = {
titleSlug: titleSlug,
};
const body = JSON.stringify({
query: query,
variables: variables,
operationName: "questionContent",
});
const xhr = new XMLHttpRequest();
xhr.open("POST", "https://leetcode.com/graphql/");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onload = function () {
if (xhr.status >= 200 && xhr.status < 300) {
const data = JSON.parse(xhr.responseText);
resolve(data.data.question.content);
} else {
console.error("Failed to fetch question content");
reject("Failed to fetch question content");
}
};
xhr.onerror = function () {
console.error("Request failed");
reject("Request failed");
};
xhr.send(body);
}, 50); // 50 milliseconds delay to avoid rate limitting
});
}
function highlightCode(language, code) {
let highlightedCode = "";
const lines = code.split("\n");
switch (language) {
case "python":
case "python3":
for (const line of lines) {
const isComment = line.trim().startsWith("#");
if (isComment) {
highlightedCode += `<span class='comment'>${line}</span>\n`;
} else {
let highlightedLine = line.replace(
/\b(if|else|elif|while|for|in|def|class|pass|return)\b/g,
"<span class='keyword'>$1</span>"
);
highlightedCode += `${highlightedLine}\n`;
}
}
break;
case "cpp":
for (const line of lines) {
// Define patterns to highlight C++ keywords and comments
const isComment =
line.trim().startsWith("//") ||
line.trim().startsWith("/*") ||
line.trim().endsWith("*/");
if (isComment) {
highlightedCode += `<span class='comment'>${line}</span>\n`;
} else {
let highlightedLine = line.replace(
/\b(if|else|while|for|return|break|continue|class|int|double|string|void|float|long|bool|vector|string|char|map|set)\b/g,
"<span class='keyword'>$1</span>"
);
highlightedCode += `${highlightedLine}\n`;
}
}
break;
default:
// If language is not recognized, simply return the original code
highlightedCode = code;
break;
}
return highlightedCode;
}
function generateHTMLContent(username = "") {
let htmlContent = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LeetCode Submissions</title>
<link rel="icon" type="image/x-icon" href="https://raw.githubusercontent.com/TheShubham99/leetcode-to-pdf/main/logo.png">
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
color: #333;
padding: 20px;
print-color-adjust: exact;
}
.header {
background-color: #f8f9fa;
padding: 20px;
text-align: center;
margin-bottom: 20px;
height:
}
.header h1 {
color: #007bff;
margin: 0;
}
.header-buttons {
margin-top: 10px;
}
.button {
background-color: #007bff;
color: #fff;
border: none;
padding: 10px 20px;
font-size: 16px;
border-radius: 4px;
cursor: pointer;
text-decoration: none;
display: inline-block;
margin-right: 10px;
transition: background-color 0.3s ease;
}
.button:hover {
background-color: #0056b3;
}
.question {
margin-bottom: 40px;
border: 1px solid #ccc;
padding: 20px;
background-color: #fff;
}
.title {
font-size: 24px;
font-weight: bold;
margin-bottom: 10px;
color: #333;
}
.language {
font-style: italic;
color: #666;
margin-bottom: 10px;
}
.description {
margin-bottom: 20px;
}
.solution {
margin-top: 20px;
}
.solution-status {
color: green;
font-weight: bold;
margin-bottom: 10px;
}
pre {
padding: 2rem;
border: 1px grey solid;
padding: 10px;
border-radius: 4px;
overflow-x: auto;
}
.keyword{
color: #0e77ae;
}
.comment{
color: green;
}
</style>
</head>
<body>
<div class="header">
<h1>My LeetCode Submissions ${
username ? `- @${username}` : ""
}</h1>
<div class="header-buttons">
<a class="button" onclick="print()" >Download PDF </a>
<a href="https://github.com/theshubham99" class="button" target="_blank">Follow @TheShubham99 on GitHub</a>
<a href="https://github.com/theshubham99/leetcode-submissions" class="button" target="_blank">Star on GitHub</a>
<a href="https://github.com/theshubham99/leetcode-to-pdf" class="button" target="_blank">View Source Code</a>
</div>
</div>
`;
// Iterate over each question in the questionMap and add it to the HTML content
for (const [
title,
{ heading, language, description, solution },
] of Object.entries(LeetCodeSubmissionDownloader.questionMap)) {
let highlightedSolution = highlightCode(
language,
solution
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'")
);
htmlContent += `
<div class="question">
<div class="title">
<a href="https://leetcode.com/problems/${title}/description" target="_blank" style="color:'black'"><b>${heading}</b> (link)</a>
</div>
<div class="description">
<h2>Description</h2>
${description}
</div>
<div style="font-size:12px;">(scroll down for solution)</div>
</br>
<div class="solution" style="page-break-before: always; page-break-after: always;">
<h2>Solution</h2>
<div class="language">Language: ${language}</div>
<div class="solution-status">Status: Accepted</div>
<pre><code>${highlightedSolution}</code></pre>
</div>
</div>
`;
}
htmlContent += `
</body>
</html>
`;
return htmlContent;
}
// Function to open print dialog for specific HTML content
function printHTML(htmlContent) {
sendMessageToPopup({
button: `Download PDF <span style="font-size: 16px; margin-left: 2px;">⤋</span>`,
processing: false,
});
// Open the HTML content in a new window
const newWindow = window.open();
newWindow.document.write(htmlContent);
// Close the document to complete the loading process
newWindow.document.close();
}
LeetCodeSubmissionDownloader.pullFromLeetCodeAPI = function () {
if (LeetCodeSubmissionDownloader.MAX_ATTEMPT <= 0) {
sendMessageToPopup({
process_status: "Error: Something Went Wrong",
button: "Please wait...",
processing: false,
});
return;
}
try {
let address = new URL(
LeetCodeSubmissionDownloader.LEETCODE_SUBMISSIONS_API +
"?offset=" +
LeetCodeSubmissionDownloader.last +
"&limit=" +
LeetCodeSubmissionDownloader.INCREASE_LAST_BY
);
let xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
LeetCodeSubmissionDownloader.processAPIResults(xmlHttp.responseText);
} else if (xmlHttp.readyState == 4) {
LeetCodeSubmissionDownloader.handleError();
}
};
xmlHttp.addEventListener("error", function () {
LeetCodeSubmissionDownloader.handleError();
});
xmlHttp.open("GET", address, true);
xmlHttp.send(null);
} catch (e) {
LeetCodeSubmissionDownloader.handleError();
}
};
LeetCodeSubmissionDownloader.handleError = async function () {
LeetCodeSubmissionDownloader.waitUsedBetweenRequests *=
LeetCodeSubmissionDownloader.INCREASE_WAIT_BY_ON_ERROR;
sendMessageToPopup({
process_status: "Error: Failed to pull API data",
processing: true,
});
console.log(
"Error: Failed to pull API data from " +
LeetCodeSubmissionDownloader.LEETCODE_SUBMISSIONS_API +
". Doubling the wait time between requests to " +
LeetCodeSubmissionDownloader.waitUsedBetweenRequests +
" milliseconds."
);
LeetCodeSubmissionDownloader.MAX_ATTEMPT -= 1;
await sleep(LeetCodeSubmissionDownloader.waitUsedBetweenRequests);
LeetCodeSubmissionDownloader.pullFromLeetCodeAPI();
};
// example question in questionMap
// const question = {
// title: title,
// heading:titleHeading,
// language: submissionsFound[i].lang,
// description: questionContent,
// solution: String(submissionsFound[i].code),
// question_id: submissionsFound[i].question_id,
// };
LeetCodeSubmissionDownloader.processAPIResults = async function (
lastWebResult
) {
let data = JSON.parse(lastWebResult);
let submissionsFound = data.submissions_dump;
LeetCodeSubmissionDownloader.last += submissionsFound.length;
LeetCodeSubmissionDownloader.processed += 0;
for (let i = 0; i < submissionsFound.length; i++) {
const title = submissionsFound[i].title_slug;
const titleHeading =
submissionsFound[i].question_id + " " + submissionsFound[i].title;
if (
title in LeetCodeSubmissionDownloader.questionMap ||
submissionsFound[i]?.status_display?.toLowerCase() !== "accepted"
) {
// if question is already in map
// or
// status is not accepted then continue
continue;
}
LeetCodeSubmissionDownloader.processed += 1;
await fetchQuestionContent(title)
.then((questionContent) => {
// Log question content
const question = {
title: title,
heading: titleHeading,
language: submissionsFound[i].lang,
description: questionContent,
solution: String(submissionsFound[i].code),
question_id: submissionsFound[i].question_id,
};
LeetCodeSubmissionDownloader.questionMap[title] = question;
})
.catch((error) => {
console.error("Failed to fetch question content:", error);
});
}
if (!data.has_next) {
sendMessageToPopup({
process_status:
`<span style="color:green;"> Total submissions processed: ` +
LeetCodeSubmissionDownloader.processed +
"</span>",
processing: false,
});
console.log(
"Total Questions processed: " + LeetCodeSubmissionDownloader.processed
);
fetchUsername()
.then((username) => {
const htmlContent = generateHTMLContent(username);
printHTML(htmlContent);
})
.catch((error) => {
console.error("Failed to get username Error:", error);
const htmlContent = generateHTMLContent("");
printHTML(htmlContent);
});
} else {
// Send messages to the popup script
sendMessageToPopup({ button: "Loading...", processing: true });
sendMessageToPopup({
process_status:
`
<div class="ui active progress indeterminate" style="margin:7px 0 10px;">
<div class="bar" style="width:60%; height:10px; border-radius:8px; background-color:purple;"></div>
</div>
Please wait..
<div> Questions saved so far: <span style="color:green">` +
LeetCodeSubmissionDownloader.processed +
`</span></div>`,
processing: true,
});
console.log(
"Questions saved so far: " + LeetCodeSubmissionDownloader.processed
);
console.log("Batch Complete! Hold on, next batch in progress.");
await sleep(LeetCodeSubmissionDownloader.waitUsedBetweenRequests);
LeetCodeSubmissionDownloader.pullFromLeetCodeAPI();
}
};
function LeetCodeSubmissionDownloader() {
return this;
}
LeetCodeSubmissionDownloader.pullFromLeetCodeAPI();