-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsummarize_popup.js
143 lines (115 loc) · 4.18 KB
/
summarize_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
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
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';
chrome.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 chrome.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);
loadingElement.style.display = '';
summaryResultElement.classList.remove('error');
summaryResultElement.style.display = '';
summaryResultElement.innerText = 'Summarizing...';
copySummaryElement.style.display = 'none';
summaryTextContents = '';
await chrome.runtime.sendMessage({
type: 'summarize_page',
...Object.fromEntries(searchParams),
});
}
await requestPageSummary();
}
document.addEventListener('DOMContentLoaded', setup);