-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
84 lines (77 loc) · 2.91 KB
/
background.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
// auto starts the extension
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (
tab.url &&
tab.url.includes('https://docs.google.com/forms/d/') &&
tab.url.includes('/edit#responses')
) {
chrome.tabs.sendMessage(tabId, { action: 'insertAutoCheckButton' });
}
if (tab.url && tab.url.includes('docs.google.com/forms')) {
let queryParameter = '';
console.log('Tab URL: ' + tab.url);
const match = /\/d\/([a-zA-Z0-9-_]+)\//.exec(tab.url);
if (match && match[1]) {
queryParameter = match[1];
console.log('Unique ID: ' + queryParameter);
}
const urlParameters = new URLSearchParams(queryParameter);
chrome.tabs.sendMessage(tabId, {
type: 'NEW',
formId: queryParameter,
});
}
});
// sheet data to API
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.action === 'submitData') {
const apiURL = 'https://text-answer-handler.onrender.com/api/v1/formatter/get-feedback';
(async () => {
console.log("Request body: " + request.data);
console.log("Type of ", typeof request.data);
try {
const response = await fetch(apiURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(request.data),
});
const data = await response.json();
console.log(data);
sendResponse({ data: data });
} catch (error) {
console.error('Error:', error);
sendResponse({ error: error.message });
}
})();
return true;
}
});
// responsible to generate questions
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
const apiURL = 'https://prompt-enhancer.onrender.com/api/v1/suffix/promptAdd';
if (request.action == 'generateQuestions') {
(async () => {
try {
const response = await fetch(apiURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ prompt: request.userPrompt }),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
console.log(data);
sendResponse({ data: data });
} catch (error) {
console.error('Error:', error);
sendResponse({ error: error.message });
}
})();
return true; // Return true to indicate you wish to send a response asynchronously
}
});