-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
53 lines (46 loc) · 1.9 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
document.getElementById('summarizeBtn').addEventListener('click', async () => {
const inputText = document.getElementById('inputText').value;
let textToSummarize = inputText;
// If no input text, fetch the current webpage's content
if (!inputText) {
try {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
const result = await chrome.scripting.executeScript({
target: { tabId: tab.id },
func: () => document.body.innerText
});
// Debugging: Log the fetched text
console.log("Fetched webpage content:", result[0].result);
textToSummarize = result[0].result;
// Check if the fetched text is empty or too short
if (!textToSummarize || textToSummarize.trim().length < 10) {
document.getElementById('result').innerText = "Error: The webpage content is too short or empty.";
return;
}
} catch (error) {
console.error("Error fetching webpage content:", error);
document.getElementById('result').innerText = "Error: Unable to fetch webpage content.";
return;
}
}
// Show loading message
document.getElementById('result').innerText = "Summarizing...";
// Send the text to the Flask backend
try {
const response = await fetch('http://127.0.0.1:5000/summarize', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: textToSummarize })
});
const data = await response.json();
// Display the summary or error message
if (data.summary) {
document.getElementById('result').innerText = data.summary;
} else {
document.getElementById('result').innerText = "Error: " + (data.error || "Unable to summarize.");
}
} catch (error) {
console.error("Error:", error); // Log the error to the console
document.getElementById('result').innerText = "Error: Failed to connect to the server.";
}
});