-
Notifications
You must be signed in to change notification settings - Fork 21
/
summarize_result.js
194 lines (155 loc) · 5.52 KB
/
summarize_result.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { fetchSettings, getActiveTab } from './lib/utils.js';
if (!globalThis.browser) {
globalThis.browser = chrome;
}
let summaryTextContents = '';
async function setup() {
const loadingElement = document.querySelector('#loading');
if (!loadingElement) {
console.error('Could not find loading div');
return;
}
const summaryResultElement = document.querySelector('#summary_result');
if (!summaryResultElement) {
console.error('Could not find summarize result div');
return;
}
summaryResultElement.style.display = 'none';
summaryResultElement.classList.remove('error');
const copySummaryElement = document.querySelector('#copy_summary');
if (!copySummaryElement) {
console.error('Could not find copy summary button');
return;
}
copySummaryElement.style.display = 'none';
copySummaryElement.addEventListener('click', async () => {
if (!summaryTextContents) {
return;
}
try {
const summaryToCopy = summaryTextContents.trim().replaceAll('\n', '\n\n');
await navigator.clipboard.writeText(summaryToCopy);
copySummaryElement.innerText = 'Copied!';
setTimeout(() => {
copySummaryElement.innerText = 'Copy summary';
}, 3000);
} catch (error) {
console.error('error copying summary to clipboard: ', error);
}
});
const summaryStatsElement = document.querySelector('#summary_stats');
if (!summaryStatsElement) {
console.error('Could not find summarize stats div');
return;
}
summaryStatsElement.style.display = 'none';
const summaryStatsTimeSavedElement = document.querySelector(
'#summary_stats_time_saved',
);
if (!summaryStatsTimeSavedElement) {
console.error('Could not find summarize stats time saved element');
return;
}
summaryStatsTimeSavedElement.innerText = '0 minutes';
const summaryCloseElement = document.getElementById('close_summary');
if (!summaryCloseElement) {
console.error('Could not find summarize close element');
return;
}
summaryCloseElement.style.display = 'none';
browser.runtime.onMessage.addListener(async (data) => {
const searchParams = new URLSearchParams(window.location.search);
const url = searchParams.get('url');
if (data.type === 'summary_finished' && data.url === url) {
loadingElement.style.display = 'none';
if (data.success) {
summaryResultElement.classList.remove('error');
summaryTextContents = new DOMParser().parseFromString(
data.summary.replaceAll(/<br>/g, '\n'),
'text/html',
).documentElement.textContent;
copySummaryElement.style.display = '';
} else {
summaryResultElement.classList.add('error');
summaryTextContents = data.summary;
copySummaryElement.style.display = 'none';
}
summaryResultElement.style.display = '';
summaryResultElement.innerText = summaryTextContents;
if (data.timeSavedInMinutes) {
summaryStatsElement.style.display = '';
summaryStatsTimeSavedElement.innerText = `${
data.timeSavedInMinutes
} minute${data.timeSavedInMinutes !== 1 ? 's' : ''}`;
}
summaryCloseElement.style.display = '';
summaryCloseElement.addEventListener('click', () => {
window.close();
});
}
});
window.addEventListener('keydown', (event) => {
if (event.key === 'Escape') window.close();
});
async function requestPageSummary() {
const hasTabAccess = await browser.permissions.contains({
permissions: ['activeTab'],
});
if (!hasTabAccess) {
summaryResultElement.style.display = '';
summaryResultElement.classList.add('error');
summaryResultElement.innerText =
"You can't summarize without allowing access to the currently active tab.";
return;
}
const searchParams = new URLSearchParams(window.location.search);
if (!searchParams.get('summary_type')) {
searchParams.set('summary_type', 'summary');
}
if (!searchParams.get('target_language')) {
searchParams.set('target_language', '');
}
// If there's no URL, get the currently active tab and default params
if (!searchParams.get('url')) {
const tab = await getActiveTab(true);
if (!tab) {
console.error('No tab/url found.');
return;
}
searchParams.set('url', tab.url);
// Add ?url=<tab.url> to the window, so it receives the proper summary
const popupUrl = new URL(window.location.href);
popupUrl.searchParams.set('url', tab.url);
window.history.replaceState(null, '', popupUrl.toString());
}
const { token, api_token, api_engine, summary_type, target_language } =
await fetchSettings();
if (token) {
searchParams.set('token', token);
}
if (api_token) {
searchParams.set('api_token', api_token);
}
if (api_engine) {
searchParams.set('api_engine', api_engine);
}
if (summary_type) {
searchParams.set('summary_type', summary_type);
}
if (target_language) {
searchParams.set('target_language', target_language);
}
loadingElement.style.display = '';
summaryResultElement.classList.remove('error');
summaryResultElement.style.display = '';
summaryResultElement.innerText = 'Summarizing...';
copySummaryElement.style.display = 'none';
summaryTextContents = '';
await browser.runtime.sendMessage({
type: 'summarize_page',
...Object.fromEntries(searchParams),
});
}
await requestPageSummary();
}
document.addEventListener('DOMContentLoaded', setup);