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