aboutsummaryrefslogtreecommitdiffstats
path: root/apps/browser-extension/src/background/background.ts
blob: 04089cc8cbddd26fbc2bb30abd6fc27f942d0e75 (plain) (blame)
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import {
  BookmarkTypes,
  ZNewBookmarkRequest,
} from "@karakeep/shared/types/bookmarks";

import { clearBadgeStatus, getBadgeStatus } from "../utils/badgeCache";
import {
  getPluginSettings,
  Settings,
  subscribeToSettingsChanges,
} from "../utils/settings";
import { getApiClient, initializeClients } from "../utils/trpc";
import { MessageType } from "../utils/type";
import { isHttpUrl } from "../utils/url";
import { NEW_BOOKMARK_REQUEST_KEY_NAME } from "./protocol";

const OPEN_KARAKEEP_ID = "open-karakeep";
const ADD_LINK_TO_KARAKEEP_ID = "add-link";
const CLEAR_CURRENT_CACHE_ID = "clear-current-cache";
const CLEAR_ALL_CACHE_ID = "clear-all-cache";
const SEPARATOR_ID = "separator-1";
const VIEW_PAGE_IN_KARAKEEP = "view-page-in-karakeep";

/**
 * Check the current settings state and register or remove context menus accordingly.
 * @param settings The current plugin settings.
 */
async function checkSettingsState(settings: Settings) {
  await initializeClients();
  if (settings?.address && settings?.apiKey) {
    registerContextMenus(settings);
  } else {
    removeContextMenus();
    await clearAllCache();
  }
}

/**
 * Remove context menus from the browser.
 */
function removeContextMenus() {
  try {
    chrome.contextMenus.removeAll();
  } catch (error) {
    console.error("Failed to remove context menus:", error);
  }
}

/**
 * Register context menus in the browser.
 * * A context menu button to open a tab with the currently configured karakeep instance.
 * * * If the "show count badge" setting is enabled, add context menu buttons to clear the cache for the current page or all pages.
 * * A context menu button to add a link to karakeep without loading the page.
 * @param settings The current plugin settings.
 */
function registerContextMenus(settings: Settings) {
  removeContextMenus();
  chrome.contextMenus.create({
    id: OPEN_KARAKEEP_ID,
    title: "Open Karakeep",
    contexts: ["action"],
  });

  chrome.contextMenus.create({
    id: ADD_LINK_TO_KARAKEEP_ID,
    title: "Add to Karakeep",
    contexts: ["link", "page", "selection", "image"],
  });

  if (settings?.showCountBadge) {
    chrome.contextMenus.create({
      id: VIEW_PAGE_IN_KARAKEEP,
      title: "View this page in Karakeep",
      contexts: ["action", "page"],
    });
    if (settings?.useBadgeCache) {
      // Add separator
      chrome.contextMenus.create({
        id: SEPARATOR_ID,
        type: "separator",
        contexts: ["action"],
      });

      chrome.contextMenus.create({
        id: CLEAR_CURRENT_CACHE_ID,
        title: "Clear Current Page Cache",
        contexts: ["action"],
      });

      chrome.contextMenus.create({
        id: CLEAR_ALL_CACHE_ID,
        title: "Clear All Cache",
        contexts: ["action"],
      });
    }
  }
}

/**
 * Handle context menu clicks by opening a new tab with karakeep or adding a link to karakeep.
 * @param info Information about the context menu click event.
 * @param tab The current tab.
 */
async function handleContextMenuClick(
  info: chrome.contextMenus.OnClickData,
  tab?: chrome.tabs.Tab,
) {
  const { menuItemId, selectionText, srcUrl, linkUrl, pageUrl } = info;
  if (menuItemId === OPEN_KARAKEEP_ID) {
    getPluginSettings().then((settings: Settings) => {
      chrome.tabs.create({ url: settings.address, active: true });
    });
  } else if (menuItemId === CLEAR_CURRENT_CACHE_ID) {
    await clearCurrentPageCache();
  } else if (menuItemId === CLEAR_ALL_CACHE_ID) {
    await clearAllCache();
  } else if (menuItemId === ADD_LINK_TO_KARAKEEP_ID) {
    addLinkToKarakeep({
      selectionText,
      srcUrl,
      linkUrl,
      pageUrl,
      title: tab?.title,
    });

    // NOTE: Firefox only allows opening context menus if it's triggered by a user action.
    // awaiting on any promise before calling this function will lose the "user action" context.
    await chrome.action.openPopup();
  } else if (menuItemId === VIEW_PAGE_IN_KARAKEEP) {
    if (tab) {
      await searchCurrentUrl(tab.url);
    }
  }
}

/**
 * Add a link to karakeep based on the provided information.
 * @param options An object containing information about the link to add.
 */
function addLinkToKarakeep({
  selectionText,
  srcUrl,
  linkUrl,
  pageUrl,
  title,
}: {
  selectionText?: string;
  srcUrl?: string;
  linkUrl?: string;
  pageUrl?: string;
  title?: string;
}) {
  let newBookmark: ZNewBookmarkRequest | null = null;
  if (selectionText) {
    newBookmark = {
      type: BookmarkTypes.TEXT,
      text: selectionText,
      sourceUrl: pageUrl,
      source: "extension",
    };
  } else {
    const finalUrl = srcUrl ?? linkUrl ?? pageUrl;

    if (finalUrl && isHttpUrl(finalUrl)) {
      newBookmark = {
        type: BookmarkTypes.LINK,
        url: finalUrl,
        source: "extension",
        title,
      };
    } else {
      console.warn("Invalid URL, bookmark not created:", finalUrl);
    }
  }
  if (newBookmark) {
    chrome.storage.session.set({
      [NEW_BOOKMARK_REQUEST_KEY_NAME]: newBookmark,
    });
  }
}

/**
 * Search current URL and open appropriate page.
 */
async function searchCurrentUrl(tabUrl?: string) {
  try {
    if (!tabUrl || !isHttpUrl(tabUrl)) {
      console.warn("Invalid URL, cannot search:", tabUrl);
      return;
    }
    console.log("Searching bookmarks for URL:", tabUrl);

    const settings = await getPluginSettings();
    const serverAddress = settings.address;

    const matchedBookmarkId = await getBadgeStatus(tabUrl);
    let targetUrl: string;
    if (matchedBookmarkId) {
      // Found exact match, open bookmark details page
      targetUrl = `${serverAddress}/dashboard/preview/${matchedBookmarkId}`;
      console.log("Opening bookmark details page:", targetUrl);
    } else {
      // No exact match, open search results page
      const searchQuery = encodeURIComponent(`url:${tabUrl}`);
      targetUrl = `${serverAddress}/dashboard/search?q=${searchQuery}`;
      console.log("Opening search results page:", targetUrl);
    }
    await chrome.tabs.create({ url: targetUrl, active: true });
  } catch (error) {
    console.error("Failed to search current URL:", error);
  }
}

/**
 * Clear badge cache for the current active page.
 */
async function clearCurrentPageCache() {
  try {
    // Get the active tab
    const [activeTab] = await chrome.tabs.query({
      active: true,
      currentWindow: true,
    });

    if (activeTab.url && activeTab.id) {
      console.log("Clearing cache for current page:", activeTab.url);
      await clearBadgeStatus(activeTab.url);

      // Refresh the badge for the current tab
      await checkAndUpdateIcon(activeTab.id);
    }
  } catch (error) {
    console.error("Failed to clear current page cache:", error);
  }
}

/**
 * Clear all badge cache and refresh badges for all active tabs.
 */
async function clearAllCache() {
  try {
    console.log("Clearing all badge cache");
    await clearBadgeStatus();
  } catch (error) {
    console.error("Failed to clear all cache:", error);
  }
}

getPluginSettings().then(async (settings: Settings) => {
  await checkSettingsState(settings);
});

subscribeToSettingsChanges(async (settings) => {
  await checkSettingsState(settings);
});

// eslint-disable-next-line @typescript-eslint/no-misused-promises -- Manifest V3 allows async functions for all callbacks
chrome.contextMenus.onClicked.addListener(handleContextMenuClick);

/**
 * Handle command events, such as adding a link to karakeep.
 * @param command The command to handle.
 * @param tab The current tab.
 */
function handleCommand(command: string, tab: chrome.tabs.Tab) {
  if (command === ADD_LINK_TO_KARAKEEP_ID) {
    addLinkToKarakeep({
      selectionText: undefined,
      srcUrl: undefined,
      linkUrl: undefined,
      pageUrl: tab?.url,
    });

    // now try to open the popup
    chrome.action.openPopup();
  } else {
    console.warn(`Received unknown command: ${command}`);
  }
}

chrome.commands.onCommand.addListener(handleCommand);

/**
 * Set the badge text and color based on the provided information.
 * @param badgeStatus
 * @param tabId The ID of the tab to update.
 */
export async function setBadge(badgeStatus: string | null, tabId?: number) {
  if (!tabId) return;

  if (badgeStatus) {
    return await Promise.all([
      chrome.action.setBadgeText({ tabId, text: ` ` }),
      chrome.action.setBadgeBackgroundColor({
        tabId,
        color: "#4CAF50",
      }),
    ]);
  } else {
    await chrome.action.setBadgeText({ tabId, text: `` });
  }
}

/**
 * Check and update the badge icon for a given tab ID.
 * @param tabId The ID of the tab to update.
 */
async function checkAndUpdateIcon(tabId: number) {
  const tabInfo = await chrome.tabs.get(tabId);
  const { showCountBadge } = await getPluginSettings();
  const api = await getApiClient();
  if (
    !api ||
    !showCountBadge ||
    !tabInfo.url ||
    !isHttpUrl(tabInfo.url) ||
    tabInfo.status !== "complete"
  ) {
    await chrome.action.setBadgeText({ tabId, text: "" });
    return;
  }
  console.log("Tab activated", tabId, tabInfo);

  try {
    const status = await getBadgeStatus(tabInfo.url);
    await setBadge(status, tabId);
  } catch (error) {
    console.error("Archive check failed:", error);
    await setBadge(null, tabId);
  }
}

chrome.tabs.onActivated.addListener(async (tabActiveInfo) => {
  await checkAndUpdateIcon(tabActiveInfo.tabId);
});

chrome.tabs.onUpdated.addListener(async (tabId) => {
  await checkAndUpdateIcon(tabId);
});

// Listen for REFRESH_BADGE messages from popup and update badge accordingly
chrome.runtime.onMessage.addListener(async (msg) => {
  if (msg && msg.type) {
    if (msg.currentTab && msg.type === MessageType.BOOKMARK_REFRESH_BADGE) {
      console.log(
        "Received REFRESH_BADGE message for tab:",
        msg.currentTab.url,
      );
      if (msg.currentTab.url) {
        await clearBadgeStatus(msg.currentTab.url);
      }
      if (typeof msg.currentTab.id === "number") {
        await checkAndUpdateIcon(msg.currentTab.id);
      }
    }
  }
});