如何在Chrome中以编程方式撤销持久化存储权限?
How to Revoke Persistent Storage Permission Programmatically in Chrome
Hey there! Great question about reversing persistent storage permissions in Chrome. Let's walk through this step by step.
First, just a quick recap of how you enable persistent storage (since you already shared that code snippet):
if (navigator.storage && navigator.storage.persist) navigator.storage.persist() .then(granted => { if (granted) console.log("Storage will not be cleared except by explicit user action"); else console.log("Storage may be cleared by the UA under storage pressure."); });
To revoke that persistent storage permission programmatically, you can use the navigator.storage.unpersist() method — it's the direct built-in counterpart to persist(). Here's a working implementation:
if (navigator.storage && navigator.storage.unpersist) navigator.storage.unpersist() .then(revoked => { if (revoked) { console.log("Success! Persistent storage permission has been revoked. The browser can now clear this storage when under space pressure."); } else { console.log("Persistent storage wasn't active for this origin, so no changes were made."); } });
Key Details to Remember:
- The
unpersist()method returns a promise that resolves to a boolean:truemeans the permission was successfully revoked,falseindicates persistent storage wasn't enabled in the first place. - This API only works in secure contexts (HTTPS websites, or
localhostfor local development) — just like thepersist()method. - Revoking persistent storage does not delete any existing stored data; it simply removes the protection against automatic cleanup when the browser is low on storage space.
内容的提问来源于stack exchange,提问作者Lutz
相关产品推荐
相关产品推荐

