fix: popup 直接下载,不通过 background
This commit is contained in:
+12
-34
@@ -54,63 +54,42 @@ async function exportPptx() {
|
||||
const captureWidth = normalizeCaptureWidth(captureWidthInput.value, null);
|
||||
|
||||
try {
|
||||
// 1. 触发采集(background 会缓存数据)
|
||||
// 1. 采集
|
||||
setProgress(15, '正在采集页面...');
|
||||
const captureResult = await chrome.runtime.sendMessage({
|
||||
type: 'WEB_TO_PPT_CAPTURE_START',
|
||||
options: { captureWidth }
|
||||
});
|
||||
if (!captureResult || !captureResult.ok) throw new Error(captureResult?.error || '采集失败');
|
||||
|
||||
if (!captureResult || !captureResult.ok) {
|
||||
throw new Error(captureResult?.error || '采集失败');
|
||||
}
|
||||
|
||||
// 2. 获取缓存数据
|
||||
// 2. 获取数据
|
||||
setProgress(40, '采集完成,获取数据...');
|
||||
const dataResult = await chrome.runtime.sendMessage({
|
||||
type: 'WEB_TO_PPT_GET_DATA'
|
||||
});
|
||||
|
||||
if (!dataResult || !dataResult.ok) {
|
||||
throw new Error(dataResult?.error || '获取数据失败');
|
||||
}
|
||||
|
||||
const jsonData = dataResult.data;
|
||||
const dataResult = await chrome.runtime.sendMessage({ type: 'WEB_TO_PPT_GET_DATA' });
|
||||
if (!dataResult || !dataResult.ok) throw new Error(dataResult?.error || '获取数据失败');
|
||||
|
||||
// 3. 转化
|
||||
setProgress(50, '正在生成 PPTX...');
|
||||
const schema = await WebToPPT.convertToPptx(jsonData, (msg) => {
|
||||
setProgress(60, msg);
|
||||
});
|
||||
const schema = await WebToPPT.convertToPptx(dataResult.data, (msg) => setProgress(60, msg));
|
||||
|
||||
// 4. 生成 PPTX blob
|
||||
// 4. 生成 PPTX
|
||||
setProgress(85, '正在打包...');
|
||||
const blob = await WebToPPT.schemaToPptxBlob(schema);
|
||||
|
||||
// 5. 转 base64
|
||||
// 5. 下载(data URL,popup 直接调用)
|
||||
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 base64 = btoa(binary);
|
||||
const dataUrl = 'data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64,' + btoa(binary);
|
||||
|
||||
// 6. 让 background 下载
|
||||
setProgress(95, '正在下载...');
|
||||
const title = jsonData.source?.title || 'webpage';
|
||||
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`;
|
||||
|
||||
const dlResult = await chrome.runtime.sendMessage({
|
||||
type: 'WEB_TO_PPT_DOWNLOAD',
|
||||
base64,
|
||||
filename
|
||||
});
|
||||
|
||||
if (!dlResult || !dlResult.ok) {
|
||||
throw new Error(dlResult?.error || '下载失败');
|
||||
}
|
||||
await chrome.downloads.download({ url: dataUrl, filename });
|
||||
|
||||
setProgress(100, '导出完成');
|
||||
setResult('success', '导出完成');
|
||||
@@ -126,7 +105,6 @@ async function exportPptx() {
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化
|
||||
document.addEventListener('DOMContentLoaded', async () => {
|
||||
const currentWidth = await getCurrentViewportWidth();
|
||||
captureWidthInput.value = String(currentWidth || '');
|
||||
|
||||
Reference in New Issue
Block a user