Skip to content

Instantly share code, notes, and snippets.

@brh55
Created May 29, 2020 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brh55/92b0db676b9595dfc4ede76b0a0d6731 to your computer and use it in GitHub Desktop.
Save brh55/92b0db676b9595dfc4ede76b0a0d6731 to your computer and use it in GitHub Desktop.
script.js
(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