mirror of
https://github.com/linkwarden/browser-extension.git
synced 2026-06-23 04:10:26 +00:00
Merge pull request #123 from QazCetelic/feat/show-already-added-icon
Show if link is already added V2
This commit is contained in:
@@ -2,6 +2,7 @@ import captureScreenshot from '../screenshot.ts';
|
||||
import { bookmarkFormValues } from '../validators/bookmarkForm.ts';
|
||||
import axios from 'axios';
|
||||
import { bookmarkMetadata } from '../cache.ts';
|
||||
import { getCurrentTabInfo } from '../utils.ts';
|
||||
|
||||
export async function postLink(
|
||||
baseUrl: string,
|
||||
@@ -111,3 +112,30 @@ export async function getLinksFetch(
|
||||
});
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
export async function checkLinkExists(
|
||||
baseUrl: string,
|
||||
apiKey: string
|
||||
): Promise<boolean> {
|
||||
const tabInfo = await getCurrentTabInfo();
|
||||
if (!tabInfo.url) {
|
||||
console.error('No URL found for current tab');
|
||||
return false;
|
||||
}
|
||||
|
||||
const url =
|
||||
`${baseUrl}/api/v1/search?cursor=0&sort=0&searchQueryString=` +
|
||||
encodeURIComponent(`url:${tabInfo.url}`);
|
||||
|
||||
const response = await fetch(url, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${apiKey}`,
|
||||
},
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
const exists = data.data.links.length > 0;
|
||||
|
||||
return exists;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { getBrowser, getCurrentTabInfo } from '../../@/lib/utils.ts';
|
||||
// import BookmarkTreeNode = chrome.bookmarks.BookmarkTreeNode;
|
||||
import { getConfig, isConfigured } from '../../@/lib/config.ts';
|
||||
import {
|
||||
checkLinkExists,
|
||||
// deleteLinkFetch,
|
||||
// updateLinkFetch,
|
||||
postLinkFetch,
|
||||
@@ -297,6 +298,32 @@ browser.runtime.onInstalled.addListener(function () {
|
||||
});
|
||||
});
|
||||
|
||||
browser.tabs.onActivated.addListener(async ({ tabId }) => {
|
||||
const cachedConfig = await getConfig();
|
||||
const linkExists = await checkLinkExists(
|
||||
cachedConfig.baseUrl,
|
||||
cachedConfig.apiKey
|
||||
);
|
||||
if (linkExists) {
|
||||
if (browser.action) {
|
||||
browser.action.setBadgeText({ tabId, text: '✓' });
|
||||
browser.action.setBadgeBackgroundColor({ tabId, color: '#4688F1' });
|
||||
} else {
|
||||
browser.browserAction.setBadgeText({ tabId, text: '✓' });
|
||||
browser.browserAction.setBadgeBackgroundColor({
|
||||
tabId,
|
||||
color: '#4688F1',
|
||||
});
|
||||
}
|
||||
} else {
|
||||
if (browser.action) {
|
||||
browser.action.setBadgeText({ tabId, text: '' });
|
||||
} else {
|
||||
browser.browserAction.setBadgeText({ tabId, text: '' });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Omnibox implementation
|
||||
|
||||
browser.omnibox.onInputStarted.addListener(async () => {
|
||||
|
||||
Reference in New Issue
Block a user