fix: 用 chrome.storage.session 传 base64,background 下载
This commit is contained in:
@@ -171,11 +171,14 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 下载 PPTX(base64 data URL)
|
||||
// 下载 PPTX(从 storage 读取 base64)
|
||||
if (message?.type === "WEB_TO_PPT_DOWNLOAD") {
|
||||
(async () => {
|
||||
const dataUrl = 'data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64,' + message.base64;
|
||||
await chrome.downloads.download({ url: dataUrl, filename: message.filename });
|
||||
const stored = await chrome.storage.session.get(['pptxBase64', 'pptxFilename']);
|
||||
if (!stored.pptxBase64) throw new Error('没有待下载的数据');
|
||||
const dataUrl = 'data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64,' + stored.pptxBase64;
|
||||
await chrome.downloads.download({ url: dataUrl, filename: stored.pptxFilename });
|
||||
await chrome.storage.session.remove(['pptxBase64', 'pptxFilename']);
|
||||
return { ok: true };
|
||||
})()
|
||||
.then(sendResponse)
|
||||
|
||||
+7
-10
@@ -75,27 +75,24 @@ async function exportPptx() {
|
||||
setProgress(85, '正在打包...');
|
||||
const blob = await WebToPPT.schemaToPptxBlob(schema);
|
||||
|
||||
// 5. 下载(data URL,popup 直接调用)
|
||||
setProgress(95, '正在下载...');
|
||||
// 5. 转 base64
|
||||
const buffer = await blob.arrayBuffer();
|
||||
const bytes = new Uint8Array(buffer);
|
||||
let binary = '';
|
||||
for (let i = 0; i < bytes.length; i += 0x8000) {
|
||||
binary += String.fromCharCode(...bytes.subarray(i, i + 0x8000));
|
||||
}
|
||||
const dataUrl = 'data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64,' + btoa(binary);
|
||||
const base64 = btoa(binary);
|
||||
|
||||
// 6. 存到 storage,让 background 下载
|
||||
setProgress(95, '正在下载...');
|
||||
const title = dataResult.data.source?.title || 'webpage';
|
||||
const safeTitle = title.replace(/[\\/:*?"<>|]+/g, '-').replace(/\s+/g, '-').slice(0, 64) || 'webpage';
|
||||
const filename = `web-to-ppt/${safeTitle}-${Date.now()}.pptx`;
|
||||
|
||||
try {
|
||||
const dlId = await chrome.downloads.download({ url: dataUrl, filename });
|
||||
console.log('[WebToPPT] download id:', dlId);
|
||||
} catch (e) {
|
||||
console.error('[WebToPPT] download error:', e.message, e);
|
||||
throw e;
|
||||
}
|
||||
await chrome.storage.session.set({ pptxBase64: base64, pptxFilename: filename });
|
||||
const dlResult = await chrome.runtime.sendMessage({ type: 'WEB_TO_PPT_DOWNLOAD' });
|
||||
if (!dlResult || !dlResult.ok) throw new Error(dlResult?.error || '下载失败');
|
||||
|
||||
setProgress(100, '导出完成');
|
||||
setResult('success', '导出完成');
|
||||
|
||||
Reference in New Issue
Block a user