Skip to content

Commit

Permalink
Add entry to context menu (sissbruecker#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
Georges-Antoine Assi committed Jul 28, 2023
1 parent 1968e2d commit abe1a83
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
4 changes: 3 additions & 1 deletion manifests/manifest.COMMON.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
},
"permissions": [
"tabs",
"storage"
"storage",
"contextMenus",
"notifications"
]
}
59 changes: 58 additions & 1 deletion src/background.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getBrowser, getCurrentTabInfo } from "./browser";
import { loadTabMetadata } from "./cache";
import { loadTabMetadata, clearCachedTabMetadata } from "./cache";
import { getConfiguration, isConfigurationComplete } from "./configuration";
import { LinkdingApi } from "./linkding";

Expand Down Expand Up @@ -94,3 +94,60 @@ browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {

await loadTabMetadata(tab.url, true);
});

/* Context menu integration */

async function saveToLinkding(info, tab) {
const isReady = await initApi();
if (!isReady) return;

const urlMetadata = await loadTabMetadata(info.linkUrl);
if (!urlMetadata) return;

if (urlMetadata.bookmark) {
chrome.notifications.create("linkding-bookmark-exists", {
type: "basic",
iconUrl: chrome.runtime.getURL("/icons/logo_48x48.png"),
title: "Linkding",
message: `Bookmark already saved`,
});
return;
}

const { description, title, url } = urlMetadata.metadata;
const tagNames =
api.default_tags
?.split(" ")
.map((tag) => tag.trim())
.filter((tag) => !!tag) ?? [];

const bookmark = {
url,
title: title ?? "",
description: description ?? "",
tag_names: tagNames,
};

try {
await api.saveBookmark(bookmark);
await clearCachedTabMetadata();

chrome.notifications.create("linkding-bookmark-saved", {
type: "basic",
iconUrl: chrome.runtime.getURL("/icons/logo_48x48.png"),
title: "Linkding",
message: `Saved bookmark "${bookmark.title}"`,
silent: true,
});
} catch (e) {
console.error(e);
}
}

chrome.contextMenus.create({
id: "save-to-linkding",
title: "Save to Linkding",
contexts: ["link"],
});

chrome.contextMenus.onClicked.addListener(saveToLinkding);
4 changes: 2 additions & 2 deletions src/form.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script>
import TagAutocomplete from './TagAutocomplete.svelte'
import {getCurrentTabInfo, openOptions} from "./browser";
import {loadTabMetadata, clearCachedTabMetadata} from "./cache";
import { getCurrentTabInfo, openOptions } from "./browser";
import { loadTabMetadata, clearCachedTabMetadata } from "./cache";
export let api;
export let configuration;
Expand Down

0 comments on commit abe1a83

Please sign in to comment.