-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
65 lines (64 loc) · 1.94 KB
/
content.js
File metadata and controls
65 lines (64 loc) · 1.94 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
"use strict";
const url = window.location.href;
const params = new URLSearchParams(url.split("?")[1]);
const query = params.get("q");
if (query && url.startsWith("https://www.google.com/search?")) {
// getting html element ready
document
.getElementById("extabar")
.insertAdjacentHTML("afterend", `<div id="chatGPTDiv"></div>`);
const chatDiv = document.getElementById("chatGPTDiv");
chatDiv.style.width = "var(--center-width)";
chatDiv.style.maxWidth =
"Calc(var(--center-abs-margin) + var(--center-width) + var(--rhs-margin) + var(--rhs-width))";
chatDiv.style.background = "#343541";
chatDiv.style.borderRadius = "4px";
chatDiv.style.padding = "8px";
chatDiv.style.fontFamily = "Google Sans,arial,sans-serif";
chatDiv.innerText = query;
const answerDiv = document.createElement("div");
answerDiv.style.padding = "8px";
answerDiv.style.fontFamily = "Google Sans,arial,sans-serif";
answerDiv.style.marginTop = "16px";
answerDiv.style.borderRadius = "4px";
answerDiv.style.color = "white";
answerDiv.style.backgroundColor = "#444654";
answerDiv.innerText = "loading...";
chatDiv.appendChild(answerDiv);
fetch(`https://api.openai.com/v1/completions`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer [API Kay]`,
},
body: JSON.stringify({
prompt: query,
model: "text-davinci-002",
temperature: 0.5,
max_tokens: 1024,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
}),
})
.then(async (response) => {
if (response.ok) {
return response.json();
} else {
const { error } = await response.json();
answerDiv.style.color = "red";
answerDiv.innerText = error.message;
}
})
.then((data) => {
if (data && data.choices && data.choices.length) {
const choices = data.choices;
answerDiv.innerText = choices[0].text;
}
})
.catch((error) => {
console.error(error);
answerDiv.style.color = "red";
answerDiv.innerText = error;
});
}