Last active
November 21, 2023 23:28
-
-
Save kolbykskk/66afffc59f1699cd3d6e1cc393301c42 to your computer and use it in GitHub Desktop.
wigs-customer-returns Shopify page script
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
<style> | |
#rt-overlay { | |
display: none; | |
position: fixed; | |
top: 0; | |
left: 0; | |
width: 100%; | |
height: 100%; | |
background-color: rgba(0 0 0 / 0.8); | |
z-index: 101; | |
} | |
#rt-container { | |
opacity: 0; | |
transition: opacity 0.2s ease; | |
} | |
#rt-container.rt-loaded { | |
opacity: 1; | |
} | |
#rt-iframe { | |
width: 100%; | |
border: none; | |
position: relative; | |
z-index: 101; | |
} | |
#rt-container.rt-modal-open #rt-overlay { | |
display: block; | |
} | |
</style> | |
<div id="rt-container"> | |
<div id="rt-overlay"></div> | |
</div> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.9/iframeResizer.min.js"></script> | |
<script> | |
(function() { | |
const container = document.getElementById('rt-container'); | |
let scrollPosition; | |
function createAndInsertIframe(id, queryParams) { | |
const domain = window.location.hostname.replace(/^www\./, ''); | |
const iframeUrl = id ? `https://returns.${domain}/returns/${id}?${queryParams}` : `https://returns.${domain}?${queryParams}`; | |
const iframe = document.createElement('iframe'); | |
iframe.src = iframeUrl; | |
iframe.id = 'rt-iframe'; | |
container.appendChild(iframe); | |
iframe.onload = () => { | |
setTimeout(() => { | |
container.classList.add('rt-loaded'); | |
}, 500); | |
}; | |
switch (domain) { | |
case 'hairtoppers.com': | |
container.parentElement.style.maxWidth = 'none'; | |
break; | |
case 'ellenwille.com': | |
container.parentElement.parentElement.style.width = '100%'; | |
break; | |
} | |
} | |
function handleSimpleMessage(message) { | |
if (message === 'open-modal') { | |
container.classList.add('rt-modal-open'); | |
scrollPosition = window.pageYOffset; | |
window.scrollTo(0, 0); | |
} else if (message === 'close-modal') { | |
container.classList.remove('rt-modal-open'); | |
window.scrollTo(0, scrollPosition); | |
} | |
} | |
document.addEventListener('DOMContentLoaded', () => { | |
const queryParams = new URLSearchParams(window.location.search); | |
const id = queryParams.get('id'); | |
createAndInsertIframe(id, queryParams); | |
iFrameResize({ | |
heightCalculationMethod: 'taggedElement', | |
onMessage: (messageData) => { | |
const message = messageData.message; | |
if (typeof message === 'string') { | |
handleSimpleMessage(message); | |
} else if (message && message.message === 'redirect') { | |
window.location.href = message.value; | |
} else if (message && message.message === 'copyToClipboard') { | |
navigator.clipboard.writeText(message.value); | |
} | |
} | |
}, '#rt-iframe'); | |
}); | |
})(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment