mirror of
https://github.com/027xiguapi/code-box.git
synced 2026-09-18 18:51:41 +00:00
微信图片加载bug
This commit is contained in:
+66
-61
@@ -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) => {
|
||||
|
||||
+31
-1
@@ -268,7 +268,36 @@ const PlasmoOverlay: FC<PlasmoCSUIProps> = ({ anchor }) => {
|
||||
setContent(dom, articleTitle)
|
||||
}
|
||||
|
||||
async function scrollToLoadImages() {
|
||||
const article = document.querySelector("#js_content")
|
||||
if (!article) return
|
||||
|
||||
// 平滑滚动到底部
|
||||
return new Promise<void>((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<PlasmoCSUIProps> = ({ 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 }[] = []
|
||||
|
||||
+2
-1
@@ -62,7 +62,8 @@
|
||||
},
|
||||
"permissions": [
|
||||
"activeTab",
|
||||
"downloads"
|
||||
"downloads",
|
||||
"contextMenus"
|
||||
],
|
||||
"host_permissions": [
|
||||
"https://*/*"
|
||||
|
||||
Reference in New Issue
Block a user