-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontent.js
103 lines (98 loc) · 3.42 KB
/
content.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
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
var div
if (document.readyState === "complete") {
div = injectDivBlock();
} else {
window.addEventListener("load", () => {
div = injectDivBlock()
});
}
//check message:
var prompt="";
switch (message.info.menuItemId) {
case 'wording':
prompt = "Check spelling and grammar";
break;
case 'improve':
prompt = "Can you improve following";
break;
default:
console.log("no default prompt match");
};
fetch(message.config.chatGPT_api_URL+"/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + message.config.chatGPT_api_key
},
body: JSON.stringify({
model: message.config.selectedModel,
messages: [{ "role": "user", "content": prompt + ". Input text: "+ message.info.selectionText }]
})
})
.then(response => {
if (!response.ok) {
throw new Error("Following error occured while fetching response \r\n Error " + response.status + ": " + response.statusText);
}
return response.json()
})
.then(data => {
// get the first suggestion from the response
const suggestion = data.choices[0].message.content.trim();
const resultContent = div.querySelector(".result-content");
resultContent.innerText = suggestion;
// Check toggle options from storage
chrome.storage.sync.get(["addCopyButtonToggle", "autoCopyResultToggle"], function(options) {
if (options.addCopyButtonToggle) {
div.querySelector(".result-copy").style.display = "block";
}
if (options.autoCopyResultToggle) {
copyTextToClipboard(suggestion);
}
});
})
.catch(error => {
console.log(error)
// show alert if something went wrong.
const resultContent = div.querySelector(".result-content");
resultContent.innerHTML = error.toString();
});
});
function copyTextToClipboard(text) {
navigator.clipboard.writeText(text).then(function() {
//console.log('Copying to clipboard was successful!');
}, function(err) {
console.error('Could not copy text: ', err);
});
}
function injectDivBlock() {
const div = document.createElement("div");
div.innerHTML = `
<div class="result-header">
<span class="result-close">×</span>
</div>
<div class="result-content">
<div class="loading-text">Generating response</div>
<div class="loading-dots">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
</div>
<div class="result-footer">
<input class="result-copy" type="button" value="Copy" />
</div>
`;
div.classList.add("result-block");
document.body.appendChild(div);
const closeButton = div.querySelector(".result-close");
closeButton.addEventListener("click", () => {
div.remove();
});
const copyButton = div.querySelector(".result-copy");
copyButton.addEventListener("click", () => {
copyTextToClipboard(div.querySelector(".result-content").innerText);
});
return div
}