-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
75 lines (67 loc) · 2.09 KB
/
background.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
const OPENAI_API_URL = 'https://api.openai.com/v1/chat/completions';
const PROMPT_PREFIX = 'Summarize the following text in the text\'s original language: ';
async function summarize(apiKey, text) {
const prompt = PROMPT_PREFIX + text;
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({
model: "gpt-3.5-turbo",
messages: [
{
role: "user",
content: prompt
}
],
temperature: 0.7
})
};
const response = await fetch(OPENAI_API_URL, requestOptions);
const data = await response.json();
return data.choices && data.choices[0] && data.choices[0].message.content.trim();
}
async function createSummaryTab(summary) {
const summaryHtml = `
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Summary</title>
<style>
body {
font-family: -apple-system,BlinkMacSystemFont,"Segoe UI","Noto Sans",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji";
max-width: 44rem;
margin: 0 auto;
padding: 1em;
font-size: 1.3rem;
line-height: 1.4;
}
pre { white-space: pre-wrap; word-wrap: break-word; }
</style>
</head>
<body>
<h1>Summary</h1>
<pre>${summary}</pre>
</body>
</html>
`;
const summaryUrl = URL.createObjectURL(new Blob([summaryHtml], { type: 'text/html' }));
await browser.tabs.create({ url: summaryUrl });
}
browser.runtime.onMessage.addListener(handleMessage);
async function handleMessage(message, sender, sendResponse) {
if (message.action === 'summarize') {
const apiKey = await browser.storage.sync.get('api_key').then((res) => res.api_key);
if (!apiKey) {
alert('Please set your OpenAI API key in the extension settings.');
return;
}
const summary = await summarize(apiKey, message.text);
createSummaryTab(summary);
// Send a response back to the popup.
sendResponse();
}
}