-
Notifications
You must be signed in to change notification settings - Fork 21
/
background.js
272 lines (239 loc) · 6.6 KB
/
background.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import { summarizeContent, fetchSettings } from './lib/utils.js';
if (!globalThis.browser) {
globalThis.browser = chrome;
}
let sessionToken = undefined;
let syncSessionFromExisting = true;
let sessionApiToken = undefined;
let sessionApiEngine = undefined;
let sessionSummaryType = undefined;
let sessionTargetLanguage = undefined;
let sessionPrivacyConsent = false;
let IS_CHROME = true;
// Very hacky, but currently works flawlessly
if (typeof browser.runtime.getBrowserInfo === 'function') {
IS_CHROME = false;
}
async function saveToken(
{
token,
api_token,
api_engine,
sync,
summary_type,
target_language,
privacy_consent,
} = {},
isManual = false,
) {
sessionToken = typeof token !== 'undefined' ? token : sessionToken;
sessionApiToken =
typeof api_token !== 'undefined' ? api_token : sessionApiToken;
sessionApiEngine =
typeof api_engine !== 'undefined' ? api_engine : sessionApiEngine;
sessionSummaryType =
typeof summary_type !== 'undefined' ? summary_type : sessionSummaryType;
sessionTargetLanguage =
typeof target_language !== 'undefined'
? target_language
: sessionTargetLanguage;
sessionPrivacyConsent =
typeof privacy_consent !== 'undefined'
? privacy_consent
: sessionPrivacyConsent;
let shouldSync = sync || !isManual;
if (typeof sessionToken === 'undefined' || sessionToken.trim().length === 0) {
shouldSync = true;
await browser.runtime.sendMessage({ type: 'reset' });
}
syncSessionFromExisting = shouldSync;
try {
await browser.storage.local.set({
session_token: token,
sync_existing: shouldSync,
api_token: sessionApiToken,
api_engine: sessionApiEngine,
summary_type: sessionSummaryType,
target_language: sessionTargetLanguage,
privacy_consent: sessionPrivacyConsent,
});
} catch (error) {
console.error(error);
await browser.runtime.sendMessage({
type: 'save-error',
});
return;
}
await updateRules();
// tell the extension popup to update the UI
await browser.runtime.sendMessage({
type: 'synced',
token: sessionToken,
api_token: sessionApiToken,
api_engine: sessionApiEngine,
summary_type: sessionSummaryType,
target_language: sessionTargetLanguage,
privacy_consent: sessionPrivacyConsent,
});
}
async function summarizePage(options) {
const { summary, success, timeSavedInMinutes } =
await summarizeContent(options);
if (summary) {
await browser.runtime.sendMessage({
type: 'summary_finished',
summary,
success,
url: options.url,
timeSavedInMinutes,
});
}
}
/*
* Adds an Authorization header to all Kagi requests.
* This allows us to provide authentication without having to
* track if the user is logged in between incognito and normal windows.
*/
async function updateRules() {
if (!sessionToken) return;
await browser.declarativeNetRequest.updateDynamicRules({
addRules: [
{
id: 1,
priority: 1,
action: {
type: 'modifyHeaders',
requestHeaders: [
{
header: 'Authorization',
value: sessionToken,
operation: 'set',
},
],
},
condition: {
urlFilter: '|https://kagi.com/*',
resourceTypes: ['main_frame'],
},
},
],
removeRuleIds: [1],
});
}
/*
* Attempts to grab sessions from existing Kagi windows.
* This allows us to track the users last session without
* having to force them to input it in to the extension.
*/
async function checkForSession() {
if (!syncSessionFromExisting) return;
const cookie = await browser.cookies.get({
url: 'https://kagi.com',
name: 'kagi_session',
});
if (!cookie || !cookie.value) return;
const token = cookie.value;
if (sessionToken !== token) {
sessionToken = token;
await saveToken({ token, sync: true });
}
}
browser.webRequest.onBeforeRequest.addListener(
checkForSession,
{ urls: ['https://kagi.com/*'] },
[],
);
browser.runtime.onMessage.addListener(async (data) => {
switch (data.type) {
case 'save_token': {
await saveToken(data, true);
break;
}
case 'open_extension': {
if (IS_CHROME) {
await browser.tabs.create({
url: `chrome://extensions/?id=${browser.runtime.id}`,
});
} else {
await browser.tabs.create({
url: `addons://detail/${browser.runtime.id}`,
});
}
break;
}
case 'summarize_page': {
await summarizePage(data);
break;
}
default:
console.debug(`Invalid type received: ${data.type}`);
break;
}
});
browser.commands?.onCommand.addListener(async (command) => {
if (command === 'summarize-active-page') {
await browser.windows.create({
url: browser.runtime.getURL('src/summarize_result.html'),
focused: true,
width: 600,
height: 500,
type: 'popup',
});
}
});
async function loadStorageData() {
const {
token,
sync_existing,
api_token,
api_engine,
summary_type,
target_language,
} = await fetchSettings();
sessionToken = token;
syncSessionFromExisting = sync_existing;
sessionApiToken = api_token;
sessionApiEngine = api_engine;
sessionSummaryType = summary_type;
sessionTargetLanguage = target_language;
}
loadStorageData();
// The function kagiSummarize is called when clicking the context menu item.
async function kagiSummarize(info) {
// The linkUrl will be undefined if function is triggered by a page event. In that case, the url is taken from pageUrl
const url = info.linkUrl || info.pageUrl;
await browser.windows.create({
url: browser.runtime.getURL(
`src/summarize_result.html?url=${encodeURIComponent(url)}`,
),
focused: true,
width: 600,
height: 500,
type: 'popup',
});
}
function kagiImageSearch(info) {
const imageUrl = info.srcUrl;
browser.tabs.create({
url: `https://kagi.com/images?q=${imageUrl}&reverse=reference`,
});
}
// Create a context menu item.
browser.contextMenus.create({
id: 'kagi-summarize',
title: 'Kagi Summarize',
contexts: ['link', 'page'], // Show the menu item when clicked on a link or elsewhere on page with no matching contexts
});
browser.contextMenus.create({
id: 'kagi-image-search',
title: 'Kagi Image Search',
contexts: ['image'],
});
// Add a listener for the context menu item.
browser.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === 'kagi-summarize') {
kagiSummarize(info, tab);
} else if (info.menuItemId === 'kagi-image-search') {
kagiImageSearch(info, tab);
}
});