我的扩展创建了压缩存档,里面有图像。我用chrome.downloads.download把我的档案下载到设备上。
const url = URL.createObjectURL(archiveBlob);
chrome.downloads.download({
url,
filename: `${archiveData.fileName}.zip`,
saveAs: false
});同时,当我将URL.createObjectURL转换为图像时,在扩展中也需要ArrayBuffer
async bufferToImage(buffer: ArrayBuffer): Promise<InstanceType<typeof Image>> {
return new Promise((res, rej) => {
const blob = new Blob([buffer]);
const objectURL = URL.createObjectURL(blob);
const img = new Image();
img.src = objectURL;
img.onload = () => res(img);
});
}我现在怎样才能在Manifest v3 ServiceWorker做到这一点呢?
发布于 2022-07-18 10:18:21
您可以将Blob转换为base64。
如果您想要导出图像:
/**
* capture and download
* 截图并下载
*/
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
chrome.tabs.query({ active: true }, async (tabs) => {
const tab = tabs[0];
const image = await chrome.tabs.captureVisibleTab(tab.windowId, {
format: "png",
});
chrome.downloads.download({
url: image,
filename: "1.png",
})
});
});如果你想要出口拉链:
import JSZip from "jszip";
/**
* example: captureVisibleTab and export zip
* 案例:截图并导出zip
*/
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
chrome.tabs.query({ active: true }, async (tabs) => {
const tab = tabs[0];
// capture
const image = await chrome.tabs.captureVisibleTab(tab.windowId, {
format: "png",
});
// image base64 => blob
const blob = await fetch(image).then((response) => response.blob());
// zip image
const zip = new JSZip();
zip.file("insta-snap1.png", blob, { binary: true });
const zipData = await zip.generateAsync({
type: "base64",
});
// download zip with base64
chrome.downloads.download({
url: `data:application/octet-stream;base64,${zipData}`,
filename: "test.zip",
});
});
});https://stackoverflow.com/questions/72474057
复制相似问题