@@ -20,6 +20,21 @@ function scrapeData() {
2020 let title = document . querySelector ( "h1.entry-title" ) ;
2121 let content = document . querySelector ( "div.entry-content" ) ;
2222 let questions = [ ] ;
23+ function formatChoices ( str ) {
24+ // Splitting it to array contains choices and answers
25+ let choices = str . split ( / ( [ a - z ] \) ) / i) ;
26+
27+ // removing the new lines between the answer.
28+ choices = choices . map ( ( choice ) => {
29+ choice = choice . trim ( ) ;
30+ return choice ;
31+ } ) ;
32+ let modifiedChoices = { } ;
33+ for ( let i = 1 ; i < choices . length ; i += 2 ) {
34+ modifiedChoices [ choices [ i ] ] = choices [ i + 1 ] + "\n" ;
35+ }
36+ return modifiedChoices ;
37+ }
2338 var i = 0 ;
2439 let isAD = function ( element ) {
2540 if (
@@ -53,12 +68,25 @@ function scrapeData() {
5368 while ( content . children [ i ] . tagName !== "P" ) {
5469 i ++ ;
5570 }
56- let p = content . children [ i ] ;
57- choices = p . innerText ;
58- choices = choices . replace ( "View Answer" , "" ) ;
71+ let x = "" ;
72+ if (
73+ content . children [ i ] . tagName === "P" ||
74+ content . children [ i ] . tagName === "PRE"
75+ ) {
76+ while (
77+ content . children [ i ] . tagName === "P" ||
78+ content . children [ i ] . tagName === "PRE"
79+ ) {
80+ x += content . children [ i ] . innerText + "\n" ;
81+ i ++ ;
82+ }
83+ }
84+ choices = x . replace ( "View Answer" , "" ) ;
85+ choices = choices . trim ( ) ;
86+ // console.log(formatChoices(choices));
87+ choices = formatChoices ( choices ) ;
5988 // get the answer
6089 // while it is an AD
61- i ++ ;
6290 while ( isAD ( content . children [ i ] ) ) {
6391 i ++ ;
6492 }
@@ -89,6 +117,7 @@ function scrapeData() {
89117 let Question = function ( child , content ) {
90118 let questionText = child . innerText . replace ( "View Answer" , "" ) ;
91119 let choices = questionText . split ( "\n" ) . slice ( 1 , - 1 ) . join ( "\n" ) ;
120+ choices = formatChoices ( choices ) ;
92121 let codeText = "" ;
93122 let answer = "" ;
94123 let explanation = "" ;
@@ -133,17 +162,10 @@ function scrapeData() {
133162 }
134163 i ++ ;
135164 }
136- // print title
137- console . log ( title . innerText ) ;
138- // print questions
139- for ( let i = 0 ; i < questions . length ; i ++ ) {
140- console . log ( questions [ i ] ) ;
141- }
165+
142166 chrome . runtime . sendMessage ( { title : title . innerText , questions } ) ;
143167}
144168
145- function format ( title , questions ) { }
146-
147169function createPDF ( title , questions ) {
148170 let doc = new jsPDF ( "p" , "mm" , [ 250 , 360 ] ) ;
149171 let x = 10 ,
@@ -195,7 +217,7 @@ function createPDF(title, questions) {
195217 let text = questionText ;
196218 let textWidth =
197219 ( doc . getStringUnitWidth ( text ) * fontSize ) / doc . internal . scaleFactor ;
198- let textLines = doc . splitTextToSize ( text , 250 ) ;
220+ let textLines = doc . splitTextToSize ( text , 230 ) ;
199221 textLines . forEach ( ( line ) => {
200222 doc . text ( line , x , y ) ;
201223 y += lineHeight ;
@@ -205,22 +227,28 @@ function createPDF(title, questions) {
205227 codeLines . forEach ( ( line ) => {
206228 doc . setFont ( "Courier" , fontStyle [ 1 ] ) ;
207229 doc . text ( line , x , y ) ;
208- y += lineHeight ;
230+ y += lineHeight - 1.5 ;
209231 } ) ;
210232 }
211- let choicesLines = doc . splitTextToSize ( choices , 230 ) ;
212- for ( let i = 0 ; i < choicesLines . length ; i ++ ) {
213- doc . setFont ( "Times" , fontStyle [ 1 ] ) ;
214- if ( choicesLines [ i ] . charAt ( 0 ) === answer ) {
233+ let choiceKeys = Object . keys ( choices ) ;
234+ for ( let i = 0 ; i < choiceKeys . length ; i ++ ) {
235+ let choiceKey = choiceKeys [ i ] ;
236+ let choiceValue = choices [ choiceKey ] ;
237+ // get how many "\n" in the choiceValue
238+ let newLineCount = ( choiceValue . match ( / \n / g) || [ ] ) . length ;
239+ // add the newLineCount to the y
240+ if ( choiceKey [ 0 ] === answer ) {
215241 doc . setFont ( "Times" , fontStyle [ 0 ] ) ;
216242 doc . setTextColor ( 0 , 0 , 128 ) ;
243+ } else {
244+ doc . setFont ( "Times" , fontStyle [ 1 ] ) ;
245+ doc . setTextColor ( 0 , 0 , 0 ) ;
217246 }
218- doc . text ( choicesLines [ i ] , x + 4 , y ) ;
219- doc . setFont ( "Times" , fontStyle [ 1 ] ) ;
220- doc . setTextColor ( 0 , 0 , 0 ) ;
221- y += lineHeight ;
247+ doc . text ( choiceKey + " " + choiceValue , x + 4 , y ) ;
248+ y += lineHeight * newLineCount ;
222249 }
223250 doc . setFont ( "Times" , fontStyle [ 1 ] ) ;
251+ doc . setTextColor ( 128 , 0 , 0 ) ;
224252 doc . text ( "Explanation:" , x , y ) ;
225253 y += lineHeight ;
226254 let explanationLines = doc . splitTextToSize ( explanation , 230 ) ;
@@ -233,6 +261,7 @@ function createPDF(title, questions) {
233261 doc . addPage ( ) ;
234262 y = 10 ;
235263 }
264+ doc . setTextColor ( 0 , 0 , 0 ) ;
236265 }
237266
238267 doc . save ( `${ title } .pdf` ) ;
@@ -244,47 +273,34 @@ function checkQuestionHeight(doc, y, lineHeight, remainingHeight, question) {
244273 let choices = question . choices ;
245274 let answer = question . answer ;
246275 let explanation = question . explanation ;
247- let fontSize = 12 ;
248- let fontStyle = [ "bold" , "normal" ] ;
249276 let text = questionText ;
250- let textWidth =
251- ( doc . getStringUnitWidth ( text ) * fontSize ) / doc . internal . scaleFactor ;
252277 if ( remainingHeight - lineHeight * 5 < 0 ) {
253278 return false ;
254279 }
255- doc . setFontSize ( fontSize ) ;
256- doc . setFont ( "Times" , fontStyle [ 0 ] ) ;
257- doc . setTextColor ( 0 , 0 , 0 ) ;
258- let x = 10 ;
259280 let textLines = doc . splitTextToSize ( text , 250 ) ;
260281 textLines . forEach ( ( line ) => {
261- // doc.text(line, x, y);
262282 y += lineHeight ;
263283 } ) ;
264284 if ( codeText !== "" ) {
265285 let codeLines = doc . splitTextToSize ( codeText , 250 ) ;
266286 codeLines . forEach ( ( line ) => {
267- doc . setFont ( "Courier" , fontStyle [ 1 ] ) ;
268- // doc.text(line, x, y);
269287 y += lineHeight ;
270288 } ) ;
271289 }
272- let choicesLines = doc . splitTextToSize ( choices , 250 ) ;
273- choicesLines . forEach ( ( line ) => {
274- doc . setFont ( "Times" , fontStyle [ 1 ] ) ;
275- // doc.text(line, x, y);
290+ let choicesLines = Object . entries ( choices ) ;
291+ for ( let i = 0 ; i < choicesLines . length ; i ++ ) {
292+ let [ choiceKey , choiceValue ] = choicesLines [ i ] ;
293+ if ( choiceKey === answer ) {
294+ }
276295 y += lineHeight ;
277- } ) ;
296+ }
297+
278298 let answerLines = doc . splitTextToSize ( answer , 250 ) ;
279299 answerLines . forEach ( ( line ) => {
280- doc . setFont ( "Times" , fontStyle [ 0 ] ) ;
281- // doc.text(line, x, y);
282300 y += lineHeight ;
283301 } ) ;
284302 let explanationLines = doc . splitTextToSize ( explanation , 250 ) ;
285303 explanationLines . forEach ( ( line ) => {
286- doc . setFont ( "Times" , fontStyle [ 1 ] ) ;
287- // doc.text(line, x, y);
288304 y += lineHeight ;
289305 } ) ;
290306 y += lineHeight ;
@@ -305,4 +321,4 @@ document.addEventListener("DOMContentLoaded", function () {
305321 } ;
306322 } ) ( ) ;
307323 }
308- } ) ;
324+ } ) ;
0 commit comments