概述权限提示应在用户发起的交互中进行。本文提供检测与提示封装。检测与提示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();

}

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部