|
76 | 76 | </main>
|
77 | 77 |
|
78 | 78 | <script type="text/javascript">
|
79 |
| - function handleStorageAccess(e) { |
80 |
| - document.requestStorageAccess().then( |
81 |
| - function () { |
82 |
| - try { |
83 |
| - // Attempt to set storage and same-site cookie |
84 |
| - sessionStorage.setItem('itp', true); |
85 |
| - document.cookie = 'itp=true; secure; SameSite=None'; |
86 |
| -
|
87 |
| - if (!document.cookie) { |
88 |
| - // Still unable to set, must be blocked |
89 |
| - throw 'Cannot set third-party cookie.'; |
90 |
| - } |
91 |
| -
|
92 |
| - // Storage is OK... redirect back to home page of app |
93 |
| - window.location.href = '{!! $redirect !!}'; |
94 |
| - } catch (error) { |
95 |
| - // Show manual cookie card |
96 |
| - console.warn('Storage access may be blocked.', error); |
97 |
| -
|
98 |
| - // Hide the attempt card and show the error card |
99 |
| - var attemptCard = document.getElementById('attempt'); |
100 |
| - var errorCard = document.getElementById('error'); |
101 |
| - attemptCard.classList.add('Polaris-Card--hide'); |
102 |
| - errorCard.classList.remove('Polaris-Card--hide'); |
103 |
| - } |
| 79 | + var tryBtn = 'TriggerAllowCookiesPrompt'; |
| 80 | + var manualBtn = 'TriggerAllowCookiesPrompt2'; |
| 81 | + var isSupported = document.requestStorageAccess !== undefined; |
| 82 | +
|
| 83 | + /** |
| 84 | + * Handle swapping the request card with the manual card. |
| 85 | + */ |
| 86 | + function swapCards() { |
| 87 | + var attemptCard = document.getElementById('attempt'); |
| 88 | + var errorCard = document.getElementById('error'); |
| 89 | + attemptCard.classList.add('Polaris-Card--hide'); |
| 90 | + errorCard.classList.remove('Polaris-Card--hide'); |
| 91 | + } |
| 92 | +
|
| 93 | + /** |
| 94 | + * Handle accessing browser storage. |
| 95 | + */ |
| 96 | + function handleStorageAccess(e, btn) { |
| 97 | + /** |
| 98 | + * Redirect back to the app. |
| 99 | + */ |
| 100 | + var redirect = function () { |
| 101 | + window.location.href = '{!! $redirect !!}'; |
| 102 | + }; |
| 103 | +
|
| 104 | + /** |
| 105 | + * Try and set items to the browser storage. |
| 106 | + * Throw on error. |
| 107 | + */ |
| 108 | + var attempt = function () { |
| 109 | + sessionStorage.setItem('itp', true); |
| 110 | + document.cookie = 'itp=true; secure; SameSite=None'; |
| 111 | +
|
| 112 | + if (!document.cookie) { |
| 113 | + // Unable to set, storage must be blocked... |
| 114 | + throw 'Cannot set third-party cookie.'; |
104 | 115 | }
|
105 |
| - ); |
| 116 | +
|
| 117 | + // Storage is OK... redirect back to home page of app |
| 118 | + redirect(); |
| 119 | + }; |
| 120 | +
|
| 121 | + /** |
| 122 | + * Unable to access storage. |
| 123 | + */ |
| 124 | + var failure = function (error) { |
| 125 | + // Show manual cookie card |
| 126 | + console.warn('Storage access may be blocked.', error); |
| 127 | + swapCards(); |
| 128 | + }; |
| 129 | +
|
| 130 | + /** |
| 131 | + * Common function for supported and unsupported browsers. |
| 132 | + */ |
| 133 | + var execute = function () { |
| 134 | + if (btn === manualBtn) { |
| 135 | + redirect(); |
| 136 | + return; |
| 137 | + } |
| 138 | +
|
| 139 | + try { |
| 140 | + attempt(); |
| 141 | + } catch (error) { |
| 142 | + failure(error); |
| 143 | + } |
| 144 | + }; |
| 145 | +
|
| 146 | + if (isSupported) { |
| 147 | + // Supported... try it |
| 148 | + document.requestStorageAccess().then(execute); |
| 149 | + } else { |
| 150 | + // Unsupported... show manual cookie card |
| 151 | + execute(); |
| 152 | + } |
106 | 153 | }
|
107 | 154 |
|
108 |
| - document.getElementById('TriggerAllowCookiesPrompt').addEventListener('click', handleStorageAccess); |
109 |
| - document.getElementById('TriggerAllowCookiesPrompt2').addEventListener('click', handleStorageAccess); |
| 155 | + if (!isSupported) { |
| 156 | + // Unsupported... show manual cookie card |
| 157 | + swapCards(); |
| 158 | + } |
| 159 | +
|
| 160 | + // Add event listeners |
| 161 | + for (var btn of [tryBtn, manualBtn]) { |
| 162 | + document.getElementById(btn).addEventListener('click', function (e) { |
| 163 | + handleStorageAccess(e, btn); |
| 164 | + }); |
| 165 | + } |
110 | 166 | </script>
|
111 | 167 | </body>
|
112 | 168 | </html>
|
0 commit comments