这份只讲页面内的那一段 JS —— 它和驱动方式无关:Electron 用 executeJavaScript 执行,CDP 用 Runtime.evaluate 执行,内容一样。
字节从哪来:由宿主传入。Electron 里是主进程 fs.readFileSync(...).toString('base64') 之后拼进脚本;页面自己的接口返回 base64 也走同一条路。不要为拿字节起本地 HTTP 服务,也不要让用户装任何运行时。
平台常见四层嵌套,顶层脚本只能看到第一层:
顶层页面(cloud / portal)
└─ iframe #1 应用壳(liyuan 之类)
└─ iframe #2 编辑器宿主(URL 里带 pptEasy / mode=editor3)
└─ iframe #3 真正的编辑器 document ← 上传控件在这里
逐层找可见的那一个(getBoundingClientRect().width > 0),宽为 0 的是预加载的隐藏副本,投进去不会有反应:
[...root.querySelectorAll('iframe')].filter(f => f.getBoundingClientRect().width > 0)
回读确认方法:把目标层的 document.body.innerText 取回来,能看到「上传文件」「幻灯片 1 / 1」就对了。正文读出来是空的属正常(内容渲染在 canvas 或异步组件里),不要据此判断"没打开"。
// w 必须是目标 frame 的 window,d 是它的 document
const bytes = Uint8Array.from(atob(B64), c => c.charCodeAt(0));
const file = new w.File([bytes], name, { type: mime }); // ← w.File,不是顶层 File
const dt = new w.DataTransfer(); // ← w.DataTransfer
input.dispatchEvent(new w.Event('change', { bubbles: true })); // ← w.Event
混用构造器的表现:不报错,input.files 看起来有值,但页面的 change handler 拿到的是空 FileList,于是"点了没反应"。这是最常见的失败原因,先查这一条。
(() => {
const vis = (el) => !!(el.offsetWidth || el.offsetHeight || el.getClientRects().length);
const inputs = [...document.querySelectorAll('input[type=file]')];
const ppt = inputs.filter((el) => /pptx|ppt|pdf/i.test(el.accept || ''));
const zone = document.querySelector('.file-input') ||
(ppt[0] && ppt[0].closest('[class*=upload],[class*=drop],[class*=drag]')) || null;
const texts = [];
for (const el of document.querySelectorAll('button,[role=button],a,div,span,h3')) {
if (el.childElementCount) continue;
const t = (el.textContent || '').trim();
if (t && t.length <= 20 && vis(el)) texts.push(t);
}
return {
url: location.href,
inputs: inputs.length,
accepts: inputs.map(i => i.accept || ''),
dropzone: !!zone,
hasUploadFn: typeof window.handleFileUpload,
texts: texts.slice(0, 60)
};
})()
accepts 决定投哪个(按 accept 认,不按下标认);texts 决定找不到控件时点什么;hasUploadFn 决定要不要改用页面自己的上传函数。
(async () => {
const bin = atob(B64); // B64 由宿主拼进来
const bytes = new Uint8Array(bin.length);
for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
const inputs = [...document.querySelectorAll('input[type=file]')];
const ppt = inputs.filter((el) => /pptx|ppt|pdf/i.test(el.accept || ''));
const input = ppt[0] || inputs[0] || null;
const zone = (input && input.closest('.file-input')) || document.querySelector('.file-input') || null;
if (!input && !zone) return { ok: false, reason: 'no-target', url: location.href };
const dt = new DataTransfer();
dt.items.add(new File([bytes], NAME, { type: MIME }));
if (input) {
try {
input.files = dt.files;
input.dispatchEvent(new Event('change', { bubbles: true }));
return { ok: true, via: 'change', url: location.href, accept: input.accept || '' };
} catch { /* 禁止赋值 files 的实现,退到 drop */ }
}
if (zone) {
for (const type of ['dragenter', 'dragover', 'drop']) {
zone.dispatchEvent(new DragEvent(type, { bubbles: true, cancelable: true, dataTransfer: dt }));
}
return { ok: true, via: 'drop', url: location.href };
}
return { ok: false, reason: 'no-target', url: location.href };
})()
注意这个 IIFE 里用的是当前 frame 自己的 File / DataTransfer / Event —— 因为脚本本身就是在这个 frame 里执行的,所以天然同帧。不要从顶层把构造器当参数传进来。
if (typeof w.handleFileUpload === 'function') {
w.handleFileUpload([file]); // 只调一次
}
实测差异:新版编辑器 ppt.cocorobo.cn/?mode=editor3 没有这个函数(只有 getPPTSlide / setPPTSlide / pptLoading),beta.ppt. 那套有。所以判断 typeof 后要能分流,不能因为读不到就直接让老师手传。
3 秒一次、最多 60 秒,看三样:页数(幻灯片 N / N 或 getPPTSlide())、标题(通常变成文件名)、"上次保存时间"。
accept 相同的 input 再试一次;仍不动 → 换方案或请老师手选。页面跑的是缓存里的旧包时,表现是"该有的函数读不到"。比一下正在跑的包和服务器现在发的包:
const loaded = [...document.querySelectorAll('script[src]')].map(s => s.src.split('/').pop());
const html = await (await fetch(location.href.split('?')[0] + '?mode=editor3&_t=' + Date.now(), { cache: 'reload' })).text();
const fresh = [...html.matchAll(/src="([^"]+\.js[^"]*)"/g)].map(m => m[1].split('/').pop());
// loaded 与 fresh 不一致 → 在跑旧包,给 iframe.src 加时间戳重载那一层
注意:重载会把编辑器重置成空白新建页(courseid= 为空、1 页),必须在老师填课程信息、上传文件之前做。已传过内容就不要重载。