fix promise

This commit is contained in:
SrGooglo 2023-03-06 02:16:08 +00:00
parent 15a6126feb
commit 9a928e8a7f

View File

@ -3,56 +3,58 @@ export default async (eventFile) => {
throw new Error("Missing eventFile") throw new Error("Missing eventFile")
} }
if (eventFile.type === "text/html") { return await new Promise((resolve, reject) => {
eventFile.getAsString((data) => { if (eventFile.type === "text/html") {
const parser = new DOMParser() eventFile.getAsString((data) => {
const doc = parser.parseFromString(data, "text/html") const parser = new DOMParser()
const img = doc.querySelector("img") const doc = parser.parseFromString(data, "text/html")
const img = doc.querySelector("img")
// TODO: Support multiple mime types // TODO: Support multiple mime types
if (!img) { if (!img) {
return reject(new Error("No image found in clipboard. Only images are supported.")) return reject(new Error("No image found in clipboard. Only images are supported."))
} }
const image = new Image() const image = new Image()
const finalExtension = "png" //img.src.split(".").pop() const finalExtension = "png" //img.src.split(".").pop()
image.crossOrigin = "Anonymous" image.crossOrigin = "Anonymous"
image.src = img.src image.src = img.src
image.onload = () => { image.onload = () => {
const canvas = document.createElement("canvas") const canvas = document.createElement("canvas")
canvas.width = image.width canvas.width = image.width
canvas.height = image.height canvas.height = image.height
const context = canvas.getContext("2d") const context = canvas.getContext("2d")
context.drawImage(image, 0, 0, image.width, image.height) context.drawImage(image, 0, 0, image.width, image.height)
canvas.toBlob((blob) => { canvas.toBlob((blob) => {
blob.lastModifiedDate = new Date() blob.lastModifiedDate = new Date()
blob.name = img.src.split("/").pop() blob.name = img.src.split("/").pop()
// remove the extension // remove the extension
blob.name = blob.name.split(".").slice(0, -1).join(".") blob.name = blob.name.split(".").slice(0, -1).join(".")
// set in the name the extension // set in the name the extension
blob.name = `${blob.name}.${finalExtension}` blob.name = `${blob.name}.${finalExtension}`
blob.filename = blob.name blob.filename = blob.name
return resolve(new File([blob], blob.name, { return resolve(new File([blob], blob.name, {
type: blob.type, type: blob.type,
lastModified: blob.lastModifiedDate lastModified: blob.lastModifiedDate
})) }))
}, `image/${finalExtension}`) }, `image/${finalExtension}`)
} }
}) })
} else { } else {
return eventFile.getAsFile() return resolve(eventFile.getAsFile())
} }
})
} }