-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice_worker.js
176 lines (152 loc) · 4.35 KB
/
service_worker.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
chrome.runtime.onMessage.addListener(async (data) => {
switch (data.type) {
case "summarize_page": {
await summarizePage(data);
break;
}
case "save_settings": {
await saveSettings(data);
break;
}
}
});
async function getSettings() {
const settings = await chrome.storage.local.get("summarizer_settings");
return settings?.summarizer_settings || {};
}
async function saveSettings(options) {
const engine = options?.engine || "cecil";
const summary_type = options?.summary_type || "summary";
const target_language = options?.target_language || null;
const api_token = options?.api_token || "";
try {
await chrome.storage.local.set({
summarizer_settings: {
engine: engine,
summary_type: summary_type,
target_language: target_language,
api_token: api_token,
}
});
} catch (error) {
console.error(error);
}
}
async function summarizePage(options) {
const { summary, success, timeSavedInMinutes } =
await summarizeContent(options);
if (summary) {
await chrome.runtime.sendMessage({
type: 'summary_finished',
summary,
success,
url: options.url,
timeSavedInMinutes,
});
}
}
chrome.contextMenus.create({
id: 'kagi-summarize',
title: 'Kagi Summarize',
contexts: ['link', 'page'],
});
chrome.contextMenus.onClicked.addListener(async (info, _tab) => {
if (info.menuItemId === 'kagi-summarize') {
// 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 chrome.windows.create({
url: chrome.runtime.getURL(
`summarize_popup.html?url=${encodeURIComponent(url)}`,
),
focused: true,
width: 600,
height: 500,
type: 'popup',
});
}
});
export async function summarizeContent({
url,
text,
}) {
const settings = await getSettings();
let summary = 'Unknown error';
let success = false;
let timeSavedInMinutes = 0;
const useApi = Boolean(
settings.api_token && ((settings.engine && settings.engine !== "cecil") || text),
);
try {
const requestParams = {
url,
summary_type: settings.summary_type || "summary",
};
if (settings.target_language) {
requestParams.target_language = settings.target_language;
}
if (useApi) {
if (settings.engine) {
requestParams.engine = settings.engine;
}
if (text) {
requestParams.text = text;
requestParams.url = undefined;
}
}
const searchParams = new URLSearchParams(requestParams);
const headers = {
"Content-Type": "application/json"
};
if (useApi)
headers["Authorization"] = `Bot ${settings.api_token}`;
const requestOptions = {
method: 'GET',
headers: headers,
credentials: 'include',
};
const response = await fetch(
`${
useApi
? 'https://kagi.com/api/v0/summarize'
: 'https://kagi.com/mother/summary_labs'
}?${searchParams.toString()}`,
requestOptions,
);
if (response.status === 200) {
const result = await response.json();
if (useApi) {
if (result.data?.output) {
summary = result.data.output;
} else if (result.error) {
summary = JSON.stringify(result.error);
}
} else {
summary = result?.output_text || 'Unknown error';
timeSavedInMinutes = result?.output_data?.word_stats?.time_saved || 0;
}
success = Boolean(result) && !Boolean(result.error);
} else {
console.error('summarize error', response.status, response.statusText);
if (response.status === 401) {
summary = 'Invalid Token! Please set a new one.';
} else {
if (!response.statusText && response.headers.get('Content-Type') == 'application/json') {
const result = await response.json();
if (result.error && result.error.length != 0) {
const error = result.error[0];
summary = `Error: ${error.code} - ${error.msg}`;
}
} else {
summary = `Error: ${response.status} - ${response.statusText}`;
}
}
}
} catch (error) {
summary = error.message ? `Error: ${error.message}` : JSON.stringify(error);
}
return {
summary,
success,
timeSavedInMinutes,
};
}