Created
November 2, 2022 17:55
-
-
Save KarllosSouza/81280f0c713fa63c8ac1c9c9b9999578 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
console.log('<= cross-domain =>') | |
const VALUE_KEY = 'myValue' | |
window.onload = () => { | |
console.log('cross-domain load =>') | |
try { | |
if (typeof window !== 'undefined' && typeof localStorage !== 'undefined') { | |
const currentCartToken = localStorage.getItem(VALUE_KEY) | |
if (currentCartToken) { | |
window.parent.postMessage(currentCartToken, '*') | |
} | |
} | |
} catch (error) { | |
console.error(`Failed to get value in localStorage: ${error}`) | |
} | |
} | |
window.addEventListener('message', ({ data }) => { | |
console.log('cross-domain event =>', data) | |
try { | |
if (typeof window !== 'undefined' && typeof localStorage !== 'undefined') { | |
localStorage.setItem(VALUE_KEY, data) | |
} | |
} catch (error) { | |
console.error(`Failed to set value in localStorage: ${error}`) | |
} | |
}) | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment