概述权限提示应在用户发起的交互中进行。本文提供检测与提示封装。检测与提示async function ensureRW(handle) {
if (typeof handle.queryPermission === 'function') {
const s = await handle.queryPermission({ mode: 'readwrite' });
if (s === 'granted') return true;
}
if (typeof handle.requestPermission === 'function') {
const s2 = await handle.requestPermission({ mode: 'readwrite' });
return s2 === 'granted';
}
return true;
}
async function onClickSave(fn) {
const handle = await window.showSaveFilePicker({ suggestedName: 'data.txt' });
const ok = await ensureRW(handle);
if (!ok) { alert('请授权写入权限'); return; }
const ws = await handle.createWritable();
await ws.write(new Blob([await fn()], { type: 'text/plain' }));
await ws.close();
}

发表评论 取消回复