-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontentScript.js
452 lines (389 loc) · 16.1 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
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
let currentFormId = null; // This variable will hold the form ID
// let currentSheetId = '1ROzHfRXbtNW4n-oDd05o0RmDvEcup3rAxiKcxrtER80'; // This variable will hold the
let currentSheetId = localStorage.getItem('spreadsheetId');
let subject = null; // This variable will hold the subject name
// Listen for messages from the background script
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
if (message.action === 'insertAutoCheckButton') {
insertAutoCheckButton();
}
if (message.type === 'NEW') {
currentFormId = message.formId; // Store the form ID
console.log('Form ID received:', currentFormId);
}
});
// This function fetches and returns the content of prompt.html
function fetchPromptHtml() {
return fetch(chrome.runtime.getURL('prompt.html')).then((response) =>
response.text(),
);
}
// This function injects HTML content into the page
function injectHtml(html) {
const targetDivs = document.getElementsByClassName('q5O05c oydeSd');
if (targetDivs.length > 0) {
const targetDiv = targetDivs[0];
const container = document.createElement('div');
container.innerHTML = html;
targetDiv.parentNode.insertBefore(container, targetDiv.nextSibling);
setUpButtonListener();
}
}
function resetSearchButton() {
const searchButton = document.getElementById('searchButton');
const addQuestionsButton = document.getElementById('addQuestionsButton');
searchButton.style.display = 'inline-block';
addQuestionsButton.style.display = 'none';
searchButton.innerText = 'Search your result';
searchButton.disabled = false;
searchButton.style.backgroundColor = '#7860bf';
}
function disableSearchButton() {
const searchButton = document.getElementById('searchButton');
searchButton.disabled = true;
searchButton.innerText = 'Searching...'; // Optional: change the button text
searchButton.style.backgroundColor = '#aaa';
}
function enableSearchButton() {
const searchButton = document.getElementById('searchButton');
searchButton.disabled = false;
searchButton.innerText = 'Search your result'; // Reset the button text
searchButton.style.backgroundColor = '#7860bf';
}
function generateUrl(formId, questions) {
const scriptUrl =
'https://script.google.com/a/macros/kiit.ac.in/s/AKfycbzmUChMVYOzNTRwUb1M802iYeYZKZGR4O7iFocYqJBlr_aTTgzTvxemGcNvuAOjm-0/exec';
var encodedQuestions = encodeURIComponent(JSON.stringify(questions));
var fullUrl =
scriptUrl + '?formId=' + formId + '&questionData=' + encodedQuestions;
showAddQuestionsButton(fullUrl);
return fullUrl;
}
function showAddQuestionsButton(url) {
const searchButton = document.getElementById('searchButton');
const addQuestionsButton = document.getElementById('addQuestionsButton');
searchButton.style.display = 'none';
addQuestionsButton.style.display = 'inline-block';
addQuestionsButton.onclick = function () {
window.open(url, '_blank');
resetSearchButton();
};
}
// Sets up the click event listener for the "Search your result" button
function setUpButtonListener() {
const searchButton = document.getElementById('searchButton');
if (searchButton) {
searchButton.addEventListener('click', function () {
const userInput = document.getElementById('prompt').value;
const promptTypeSelect =
document.getElementById('promptTypeSelect');
const promptType =
promptTypeSelect.options[promptTypeSelect.selectedIndex].value;
handleSearch(userInput, promptType);
});
}
}
function showSpreadsheetIdInput() {
const targetDiv = document.querySelector('.P2pQDc');
if (!targetDiv) return;
// Textarea for input
const textarea = document.createElement('textarea');
textarea.id = 'spreadsheetIdInput';
textarea.style.cssText = 'width: 100%; height: 50px; margin-top: 10px;';
textarea.placeholder = 'Enter your linked spreadsheet ID here';
// Submit button
const submitButton = document.createElement('button');
submitButton.innerText = 'Submit';
submitButton.style.cssText =
'margin-top: 10px; padding: 10px 30px; color:white ; background-color: #7860bf; border: none;';
submitButton.addEventListener('click', function () {
const enteredId = textarea.value.trim();
if (enteredId) {
localStorage.setItem('spreadsheetId', enteredId); // Store the ID
currentSheetId = enteredId;
insertAutoCheckButton();
textarea.remove();
submitButton.remove();
} else {
alert('Please enter a valid spreadsheet ID.');
}
});
targetDiv.appendChild(textarea);
targetDiv.appendChild(submitButton);
}
function handleSearch(userPrompt, promptType) {
if (!userPrompt) {
alert('Please enter a prompt.');
return;
}
disableSearchButton();
if (!currentFormId) {
alert('Form ID has not been received yet.');
return;
}
// Send message to background script for GPT prompt
chrome.runtime.sendMessage(
{ action: 'generateQuestions', userPrompt: userPrompt },
function (response) {
if (response.error) {
alert('Error: ' + response.error);
enableSearchButton();
return;
}
if (response.data) {
const questions = response.data;
if (questions.length === 0) {
alert('No questions found');
enableSearchButton();
return;
}
console.log('Final Questions : ' + questions);
const url = generateUrl(currentFormId, questions);
console.log('Url : ' + url);
}
},
);
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initializeContentScript);
} else {
initializeContentScript();
}
function insertAutoCheckButton() {
if (document.getElementById('autoCheckButton') || !currentSheetId) {
return; // Do not proceed if button exists or sheet ID is missing
}
const targetDiv = document.querySelector('.P2pQDc'); // Select the target element
console.log('target', targetDiv);
if (targetDiv) {
// Create the button element
const button = document.createElement('button');
button.type = 'button';
button.id = 'autoCheckButton';
button.innerText = 'Auto Check Theory Questions';
button.style.cssText =
'cursor: pointer; padding: 10px; color: white; background-color: #7860bf; border: none;';
// Insert the button as the second child
targetDiv.insertBefore(button, targetDiv.children[1]);
}
// Find the button and add a click event listener
const autoCheckButton = document.getElementById('autoCheckButton');
if (autoCheckButton) {
autoCheckButton.addEventListener('click', function () {
insertSubjectInputField();
});
}
}
function initializeContentScript() {
if (currentSheetId) {
insertAutoCheckButton(); // Show button if ID is known
} else {
showSpreadsheetIdInput(); // Show input if ID is not known
}
fetchPromptHtml().then((html) => injectHtml(html));
}
function handleAutoCheckButtonClick() {
// Create a container for the URL input and instructions
const urlContainer = document.createElement('div');
urlContainer.id = 'copyDataContainer';
urlContainer.style.cssText = 'margin-top: 10px;';
// Add instruction text
const instructionText = document.createElement('p');
instructionText.innerText =
'Copy and paste this URL in a new tab, then copy the data. After copying, click the "I Have Copied the Data" button below.';
urlContainer.appendChild(instructionText);
// Create an input field for the URL
const urlInput = document.createElement('input');
urlInput.type = 'text';
urlInput.value = `https://script.google.com/a/macros/kiit.ac.in/s/AKfycbxwHrR53Fib2GGFCxXax44GxgOjRStYwWROZhCLRm-DoMRF7eqIQ_-uJLaJ588aKnjw/exec?sheetId=${currentSheetId}`;
urlInput.readOnly = true;
urlInput.style.cssText = 'width: 80%;';
urlContainer.appendChild(urlInput);
// Create a button for confirming after copying
const confirmButton = document.createElement('button');
confirmButton.innerText = 'I Have Copied the Data';
confirmButton.style.cssText = 'margin-top: 10px; padding: 10px;';
confirmButton.onclick = () => {
showPasteDataArea(); // Show the paste data area
};
urlContainer.appendChild(confirmButton);
// Append the container to the document
const targetDiv = document.querySelector('.P2pQDc');
if (targetDiv) {
targetDiv.appendChild(urlContainer);
}
// Automatically select the URL text
urlInput.focus();
urlInput.select();
}
function insertSubjectInputField() {
const targetDiv = document.querySelector('.P2pQDc');
if (!targetDiv) return;
// Create a container for the input field and button
const container = document.createElement('div');
container.style.cssText = 'margin-top: 10px;';
// Create the input field for the subject
const subjectInput = document.createElement('input');
subjectInput.type = 'text';
subjectInput.placeholder = 'Enter Subject Name';
subjectInput.style.cssText = 'padding: 5px;';
// Create the button to store the subject
const storeSubjectButton = document.createElement('button');
storeSubjectButton.innerText = 'Store Subject';
storeSubjectButton.style.cssText = 'margin-left: 10px; padding: 5px 10px; cursor: pointer; background-color: #4CAF50; color: white;';
// Append the input field and button to the container
container.appendChild(subjectInput);
container.appendChild(storeSubjectButton);
// Append the container to the target div
targetDiv.appendChild(container);
// Add click event listener to the button
storeSubjectButton.addEventListener('click', function () {
const subjectName = subjectInput.value.trim();
if (subjectName) {
storeSubjectName(subjectName);
container.remove(); // Remove the input field and button
handleAutoCheckButtonClick(); // Show the URL input and instructions
} else {
alert('Please enter a subject name.');
}
});
}
function storeSubjectName(name) {
subject = name; // Store the subject name in the global variable
console.log('Subject stored:', subject);
}
function showPasteDataArea() {
// Select or create the container for pasting data
let pasteContainer = document.getElementById('pasteDataContainer');
if (!pasteContainer) {
pasteContainer = document.createElement('div');
pasteContainer.id = 'pasteDataContainer';
const targetDiv = document.querySelector('.P2pQDc');
if (targetDiv) {
targetDiv.appendChild(pasteContainer);
}
}
// Clear previous contents (if any)
pasteContainer.innerHTML = '';
// Create the textarea for pasting data
const dataInput = document.createElement('textarea');
dataInput.id = 'pastedDataInput';
dataInput.placeholder = 'Paste your data here';
dataInput.style.cssText = 'width: 100%; height: 100px; margin-top: 10px;';
// Create the button for submitting the pasted data
const submitButton = document.createElement('button');
submitButton.id = 'submitPastedDataButton';
submitButton.innerText = 'Submit Pasted Data';
submitButton.style.cssText = 'margin-top: 10px; padding: 10px;';
submitButton.onclick = submitPastedData;
// Append the textarea and button to the pasteContainer
pasteContainer.appendChild(dataInput);
pasteContainer.appendChild(submitButton);
// Ensure the container is visible
pasteContainer.style.display = 'block';
}
async function submitPastedData() {
const pastedData = document.getElementById('pastedDataInput').value;
if (!pastedData) {
alert('Please paste the data');
return;
}
const parsedPastedData = JSON.parse(pastedData);
const refactoredData = refactorDataForAPI(parsedPastedData, subject);
console.log(refactoredData);
console.log("Type", typeof refactoredData);
displayLoadingScreen(true);
removeDataContainers();
try {
chrome.runtime.sendMessage(
{ action: 'submitData', data: refactoredData },
function (response) {
if (response.error) {
alert('Error: ' + response.error);
enableSearchButton();
return;
}
if (response.data) {
console.log('Response Data', response.data);
const encodedData = encodeURIComponent(JSON.stringify(response.data));
const marksUrl =
'https://script.google.com/a/macros/kiit.ac.in/s/AKfycbwRg07RjU858bjirv4r6Jht9txCaQ3j5SnpSlIiEWD9QrRTN10rYHZ3L5MY9n84N1HT/exec' +
'?spreadsheetId=' +
currentSheetId +
'&jsonData=' +
encodedData;
console.log('Marks URL: ' + marksUrl);
displayLoadingScreen(false);
displayURLContainer(marksUrl);
}
},
);
} catch (error) {
displayLoadingScreen(false);
alert('Error: ' + error);
}
}
function refactorDataForAPI(data, subject) {
return {
subject: subject,
sheet_data: data
};
}
function displayLoadingScreen(show) {
if (!show) {
const loadingScreen = document.getElementById('loadingScreen');
if (loadingScreen) {
loadingScreen.remove();
}
return;
}
const targetDiv = document.querySelector('.P2pQDc');
if (targetDiv) {
// Create a container for the loading indicator
const loadingContainer = document.createElement('div');
loadingContainer.id = 'loadingScreen';
loadingContainer.style.cssText = 'width:100%; margin-top: 10px;';
// Create a disabled button to act as a loading indicator
const loadingButton = document.createElement('button');
loadingButton.innerText = 'Loading...';
loadingButton.style.cssText = 'width:100%; padding: 10px; cursor: not-allowed; background-color: #f3f3f3; color: #999;';
loadingButton.disabled = true; // Disable the button to prevent clicks
// Append the button to the container
loadingContainer.appendChild(loadingButton);
// Insert the container into the page
targetDiv.appendChild(loadingContainer);
}
}
function displayURLContainer(url) {
const targetDiv = document.querySelector('.P2pQDc');
if (targetDiv) {
// Create a container for the button
const buttonContainer = document.createElement('div');
buttonContainer.id = 'urlButtonContainer';
buttonContainer.style.cssText = 'margin-top: 10px;';
// Add a button
const openUrlButton = document.createElement('button');
openUrlButton.innerText = 'Click Here to update marks';
openUrlButton.style.cssText = 'padding: 10px; cursor: pointer;';
// Append the button to the container
buttonContainer.appendChild(openUrlButton);
// Insert the container into the page
targetDiv.appendChild(buttonContainer);
// Add event listener to the button to open the URL in a new tab and then remove the button
openUrlButton.addEventListener('click', () => {
window.open(url, '_blank');
// Remove the button container from the UI
buttonContainer.remove();
});
}
}
function removeDataContainers() {
const urlContainer = document.getElementById('pasteDataContainer');
if (urlContainer && urlContainer.parentNode) {
urlContainer.parentNode.removeChild(urlContainer);
}
const copyData = document.getElementById('copyDataContainer');
if (copyData && copyData.parentNode) {
copyData.parentNode.removeChild(copyData);
}
}