MCPcopy Create free account
hub / github.com/027xiguapi/code-box / handler

Function handler

background/messages/download.ts:5–50  ·  view source on GitHub ↗
(req, res)

Source from the content-addressed store, hash-verified

3import type { PlasmoMessaging } from "@plasmohq/messaging"
4
5const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
6 const { imageUrls, title, action, onProgress } = req.body
7 if (action === "downloadAllImages") {
8 const zip = new JSZip()
9
10 const fetchImage = (url) => {
11 return fetch(url)
12 .then((response) => response.blob())
13 .then((blob) => {
14 const fileName = url.split("/").pop().split("?")[0]
15 return { blob, fileName }
16 })
17 }
18
19 Promise.all(imageUrls.map(fetchImage))
20 .then((results) => {
21 results.forEach(({ blob, fileName }, index) => {
22 onProgress && onProgress(index + 1, imageUrls.length)
23 zip.file(fileName || `image-${index}.jpg`, blob)
24 })
25
26 return zip.generateAsync({ type: "blob" })
27 })
28 .then((zipBlob) => {
29 const reader = new FileReader()
30 reader.onload = function (event) {
31 const dataUrl = event.target.result as string
32
33 chrome.downloads.download(
34 {
35 url: dataUrl,
36 filename: `${title}.zip`
37 },
38 () => {
39 res.send({ code: 200, msg: "success" })
40 }
41 )
42 }
43 reader.readAsDataURL(zipBlob)
44 })
45 .catch((err) => {
46 console.error(err)
47 res.send({ code: 0, msg: err })
48 })
49 }
50}
51
52export default handler

Callers

nothing calls this directly

Calls 1

sendMethod · 0.80

Tested by

no test coverage detected