-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
353 lines (342 loc) · 9.92 KB
/
popup.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
let btn = document.getElementById("btn");
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
let title = message.title;
let questions = message.questions;
createPDF(title, questions);
});
btn.addEventListener("click", async () => {
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (tab.url.match(/sanfoundry.com/)) {
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: scrapeData,
});
} else {
alert("This extension only works on sanfoundry.com");
}
});
function scrapeData() {
let title = document.querySelector("h1.entry-title");
let content = document.querySelector("div.entry-content");
let questions = [];
function formatChoices(str) {
// Splitting it to array contains choices and answers
let choices = str.split(/(^[a-z]\))/im);
// removing the new lines between the answer.
choices = choices.map((choice) => {
choice = choice.trim();
return choice;
});
let modifiedChoices = {};
for (let i = 1; i < choices.length; i += 2) {
modifiedChoices[choices[i]] = choices[i + 1] + "\n";
}
return modifiedChoices;
}
var i = 0;
let isAD = function (element) {
if (
element &&
element.classList &&
(element.classList.contains("google-auto-placed") ||
element.classList.contains("ap_container") ||
element.classList.contains("google-auto-placed ap_container") ||
element.classList.contains("sf-mobile-ads") ||
element.classList.contains("sf-desktop-ads"))
) {
return true;
}
return false;
};
let QuestionWithCode = function (child, content) {
let questionText = child.innerText;
let codeText = "";
let choices = "";
let answer = "";
let explanation = "";
let ExplanationCode = "";
i++;
// get the code
// skip ad
while (isAD(content.children[i])) {
i++;
}
// skip website recommendation
while (i < content.children.length) {
let child = content.children[i];
if (child.tagName === "PRE" || child.querySelector("pre")) {
break;
}
i++;
}
// skip ad
while (isAD(content.children[i])) {
i++;
}
// get the pre tag
let pre = content.children[i];
// get the code formatted
codeText = pre.innerText;
// get the choices
while (content.children[i].tagName !== "P") {
i++;
}
let x = "";
if (
content.children[i].tagName === "P" ||
content.children[i].tagName === "PRE"
) {
while (
content.children[i].tagName === "P" ||
content.children[i].tagName === "PRE"
) {
x += content.children[i].innerText + "\n";
i++;
}
}
choices = x.replace("View Answer", "");
choices = choices.trim();
// console.log(formatChoices(choices));
choices = formatChoices(choices);
// get the answer
// while it is an AD
while (isAD(content.children[i])) {
i++;
}
// the answer is in div tag, skip any other tags
while (content.children[i].tagName !== "DIV") {
i++;
}
let answerDiv = content.children[i];
answer = answerDiv.innerText;
// remove "Answer: " from the string
// remove "Explanation: " from the string
answer = answer.replace("Answer: ", "");
answer = answer.replace("Explanation: ", "");
let newAnswer = answer.split("\n");
answer = newAnswer[0];
// trim the answer
answer = answer.trim();
explanation = newAnswer[1];
let preTag = answerDiv.querySelector("pre");
if (preTag) {
ExplanationCode = preTag.innerText;
explanation += "\n" + ExplanationCode;
}
return {
questionText,
codeText,
choices,
answer,
explanation,
};
};
let Question = function (child, content) {
let questionText = child.innerText.replace("View Answer", "");
let questionLines = questionText.split("\n");
let choicesStartIndex = questionLines.findIndex((line) =>
line.match(/^[a-z]\)/)
);
let questionLinesWithoutChoices = questionLines.slice(0, choicesStartIndex);
let choicesLines = questionLines.slice(choicesStartIndex, -1);
let questionTextWithoutChoices = questionLinesWithoutChoices.join("\n");
let choices = formatChoices(choicesLines.join("\n"));
let codeText = "";
let answer = "";
let explanation = "";
let ExplanationCode = "";
i++;
while (isAD(content.children[i])) {
i++;
}
let answerDiv = content.children[i];
answer = answerDiv.innerText;
answer = answer.replace("Answer: ", "");
answer = answer.replace("Explanation: ", "");
let newAnswer = answer.split("\n");
answer = newAnswer[0];
// trim the answer
answer = answer.trim();
explanation = newAnswer[1];
let preTag = answerDiv.querySelector("pre");
if (preTag) {
ExplanationCode = preTag.innerText;
explanation += "\n" + ExplanationCode;
}
return {
questionText: questionTextWithoutChoices,
codeText,
choices,
answer,
explanation,
};
};
while (i < content.children.length) {
let child = content.children[i];
if (child.tagName === "P" && child.innerText.match(/^\d+\./)) {
if (child.children.length > 0) {
let question = Question(child, content, i);
questions.push(question);
} else {
let question = QuestionWithCode(child, content, i);
questions.push(question);
}
}
i++;
}
chrome.runtime.sendMessage({ title: title.innerText, questions });
}
function createPDF(title, questions) {
let doc = new jsPDF("p", "mm", [1200, 700]);
let x = 10,
y = 10,
lineHeight = 7,
fontSize = 12;
doc.setFontSize(fontSize);
doc.setFont("Lato-Regular");
doc.setFont("Roboto-Medium");
doc.setTextColor(0, 0, 0);
let text = title;
let textWidth =
(doc.getStringUnitWidth(title) * fontSize) / doc.internal.scaleFactor;
x = (doc.internal.pageSize.width - textWidth) / 2;
let textLines = doc.splitTextToSize(text, 220);
textLines.forEach((line) => {
doc.text(line, x, y);
y += lineHeight;
});
y += lineHeight;
y += lineHeight / 2;
doc.setFont("Lato-Regular");
for (let i = 0; i < questions.length; i++) {
let question = questions[i];
let remainingHeight = doc.internal.pageSize.height - y;
if (
checkQuestionHeight(
doc,
y,
lineHeight,
remainingHeight,
question,
fontSize
) == false
) {
doc.addPage();
y = 10;
remainingHeight = doc.internal.pageSize.height - y;
}
let questionText = question.questionText;
let codeText = question.codeText;
let choices = question.choices;
let answer = question.answer;
let explanation = question.explanation;
doc.setTextColor(0, 0, 0);
let x = 10;
let text = questionText;
let textWidth =
(doc.getStringUnitWidth(text) * fontSize) / doc.internal.scaleFactor;
let textLines = doc.splitTextToSize(text, 220);
textLines.forEach((line) => {
doc.text(line, x, y);
y += lineHeight;
});
if (codeText !== "") {
doc.setFont("courier");
let codeLines = doc.splitTextToSize(codeText, 220);
codeLines.forEach((line) => {
doc.text(line, x, y);
y += lineHeight - 1.3;
});
y += 0.5;
}
y += 1;
let choiceKeys = Object.keys(choices);
doc.setFont("Lato-Regular");
for (let i = 0; i < choiceKeys.length; i++) {
let choiceKey = choiceKeys[i];
let choiceValue = choices[choiceKey];
// get how many "\n" in the choiceValue
let newLineCount = (choiceValue.match(/\n/g) || []).length;
// add the newLineCount to the y
if (choiceKey[0] === answer) {
doc.setFont("Roboto-Medium");
doc.setTextColor(0, 0, 255);
} else {
doc.setFont("Lato-Regular");
doc.setTextColor(0, 0, 0);
}
doc.text(choiceKey + " " + choiceValue, x + 4, y);
y += lineHeight * newLineCount;
}
doc.setTextColor(175, 0, 0);
doc.setFont("Roboto-Medium");
doc.text("Explanation:", x, y);
doc.setFont("Lato-Regular");
y += lineHeight;
let explanationLines = doc.splitTextToSize(explanation, 220);
explanationLines.forEach((line) => {
doc.text(line, x, y);
y += lineHeight;
});
y += lineHeight;
if (y > doc.internal.pageSize.height + 300) {
doc.addPage();
y = 10;
}
doc.setTextColor(0, 0, 0);
}
doc.save(`${title}.pdf`);
}
function checkQuestionHeight(doc, y, lineHeight, remainingHeight, question) {
let questionText = question.questionText;
let codeText = question.codeText;
let choices = question.choices;
let answer = question.answer;
let explanation = question.explanation;
let text = questionText;
if (remainingHeight - lineHeight * 5 < 0) {
return false;
}
let textLines = doc.splitTextToSize(text, 220);
textLines.forEach((line) => {
y += lineHeight;
});
if (codeText !== "") {
let codeLines = doc.splitTextToSize(codeText, 220);
codeLines.forEach((line) => {
y += lineHeight;
});
}
let choicesLines = Object.entries(choices);
for (let i = 0; i < choicesLines.length; i++) {
let [choiceKey, choiceValue] = choicesLines[i];
if (choiceKey === answer) {
}
y += lineHeight;
}
let answerLines = doc.splitTextToSize(answer, 220);
answerLines.forEach((line) => {
y += lineHeight;
});
let explanationLines = doc.splitTextToSize(explanation, 220);
explanationLines.forEach((line) => {
y += lineHeight;
});
y += lineHeight;
if (y > doc.internal.pageSize.height) {
return false;
}
return true;
}
document.addEventListener("DOMContentLoaded", function () {
var links = document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++) {
(function () {
var ln = links[i];
var location = ln.href;
ln.onclick = function () {
chrome.tabs.create({ active: true, url: location });
};
})();
}
});