diff --git a/background/bindContextMenus.ts b/background/bindContextMenus.ts new file mode 100644 index 0000000..3b9ee33 --- /dev/null +++ b/background/bindContextMenus.ts @@ -0,0 +1,35 @@ +const initContextMenus = async () => { + chrome.contextMenus.onClicked.addListener(async (info) => { + switch (info.menuItemId) { + case "copy_markdown_links": + case "copy_markdown_links_full": + let tab: chrome.tabs.Tab; + + [tab] = await chrome.tabs.query({ active: true, lastFocusedWindow: true }); + + if (tab && !tab.url.includes("chrome")) { + const messageType = info.menuItemId === "copy_markdown_links" + ? "toolbox:copyLinkToMarkdown" + : "toolbox:copyLinkToMarkdownFull"; + + return await chrome.tabs.sendMessage(tab.id, { type: messageType }); + } + + break; + } + }); + + chrome.contextMenus.create({ + title: "复制链接为 Markdown", + contexts: ["page"], + id: "copy_markdown_links", + }); + + chrome.contextMenus.create({ + title: "复制完整链接为 Markdown", + contexts: ["page"], + id: "copy_markdown_links_full", + }); +}; + +export default initContextMenus; diff --git a/background/index.ts b/background/index.ts index cb0ff5c..16a15b3 100644 --- a/background/index.ts +++ b/background/index.ts @@ -1 +1,5 @@ +import initContextMenus from "./bindContextMenus"; + +initContextMenus(); + export {}; diff --git a/contents/copy.ts b/contents/copy.ts new file mode 100644 index 0000000..7f7b7e3 --- /dev/null +++ b/contents/copy.ts @@ -0,0 +1,13 @@ +chrome.runtime.onMessage.addListener((req) => { + // 复制精简链接 + if (req.type === "toolbox:copyLinkToMarkdown") { + const link = `${location.origin}${location.pathname}`; + + navigator.clipboard.writeText(`[${document.title}](${link})`); + } + + // 复制完整链接带参数 + if (req.type === "toolbox:copyLinkToMarkdownFull") { + navigator.clipboard.writeText(`[${document.title}](${location.href})`); + } +}); diff --git a/package.json b/package.json index d9cea45..c63115f 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ }, "manifest": { "permissions": [ - "tabs", "storage", "scripting" + "tabs", "storage", "scripting", "contextMenus" ], "host_permissions": [ "https://*/*"