-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
82 lines (75 loc) · 2.43 KB
/
popup.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
document.addEventListener('DOMContentLoaded', function () {
// Retrieve the stored API key
chrome.storage.local.get(['apiKey'], function (result) {
const apiKey = result.apiKey
// Check if API key is available
if (!apiKey) {
// Redirect to apiKeyInput.html if API key is not available
window.location.href = 'apiKeyInput.html'
return
}
// Add event listener for the Summarize button
document
.getElementById('summarizeButton')
.addEventListener('click', async function () {
// Fetch the text content from the current tab
const tabQuery = { active: true, currentWindow: true }
chrome.tabs.query(tabQuery, async function (tabs) {
const currentTab = tabs[0]
chrome.scripting.executeScript(
{
target: { tabId: currentTab.id },
function: getTextFromPage,
},
async (result) => {
if (result && result[0] && result[0].result) {
const pageText = result[0].result
const summary = await fetchSummary(apiKey, pageText)
// Display the summary
document.getElementById('summaryArea').value = summary
} else {
console.error('Failed to fetch text from the page.')
}
}
)
})
})
})
})
// Function to fetch text content from the current webpage
function getTextFromPage() {
// Retrieve text content (you can make this more sophisticated)
return document.body.innerText
}
// Function to call OpenAI API for text summarization
async function fetchSummary(apiKey, text) {
try {
const payload = {
model: 'gpt-3.5-turbo',
messages: [
{
role: 'user',
content: `Please summarize the following text: ${text}`,
},
],
temperature: 0.7,
}
const response = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify(payload),
})
const data = await response.json()
if (data.choices && data.choices.length > 0) {
return data.choices[0].message.content
} else {
return 'Unable to generate summary.'
}
} catch (error) {
console.error('Error fetching summary:', error)
return 'Error fetching summary.'
}
}