-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
contentScript.js
90 lines (80 loc) · 3.05 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
function getEmailBody(signatureDelimiter) {
const selector = "div[aria-label='Message Body'], div[aria-label='Message text'], div.editable";
const element = document.querySelector(selector);
if (element) {
let emailText = element.innerHTML;
if (signatureDelimiter && emailText.includes(signatureDelimiter)) {
emailText = emailText.substring(0, emailText.indexOf(signatureDelimiter)).trim();
}
return emailText;
}
return null;
}
async function sendToChatGPT(text, styles, apiKey, signatureDelimiter) {
const response = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({
messages: [{"role": "user", "content": `Review and provide suggestions for the following email draft combining the following styles or only a single style if only one is provided: ${styles.join(', ')}. Please return only the revised email text without suggesting a subject. Email draft: ${text}`}],
model: "gpt-3.5-turbo",
max_tokens: 150,
n: 1,
stop: null,
temperature: 0.8
})
});
const data = await response.json();
if (data.choices && data.choices.length > 0) {
displaySuggestions(data.choices[0].message.content, signatureDelimiter);
} else {
console.log("No suggestions received");
}
}
function displaySuggestions(suggestions, signatureDelimiter) {
const selector = "div[aria-label='Message Body'], div[aria-label='Message text'], div.editable";
const element = document.querySelector(selector);
if (element) {
let newText = suggestions.trim();
newText = newText.replace(/\n/g, '<br>');
if (signatureDelimiter) {
const signatureIndex = element.innerHTML.indexOf(signatureDelimiter);
if (signatureIndex !== -1) {
const signature = element.innerHTML.substring(signatureIndex);
newText = newText + '<br><br>' + signature;
}
}
if (element.getAttribute('contenteditable') === 'true') {
element.focus();
document.execCommand('selectAll', false, null);
document.execCommand('insertHTML', false, newText);
} else {
element.innerHTML = newText;
}
}
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "reviewEmail") {
const selectedStyles = request.styles;
if (request) {
chrome.storage.sync.get(["apiKey", "signatureDelimiter"], result => {
if (result.apiKey) {
const emailBody = getEmailBody(result.signatureDelimiter);
sendToChatGPT(emailBody, selectedStyles, result.apiKey, result.signatureDelimiter).then(() => {
sendResponse({ success: true });
}).catch(() => {
sendResponse({ success: false });
});
} else {
alert("Please enter and save your OpenAI API key in the extension settings.");
sendResponse({ success: false });
}
});
} else {
sendResponse({ success: false });
}
return true;
}
});