-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconversation.js
More file actions
289 lines (224 loc) · 10.3 KB
/
conversation.js
File metadata and controls
289 lines (224 loc) · 10.3 KB
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
(() => {
// Function to extract questions, answers, or texts from the div elements with the class 'text-message'
function extractTexts() {
var textMessageDivs = document.querySelectorAll('div.text-message');
var texts = [];
textMessageDivs.forEach(function(div) {
texts.push(div.textContent.trim());
});
// Ensuring that the overall length of the array is even
if (texts.length % 2 !== 0) {
texts.push('');
}
return texts;
}
// Function to extract questions from the texts array
function extractQuestions() {
var texts = extractTexts();
var questions = [];
texts.forEach(function(text, index) {
if (index % 2 === 0) {
questions.push(text);
}
});
return questions;
}
// Function to extract answers from the texts array
function extractAnswers () {
// Selecting all the divs
const allDivs = document.querySelectorAll("div");
// Creating a new array
const answerDivs = [];
// Populating the newly created array with those divs whose first child is a paragraph
// element but which may or may not contain other elements as well
for (let i = 0; i < allDivs.length; i++) {
if (allDivs[i].firstElementChild && allDivs[i].firstElementChild.tagName === "P") {
answerDivs.push(allDivs[i]);
}
};
// Formatting the answers and pushing them into the 'answers' array
const answers = [];
for (let j=0; j<answerDivs.length; j++) {
let childNodes = answerDivs[j].childNodes;
let answer = "";
for (let k=0; k<childNodes.length; k++) {
if (childNodes[k].tagName === "P") {
answer += "P-Element" + " ";
answer += childNodes[k].innerText;
answer += "\\split-string-here\\";
} else if (childNodes[k].tagName === "TABLE") {
answer += "TABLE-Element" + " ";
answer += childNodes[k].innerText;
answer += "\\split-string-here\\";
} else if (childNodes[k].tagName === "UL") {
answer += "UNORDERED-LIST-Element" + " ";
answer += childNodes[k].innerText;
answer += "\\split-string-here\\";
} else if (childNodes[k].tagName === "OL") {
answer += "ORDERED-LIST-Element" + " ";
answer += childNodes[k].innerText;
answer += "\\split-string-here\\";
}
else if (childNodes[k].tagName === "PRE") {
answer += "CODE-Element" + " ";
answer += childNodes[k].innerText;
answer += "\\split-string-here\\";
}
}
answers.push(answer);
}
return answers;
};
function generatePDF(questions, answers, conversationTitle) {
// Defining the document and its styles components
var docDefinition = {
content: [],
styles: {
header: {
bold: true,
fontSize: 24,
decoration: "underline",
margin: [0, 0, 0, 32],
alignment: "center"
},
question: {
bold: true,
fontSize: 16,
lineHeight: 1.2,
margin: [0, 14, 0, 7]
},
paragraph: {
fontSize: 12,
margin: [0, 7, 0, 7]
},
list: {
fontSize: 12,
margin: [0, 5, 0, 5]
},
code: {
fontSize: 12,
italics: true,
preformatted: true,
margin: [7, 7, 7, 7],
background: '#DFDFDF'
}
}
}
// Adding the document header
docDefinition.content.push({text: conversationTitle, style: 'header'})
for (let i=0; i<questions.length; i++) {
// Pushing the question onto the document
docDefinition.content.push({ text: "Q: " + questions[i], style: 'question' });
// Pushing the answer onto the document
const answerElements = answers[i].split("\\split-string-here\\");
for (let j=0; j<answerElements.length; j++) {
const firstSpaceIndex = answerElements[j].indexOf(' ');
const elementIdentifier = answerElements[j].substr(0, firstSpaceIndex);
const elementContent = answerElements[j].substr(firstSpaceIndex + 1);
if (elementIdentifier === "P-Element") {
docDefinition.content.push({ text: elementContent, style: 'paragraph' });
} else if (elementIdentifier === "TABLE-Element") {
const tableNodes = elementContent.split('\n');
const tableBody = tableNodes.map(line => line.split('\t'));
tableBody[0] = tableBody[0].map(item => {
return {text: item, fontSize: 14, bold: true};
});
docDefinition.content.push({
table: {
headerRows: 1,
body: tableBody
},
layout: {
hLineWidth: function (i, node) {
return (i === 0 || i === node.table.body.length) ? 1.5 : 1;
},
vLineWidth: function (i, node) {
return (i === 0 || i === node.table.widths.length) ? 1.5 : 1;
},
hLineColor: function (i, node) {
return (i === 0 || i === node.table.body.length) ? 'black' : 'gray';
},
vLineColor: function (i, node) {
return (i === 0 || i === node.table.widths.length) ? 'black' : 'gray';
},
fillColor: function (i, node) {
return (i === 0) ? '#CCCCCC' : null;
},
paddingLeft: function(i, node) { return 7; },
paddingRight: function(i, node) { return 7; },
paddingTop: function(i, node) { return 7; },
paddingBottom: function(i, node) { return 7; }
}
});
} else if (elementIdentifier === "UNORDERED-LIST-Element") {
const listElements = elementContent.split("\n");
const formattedListElements = [];
for (let k=0; k<listElements.length; k++) {
if (listElements !== "") {
formattedListElements.push({text: listElements[k], style: 'list'})
}
}
docDefinition.content.push({
ul : formattedListElements
})
} else if (elementIdentifier === "ORDERED-LIST-Element") {
const listElements = elementContent.split("\n");
const formattedListElements = [];
for (let l=0; l<listElements.length; l++) {
if (listElements[l] !== "") {
formattedListElements.push({text: listElements[l], style: 'list'})
}
}
docDefinition.content.push({
ol : formattedListElements
})
} else if (elementIdentifier === "CODE-Element") {
const formattedElementContent = elementContent.replace("Copy", "").replace("code", "")
const firstSpaceIndex = formattedElementContent.indexOf(' ');
const language = formattedElementContent.substr(0, firstSpaceIndex);
const code = formattedElementContent.substr(firstSpaceIndex + 2);
docDefinition.content.push({text: " " + language, fontSize: 12, color: '#DFDFDF', background:
"black"})
docDefinition.content.push({ text: code, style: "code"});
}
}
}
const pdfDoc = pdfMake.createPdf(docDefinition);
return pdfDoc;
};
let questions;
let answers;
let title;
let interval;
let pdf;
// Extracting the conversation data
chrome.runtime.onMessage.addListener((message, sender, response) => {
if (message.action === "extractData") {
interval = setInterval(() => {
answers = extractAnswers();
questions = extractQuestions();
if (answers.length !== 0 && questions.length !== 0 && answers.length === questions.length) {
clearInterval(interval)
title = document.title;
}
}, 500)
}
})
// Generating the PDF document
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "generatePDF") {
if (answers && questions && answers.length === questions.length && answers.length > 0) {
pdf = generatePDF(questions, answers, title);
sendResponse({ reply: "pdfGenerated" });
} else {
sendResponse({ reply: "error", message: "Data was not extracted yet!" });
}
}
});
// Downloading the PDF document
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "downloadPDF") {
pdf.download(`${title}.pdf`)
}
});
}) ();