fix: 下载移到 background.js 执行,和原插件一致

This commit is contained in:
李进
2026-07-27 14:43:29 +08:00
parent 1df8a826c4
commit 3f7c9648d2
2 changed files with 24 additions and 6 deletions
+12
View File
@@ -259,6 +259,18 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
return true;
}
// 下载 PPTX(由 popup 调用,background 执行下载)
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 });
return { ok: true };
})()
.then(sendResponse)
.catch(error => sendResponse({ ok: false, error: error.message || String(error) }));
return true;
}
return false;
});
+12 -6
View File
@@ -101,25 +101,31 @@ async function exportPptx() {
setProgress(50 + Math.min(40, Math.floor(Math.random() * 40)), msg);
});
// 4. 生成 PPTX
// 4. 生成 PPTX 并通过 background 下载
setProgress(90, '正在打包...');
const blob = await WebToPPT.schemaToPptxBlob(schema);
// 5. 下载(用 data URL,和原插件一致)
setProgress(95, '正在下载...');
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);
// 5. 让 background 执行下载
setProgress(95, '正在下载...');
const title = jsonData.source?.title || 'webpage';
const safeTitle = title.replace(/[\\/:*?"<>|]+/g, '-').replace(/\s+/g, '-').slice(0, 64) || 'webpage';
const filename = `web-to-ppt/${safeTitle}-${Date.now()}.pptx`;
await chrome.downloads.download({ url: dataUrl, filename });
const dlResult = await chrome.runtime.sendMessage({
type: 'WEB_TO_PPT_DOWNLOAD',
base64,
filename
});
if (!dlResult || !dlResult.ok) {
throw new Error(dlResult?.error || '下载失败');
}
setProgress(100, `已导出:${filename}`);
setResult('success', '导出完成');