-
Notifications
You must be signed in to change notification settings - Fork 3
/
service_worker.js
75 lines (66 loc) · 1.93 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
const kagiBaseUrl = "https://kagi.com/";
let extensionToken = undefined; // use process memory to hold the token
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === chrome.runtime.OnInstalledReason.INSTALL) {
chrome.tabs.create({ url: kagiBaseUrl });
chrome.contextMenus.create({
id: "kagi-image-search",
title: "Kagi Image Search",
contexts: ["image"],
});
}
});
async function loadTokenFromCookies() {
const cookie = await chrome.cookies.get({
url: kagiBaseUrl,
name: "kagi_session",
});
if (!cookie || !cookie.value || cookie.value.trim().length === 0) {
return;
}
let valuesDiffer = cookie.value !== extensionToken;
extensionToken = cookie.value;
if (valuesDiffer) await updateRules();
}
chrome.webRequest.onCompleted.addListener(loadTokenFromCookies, {
urls: ["https://*.kagi.com/*"],
});
async function updateRules() {
// https://developer.chrome.com/docs/extensions/reference/api/declarativeNetRequest#dynamic-and-session-rules
// dynamic needed as the token can't be known ahead of time
await chrome.declarativeNetRequest.updateDynamicRules({
addRules: [
{
id: 1,
priority: 1,
action: {
type: "modifyHeaders",
requestHeaders: [
{
header: "X-Kagi-Authorization",
value: extensionToken,
operation: "set",
},
],
},
condition: {
urlFilter: `||kagi.com/`,
resourceTypes: ["main_frame", "xmlhttprequest"],
},
},
],
removeRuleIds: [1],
});
}
// Image Search
function kagiImageSearch(info) {
const imageUrl = encodeURIComponent(info.srcUrl);
chrome.tabs.create({
url: `${kagiBaseUrl}images?q=${imageUrl}&reverse=reference`,
});
}
chrome.contextMenus.onClicked.addListener((info, _) => {
if (info.menuItemId === "kagi-image-search") {
kagiImageSearch(info);
}
});