diff --git a/background/index.ts b/background/index.ts index ea08b6c..94e12c6 100644 --- a/background/index.ts +++ b/background/index.ts @@ -14,6 +14,11 @@ chrome.runtime.onInstalled.addListener(function (object) { // 创建右键菜单 function createContextMenus() { + // 先移除所有已存在的菜单项 + chrome.contextMenus.removeAll(() => { + + }) + // 翻译选中文本 chrome.contextMenus.create({ id: "translateText", @@ -51,72 +56,72 @@ function createContextMenus() { } // 监听右键菜单点击 -chrome.contextMenus.onClicked.addListener((info, tab) => { - if (!tab?.id) return +// chrome.contextMenus.onClicked.addListener((info, tab) => { +// if (!tab?.id) return - switch (info.menuItemId) { - case "translateText": - chrome.tabs.sendMessage(tab.id, { - action: "sendToChat", - text: `请将以下文本翻译成中文:\n\n${info.selectionText}`, - autoSend: true - }) - break +// switch (info.menuItemId) { +// case "translateText": +// chrome.tabs.sendMessage(tab.id, { +// action: "sendToChat", +// text: `请将以下文本翻译成中文:\n\n${info.selectionText}`, +// autoSend: true +// }) +// break - case "explainCode": - chrome.tabs.sendMessage(tab.id, { - action: "sendToChat", - text: `请解释以下代码:\n\n\`\`\`\n${info.selectionText}\n\`\`\``, - autoSend: true - }) - break +// case "explainCode": +// chrome.tabs.sendMessage(tab.id, { +// action: "sendToChat", +// text: `请解释以下代码:\n\n\`\`\`\n${info.selectionText}\n\`\`\``, +// autoSend: true +// }) +// break - case "summarizeText": - chrome.tabs.sendMessage(tab.id, { - action: "sendToChat", - text: `请帮我总结以下内容:\n\n${info.selectionText}`, - autoSend: true - }) - break +// case "summarizeText": +// chrome.tabs.sendMessage(tab.id, { +// action: "sendToChat", +// text: `请帮我总结以下内容:\n\n${info.selectionText}`, +// autoSend: true +// }) +// break - case "captureAndAnalyze": - chrome.tabs.captureVisibleTab(null, { format: "png" }, (dataUrl) => { - chrome.tabs.sendMessage(tab.id, { - action: "sendImageToChat", - images: [dataUrl] - }) - setTimeout(() => { - chrome.tabs.sendMessage(tab.id, { - action: "sendToChat", - text: "请分析这张图片的内容", - autoSend: false - }) - }, 100) - }) - break +// case "captureAndAnalyze": +// chrome.tabs.captureVisibleTab(null, { format: "png" }, (dataUrl) => { +// chrome.tabs.sendMessage(tab.id, { +// action: "sendImageToChat", +// images: [dataUrl] +// }) +// setTimeout(() => { +// chrome.tabs.sendMessage(tab.id, { +// action: "sendToChat", +// text: "请分析这张图片的内容", +// autoSend: false +// }) +// }, 100) +// }) +// break - case "sendImageToChat": - if (info.srcUrl) { - // 下载图片并转换为 base64 - fetch(info.srcUrl) - .then((response) => response.blob()) - .then((blob) => { - const reader = new FileReader() - reader.onloadend = () => { - chrome.tabs.sendMessage(tab.id, { - action: "sendImageToChat", - images: [reader.result as string] - }) - } - reader.readAsDataURL(blob) - }) - .catch((error) => { - console.error("Failed to download image:", error) - }) - } - break - } -}) +// case "sendImageToChat": +// if (info.srcUrl) { +// // 下载图片并转换为 base64 +// fetch(info.srcUrl) +// .then((response) => response.blob()) +// .then((blob) => { +// const reader = new FileReader() +// reader.onloadend = () => { +// chrome.tabs.sendMessage(tab.id, { +// action: "sendImageToChat", +// images: [reader.result as string] +// }) +// } +// reader.readAsDataURL(blob) +// }) +// .catch((error) => { +// console.error("Failed to download image:", error) +// }) +// } +// break +// } +// }) // 监听来自 content script 的消息 chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { diff --git a/contents/weixin.tsx b/contents/weixin.tsx index eae4c19..6cd570b 100644 --- a/contents/weixin.tsx +++ b/contents/weixin.tsx @@ -268,7 +268,36 @@ const PlasmoOverlay: FC = ({ anchor }) => { setContent(dom, articleTitle) } + async function scrollToLoadImages() { + const article = document.querySelector("#js_content") + if (!article) return + + // 平滑滚动到底部 + return new Promise((resolve) => { + const scrollHeight = document.documentElement.scrollHeight + const clientHeight = document.documentElement.clientHeight + const scrollStep = 300 // 每次滚动的距离 + let currentScroll = 0 + + const scrollInterval = setInterval(() => { + currentScroll += scrollStep + window.scrollTo(0, currentScroll) + + if (currentScroll >= scrollHeight - clientHeight) { + clearInterval(scrollInterval) + // 等待图片加载 + setTimeout(() => { + window.scrollTo(0, 0) // 滚回顶部 + resolve() + }, 500) + } + }, 100) + }) + } + async function downloadMarkdown() { + await scrollToLoadImages() + const html = document.querySelector("#img-content") const markdown = turndownService.turndown(html) await saveMarkdown(markdown, articleTitle) @@ -277,10 +306,11 @@ const PlasmoOverlay: FC = ({ anchor }) => { async function downloadMarkdownWithImages( onProgress?: (current: number, total: number) => void ) { + await scrollToLoadImages() + const html = document.querySelector("#img-content") if (!html) return - // 先生成 markdown,再从中提取实际图片 URL,确保 URL 完全一致 let markdown = turndownService.turndown(html) const mdImageRegex = /!\[([^\]]*)\]\(([^)]+)\)/g const imageEntries: { alt: string; url: string }[] = [] diff --git a/package.json b/package.json index ef2e50f..570529f 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,8 @@ }, "permissions": [ "activeTab", - "downloads" + "downloads", + "contextMenus" ], "host_permissions": [ "https://*/*"