-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathbackground.js
118 lines (99 loc) · 3.12 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
import { getBrowser, getCurrentTabInfo, showBadge, removeBadge } from "./browser";
import { loadServerMetadata } from "./cache";
import { getConfiguration, isConfigurationComplete } from "./configuration";
import { LinkdingApi } from "./linkding";
const browser = getBrowser();
let api = null;
let configuration = null;
let hasCompleteConfiguration = false;
async function initApi() {
if (api) {
return true;
}
configuration = await getConfiguration();
hasCompleteConfiguration = isConfigurationComplete(configuration);
if (hasCompleteConfiguration) {
api = new LinkdingApi(configuration);
} else {
api = null;
}
return api !== null;
}
/* Dynamic badge */
async function setDynamicBadge(tabId, tabMetadata) {
// Set badge if tab is bookmarked
if (tabMetadata?.bookmark) {
showBadge(tabId);
} else {
removeBadge(tabId);
}
}
/* Omnibox / Search integration */
browser.omnibox.onInputStarted.addListener(async () => {
const isReady = await initApi();
const description = isReady
? "Search bookmarks in linkding"
: "⚠️ Please configure the linkding extension first";
browser.omnibox.setDefaultSuggestion({ description });
});
browser.omnibox.onInputChanged.addListener((text, suggest) => {
if (!api) {
return;
}
api
.search(text, { limit: 5 })
.then((results) => {
const bookmarkSuggestions = results.map((bookmark) => ({
content: bookmark.url,
description: bookmark.title || bookmark.website_title || bookmark.url,
}));
suggest(bookmarkSuggestions);
})
.catch((error) => {
console.error(error);
});
});
browser.omnibox.onInputEntered.addListener(async (content, disposition) => {
if (!hasCompleteConfiguration || !content) {
return;
}
const isUrl = /^http(s)?:\/\//.test(content);
const url = isUrl
? content
: `${configuration.baseUrl}/bookmarks?q=${encodeURIComponent(content)}`;
// Edge doesn't allow updating the New Tab Page (tested with version 117).
// Trying to do so will throw: "Error: Cannot update NTP tab."
// As a workaround, open a new tab instead.
if (disposition === "currentTab") {
const tabInfo = await getCurrentTabInfo();
if (tabInfo.url === "edge://newtab/") {
disposition = "newForegroundTab";
}
}
switch (disposition) {
case "currentTab":
browser.tabs.update({ url });
break;
case "newForegroundTab":
browser.tabs.create({ url });
break;
case "newBackgroundTab":
browser.tabs.create({ url, active: false });
break;
}
});
/* Precache bookmark / website metadata when tab or URL changes */
browser.tabs.onActivated.addListener(async (activeInfo) => {
const tabInfo = await getCurrentTabInfo();
let tabMetadata = await loadServerMetadata(tabInfo.url, true);
setDynamicBadge(activeInfo.tabId, tabMetadata);
});
browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
// Only interested in URL changes
// Ignore URL changes in non-active tabs
if (!changeInfo.url || !tab.active) {
return;
}
let tabMetadata = await loadServerMetadata(tab.url, true);
setDynamicBadge(tabId, tabMetadata);
});