fix: 下载移到 background.js 执行,和原插件一致
This commit is contained in:
+12
-6
@@ -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', '导出完成');
|
||||
|
||||
Reference in New Issue
Block a user