Created
May 29, 2020 21:18
-
-
Save brh55/92b0db676b9595dfc4ede76b0a0d6731 to your computer and use it in GitHub Desktop.
script.js
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
(function (d, s, id) { | |
var js, | |
sjs = d.getElementsByTagName(s)[0]; | |
if (d.getElementById(id)) return; | |
js = d.createElement(s); | |
js.id = id; | |
js.src = "https://sdk.snapkit.com/js/v1/create.js"; | |
sjs.parentNode.insertBefore(js, sjs); | |
if ( | |
document.getElementsByClassName("snapchat-creative-kit-share").length === 0 | |
) { | |
window.snapKitInit = function () { | |
snap.creativekit.initalizeShareButtons( | |
document.getElementsByClassName("snapchat-share-button") | |
); | |
}; | |
} | |
var url = encodeURIComponent(window.location.href.replace("https://", "")); | |
var button = document.getElementById("snapchatShare"); | |
var qsKeys = ["title", "siteName", "currency", "price", "image"]; | |
var meta = [ | |
'meta[property="og:title"]', | |
'meta[property="og:site_name"]', | |
'meta[property="og:price:currency"]', | |
'meta[property="og:price:amount"]', | |
'meta[property="og:image:secure_url"]', | |
]; | |
meta = meta.map((tag, index) => { | |
let data = document.querySelector(tag); | |
if (data) { | |
if (index === meta.length - 1) { | |
data.content = data.content.replace("https://", "").trim(); | |
} | |
return data.content; | |
} | |
return null; | |
}); | |
var attachedQs = meta.reduce((qs, currentValue, index) => { | |
if (currentValue) { | |
qs += `&${qsKeys[index]}=${encodeURI(currentValue)}`; | |
} | |
return qs; | |
}, `url=${url}`); | |
attachedQs = btoa(attachedQs); | |
button.setAttribute( | |
"data-share-url", | |
"https://snapchat-shopify.herokuapp.com/redirect?" + attachedQs | |
); | |
})(document, "script", "snapkit-creative-kit-sdk"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment