| 2 | |
| 3 | // 处理截图请求 |
| 4 | const handler: PlasmoMessaging.MessageHandler = async (req, res) => { |
| 5 | const { action, text, autoSend, images, prompt } = req.body |
| 6 | |
| 7 | try { |
| 8 | if (action === "captureVisibleTab") { |
| 9 | // 截取当前可见标签页 |
| 10 | const dataUrl = await chrome.tabs.captureVisibleTab(null, { |
| 11 | format: "png" |
| 12 | }) |
| 13 | res.send({ success: true, dataUrl }) |
| 14 | return |
| 15 | } |
| 16 | |
| 17 | if (action === "sendToActiveTab") { |
| 18 | // 发送消息到当前活动标签页 |
| 19 | const [tab] = await chrome.tabs.query({ |
| 20 | active: true, |
| 21 | currentWindow: true |
| 22 | }) |
| 23 | |
| 24 | if (tab.id) { |
| 25 | await chrome.tabs.sendMessage(tab.id, { |
| 26 | action: "sendToChat", |
| 27 | text, |
| 28 | autoSend |
| 29 | }) |
| 30 | res.send({ success: true }) |
| 31 | } else { |
| 32 | res.send({ success: false, error: "No active tab found" }) |
| 33 | } |
| 34 | return |
| 35 | } |
| 36 | |
| 37 | if (action === "sendImageToActiveTab") { |
| 38 | // 发送图片到当前活动标签页 |
| 39 | const [tab] = await chrome.tabs.query({ |
| 40 | active: true, |
| 41 | currentWindow: true |
| 42 | }) |
| 43 | |
| 44 | if (tab.id) { |
| 45 | await chrome.tabs.sendMessage(tab.id, { |
| 46 | action: "sendImageToChat", |
| 47 | images |
| 48 | }) |
| 49 | res.send({ success: true }) |
| 50 | } else { |
| 51 | res.send({ success: false, error: "No active tab found" }) |
| 52 | } |
| 53 | return |
| 54 | } |
| 55 | |
| 56 | res.send({ success: false, error: "Unknown action" }) |
| 57 | } catch (error) { |
| 58 | console.error("[Chat Handler] Error:", error) |
| 59 | res.send({ success: false, error: error.message }) |
| 60 | } |
| 61 | } |