-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
86 lines (71 loc) · 2.68 KB
/
main.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
const api_key = ''; // your API key here (sk-xxxxxx)
askToGPT = function (word) {
try {
var query = word.selectionText;
if (api_key == '' || api_key == undefined) {
chrome.tabs.create({ url: "javascript:alert('Please enter your API key!')" });
return;
}
fetch("https://api.openai.com/v1/completions", {
body: JSON.stringify({
"model": "text-davinci-003",
"prompt": "Selesaikan soal ini dengan jawaban A,B,C,D atau E\n\nQuestion : " + query + "\nAnswer :",
"max_tokens": 100,
"temperature": 0,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0,
"stop": ["\n"]
}),
headers: {
Authorization: "Bearer " + api_key,
"Content-Type": "application/json"
},
method: "POST"
}).then(function (response) {
return response.json();
}).then(function (json) {
if (json.error && json.error.code != '') {
chrome.notifications.create({
type: "basic",
iconUrl: "assets/icons/icon.png",
title: "Solve This Questions! - Error",
message: `[${json.error.code}] ${json.error.message ?? ''}`,
});
} else {
var answer = json.choices[0].text;
// clear all notifications
chrome.notifications.getAll(function (notifications) {
for (var key in notifications) {
chrome.notifications.clear(key);
}
});
chrome.notifications.create({
type: "basic",
iconUrl: "assets/icons/icon.png",
title: "Solve This Questions! - Answer",
message: answer,
});
}
}).catch(function (error) {
console.log(error);
});
} catch (error) {
console.log(error);
}
};
chrome.contextMenus.removeAll(function () {
chrome.contextMenus.create({
id: "1",
title: "Solve this question!",
contexts: ["selection"],
});
})
chrome.runtime.onInstalled.addListener(() => {
if (api_key == '' || api_key == undefined) {
chrome.tabs.create({ url: "javascript:alert('Please set your API key in the extension options!')" });
} else {
chrome.tabs.create({ url: "javascript:alert('All set! You can now use the extension!')" });
}
});
chrome.contextMenus.onClicked.addListener(askToGPT);