File size: 1,940 Bytes
a548ebc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Helper function to get a cookie.
// From https://stackoverflow.com/questions/10730362/get-cookie-by-name
function getCookie(name) {
    const value = `; ${document.cookie}`;
    const parts = value.split(`; ${name}=`);
    if (parts.length === 2) return parts.pop().split(';').shift();
}

document.addEventListener('DOMContentLoaded', event => {

    // Always try to get the cookie.
    const cookieValue = getCookie('embeddedCookie');
    if (cookieValue) {
        document.getElementById('cookieValue').innerText = cookieValue;
    }
});


// Check for iOS / Safari.
if (!!document.hasStorageAccess) {
    document.hasStorageAccess().then(result => {

        // If we don't have access we must request it, but the request
        // must come from a UI event.
        if (!result) {

            // Show the button and tie to the click.
            const requestStorageAccessButton =
                  document.getElementById('requestStorageAccessButton');
            requestStorageAccessButton.style.display = "block";
            requestStorageAccessButton.addEventListener("click", event => {

                // On UI event, consume the event by requesting access.
                document.requestStorageAccess().then(result => {

                    // Finally, we are allowed! Reload to get the cookie.
                    window.location.reload();
                }).catch(err => {

                    // If we get here, it means either our page
                    // was never loaded as a first party page,
                    // or the user clicked 'Don't Allow'.
                    // Either way open that now so the user can request
                    // from there (or learn more about us).
                    window.top.location = window.location.href +
                                          "requeststorageaccess.html";
                });
            });
        }
    }).catch(err => console.error(err));
}