-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup_reference_openAI.js
More file actions
64 lines (61 loc) · 2.32 KB
/
popup_reference_openAI.js
File metadata and controls
64 lines (61 loc) · 2.32 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
document.getElementById('replyBtn').addEventListener('click', async () => {
// Get the message from the LinkedIn message box
chrome.tabs.query({ active: true, currentWindow: true }, async ([tab]) => {
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: () => {
let input = document.querySelector('[contenteditable="true"][role="textbox"]');
let message = input ? input.innerText : '';
return message;
},
}, async (results) => {
const messageText = results && results[0] ? results[0].result : '';
if (!messageText) {
alert('Could not read the message box!');
return;
}
// Call OpenAI API to generate a reply
const apiKey = 'YOUR_OPENAI_API_KEY'; // <-- Replace with your OpenAI API key
const apiUrl = 'https://api.openai.com/v1/chat/completions';
const data = {
model: 'gpt-3.5-turbo',
messages: [
{ role: 'system', content: 'You are a helpful assistant that writes professional LinkedIn message replies.' },
{ role: 'user', content: messageText }
],
max_tokens: 200
};
try {
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify(data)
});
const result = await response.json();
const reply = result.choices && result.choices[0] && result.choices[0].message && result.choices[0].message.content
? result.choices[0].message.content.trim()
: 'Could not generate a reply.';
// Insert the reply into the message box
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: (reply) => {
let input = document.querySelector('[contenteditable="true"][role="textbox"]');
if (input) {
input.focus();
input.innerHTML = `<p>${reply.replace(/\n/g, '<br>')}</p>`;
input.dispatchEvent(new Event('input', { bubbles: true }));
} else {
alert('Message input not found!');
}
},
args: [reply]
});
} catch (e) {
alert('Failed to generate reply: ' + e.message);
}
});
});
});