Skip to content

Commit e227261

Browse files
committed
initial commit
0 parents  commit e227261

7 files changed

+124
-0
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Kagi Chrome Extension
2+
3+
This enables basic Kagi features for your Chromium browser (Chrome, Brave, Edge, Opera, [etc.](https://en.wikipedia.org/wiki/Chromium_(web_browser)#Browsers_based_on_Chromium)).
4+
5+
Install from the [Chrome Web Store](TODO ADD LINK) (not yet available)
6+
7+
## Features
8+
- Sets Kagi as your default search engine
9+
- Preserves your login across private browsing.
10+
- Search-by-image by right-clicking on an image.

icons/icon_16px.png

5.42 KB
Loading

icons/icon_180px.png

2.42 KB
Loading

icons/icon_32px.png

6.36 KB
Loading

icons/icon_48px.png

1.33 KB
Loading

manifest.json

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"manifest_version": 3,
3+
"name": "Kagi Search",
4+
"version": "1.0",
5+
"description": "Set Kagi as your default search engine, while also preserving your session in private browsing. Remember to enable 'Allow Incognito'.",
6+
"background": {
7+
"service_worker": "service_worker.js",
8+
"type": "module"
9+
},
10+
"icons": {
11+
"16": "icons/icon_16px.png",
12+
"32": "icons/icon_32px.png",
13+
"48": "icons/icon_48px.png",
14+
"180": "icons/icon_180px.png"
15+
},
16+
"action": {
17+
"default_icon": "icons/icon_32px.png",
18+
"default_title": "Kagi Search"
19+
},
20+
"permissions": [
21+
"contextMenus",
22+
"cookies",
23+
"declarativeNetRequest",
24+
"webRequest"
25+
],
26+
"host_permissions": ["https://*.kagi.com/*"],
27+
"chrome_settings_overrides": {
28+
"search_provider": {
29+
"name": "Kagi",
30+
"search_url": "https://kagi.com/search?q={searchTerms}",
31+
"favicon_url": "https://assets.kagi.com/v2/favicon-32x32.png",
32+
"keyword": "@kagi",
33+
"is_default": true,
34+
"suggest_url": "https://kagi.com/api/autosuggest?q={searchTerms}",
35+
"encoding": "UTF-8"
36+
}
37+
},
38+
"incognito": "spanning"
39+
}

service_worker.js

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const kagiBaseUrl = "https://kagi.com/";
2+
let extensionToken = undefined; // use process memory to hold the token
3+
4+
chrome.runtime.onInstalled.addListener((details) => {
5+
if (details.reason === chrome.runtime.OnInstalledReason.INSTALL) {
6+
chrome.tabs.create({ url: kagiBaseUrl });
7+
}
8+
});
9+
10+
async function loadTokenFromCookies() {
11+
const cookie = await chrome.cookies.get({
12+
url: kagiBaseUrl,
13+
name: "kagi_session",
14+
});
15+
16+
if (!cookie || !cookie.value || cookie.value.trim().length === 0) {
17+
return;
18+
}
19+
20+
let valuesDiffer = cookie.value !== extensionToken;
21+
extensionToken = cookie.value;
22+
if (valuesDiffer) await updateRules();
23+
}
24+
25+
chrome.webRequest.onCompleted.addListener(loadTokenFromCookies, {
26+
urls: [`${kagiBaseUrl}*`],
27+
});
28+
29+
async function updateRules() {
30+
// https://developer.chrome.com/docs/extensions/reference/api/declarativeNetRequest#dynamic-and-session-rules
31+
// dynamic needed as the token can't be known ahead of time
32+
await chrome.declarativeNetRequest.updateDynamicRules({
33+
addRules: [
34+
{
35+
id: 1,
36+
priority: 1,
37+
action: {
38+
type: "modifyHeaders",
39+
requestHeaders: [
40+
{
41+
header: "Authorization",
42+
value: extensionToken,
43+
operation: "set",
44+
},
45+
],
46+
},
47+
condition: {
48+
urlFilter: `${kagiBaseUrl}*`,
49+
resourceTypes: ["main_frame"],
50+
},
51+
},
52+
],
53+
removeRuleIds: [1],
54+
});
55+
}
56+
57+
// Image Search
58+
function kagiImageSearch(info) {
59+
const imageUrl = encodeURIComponent(info.srcUrl);
60+
chrome.tabs.create({
61+
url: `${kagiBaseUrl}images?q=${imageUrl}&reverse=reference`,
62+
});
63+
}
64+
65+
chrome.contextMenus.create({
66+
id: "kagi-image-search",
67+
title: "Kagi Image Search",
68+
contexts: ["image"],
69+
});
70+
71+
chrome.contextMenus.onClicked.addListener((info, tab) => {
72+
if (info.menuItemId === "kagi-image-search") {
73+
kagiImageSearch(info, tab);
74+
}
75+
});

0 commit comments

Comments
 (0)