Does Safari's "Storage Access API" only work with iframes?

855 Views Asked by At

I am trying to use the Storage Access API in plain old JavaScript inside of a modal. I am not using an iframe. All of the docs that I see online for the Storage Access API reference iframes. Does this mean the technology only works with iframes or can I use it in a regular javascript file?

I've tried attaching it to an onclick event in html and also creating it programatically with javascript, but neither of these seem to be working. I cannot get the "Do you want to allow 'video.example' to use cookies and website data while browsing 'news.example.com'" to show.

<button onlick="showSafariMessage()"  type="button">Show Safari Message</div>

<script>
    var showSafariMessage = function () {
        document.hasStorageAccess().then(hasAccess => {
            if (!hasAccess) {
                return document.requestStorageAccess();
            }
        }).then((result) => {
            // Now we have first-party storage access!

            // Let's access some items from the first-party cookie jar
            document.cookie = "foo=bar"; // set a cookie
            localStorage.setItem("username", "John"); // access a localStorage entry
        }).catch((error) => {
            // error obtaining storage access.
        });
    }
</script>

I expect to see the Safari popup, but I am not. Please help!

0

There are 0 best solutions below