-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
111 lines (97 loc) · 3.62 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
document.addEventListener('DOMContentLoaded', function() {
const summarizeButton = document.getElementById('summarizeButton');
const output = document.getElementById('output');
const slider = document.getElementById('summaryLevel');
if (!summarizeButton || !output || !slider) {
console.error('Required elements not found in the DOM');
return;
}
summarizeButton.addEventListener('click', async () => {
console.log('Summarize button clicked');
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (!tab) {
output.textContent = 'No active tab found. Please try again.';
console.error('No active tab found');
return;
}
const button = document.getElementById('summarizeButton');
summarizeButton.classList.add('loading');
summarizeButton.textContent = 'Summarizing...';
output.textContent = '';
try {
await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ['content.js']
});
const results = await chrome.scripting.executeScript({
target: { tabId: tab.id },
func: () => {
// Your existing email extraction code
return document.body.innerText; // Example extraction
}
});
if (results && results[0] && results[0].result) {
const text = results[0].result;
const apiUrl = 'https://summarizer-with-levels-354958375757.us-central1.run.app/summarize/';
const summaryLevel = getSummaryLevel();
try {
let response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ text, level: summaryLevel })
});
while (response.status === 301 || response.status === 302) {
const location = response.headers.get('Location');
if (!location) throw new Error('Redirect location not found');
response = await fetch(location, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ text, level: summaryLevel })
});
}
if (response.ok) {
const data = await response.json();
output.textContent = data.summary;
output.classList.add('typing-effect');
} else {
output.textContent = 'Error: ' + response.statusText;
console.error('API response error:', response.statusText);
}
} catch (error) {
output.textContent = 'Error: ' + error.message;
console.error('Fetch error:', error);
}
} else {
console.error('No results from email extraction script');
}
} catch (error) {
output.textContent = 'Error: ' + error.message;
console.error('Script execution error:', error);
} finally {
button.classList.remove('loading');
summarizeButton.textContent = 'Summarize';
setTimeout(() => output.classList.remove('typing-effect'), 1000);
}
});
function getSummaryLevel() {
const slider = document.getElementById('summaryLevel');
const levels = ['shortest', 'normal', 'elaborative'];
return levels[Math.round(slider.value)];
}
function animateText(text) {
let index = 0;
output.textContent = ''; // Clear output before starting the animation
function type() {
if (index < text.length) {
output.textContent += text[index];
index++;
setTimeout(type, 2000); // Adjust typing speed (50ms delay)
}
}
type(); // Start the animation
}
});