lykdat
Version:
Official JavaScript SDK for Lykdat API (https://solutions.lykdat.com/)
241 lines (228 loc) • 10.7 kB
JavaScript
const $e61084b05bbe370c$var$CSS = `
.lykdat-button {
background-color: black;
padding: 10px;
border-radius: 36px;
display: flex;
align-items: center;
justify-content: space-evenly;
outline: none;
border: none;
cursor: pointer;
transition: all 0.5s;
color: #fff;
box-shadow: 0px 12px 24px rgba(8, 8, 8, 0.3);
margin: 0 auto;
}
.lykdat-button:hover {
width: auto;
}
.lykdat-button > .icon {
width: 30px;
height: 30px;
object-fit: contain;
}
.lykdat-button > .text {
display: none;
}
.lykdat-button:hover > .text {
display: inline-block;
margin: 0 10px;
}
.lykdat-float-btn {
position: fixed;
bottom: 20px;
right: 20px;
z-index: 9999;
}`;
const $e61084b05bbe370c$var$IMG_SEARCH_ICON = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFMAAABMCAYAAAARMotOAAAFxElEQVR4Xu2ceahVVRTGn0Na5tBAhaYZkmVJZBkRUSEUFQRmWAmRSoUVRANURBARBRGCFUFU0B9mFA0WEflHVmBzRLO9xj+abaCBonn6+pbrHD33e/cM774z3nd+8CHPs9Ze+37vvrP32XvfOzDQ0tLS0tLS0tLS0rINAJOoJdQd1OPUZuo7agv1LHUftYqaR43R/JaBrSbuRl1JfUL9h3R+o+6lFmhboxoasghuYi/8QF1NTdR2RxU0YAx1MfV7hz29sZ6apjVGDfB31L9iykh4itpJ6/Q9fNHHUn+LGXmwmhqr9UqHndiduoZ6Ej6KvjNMbaLOQsqL4fVdqY8Qzz/UM9S51P7wwWlv+Ci/lvpxe+gQLPd4rVkq7MAp1JfSsV6wkXiZth+F16/VpAhfwfsyXvNCeG0O9WpnWgcvISG/UFj4KOQzCIRs0hohvLYn9YUmBHxLHa453WDcVGqD5If8SZ2qOYUDH1Hflc6MlM1aJ4TXlmpwgN0/F2t8Eoyfgfgp1XqNLxwWPVJ7kQOrtU4Ir92twQEbqXEanwZzztSGAn6iJmh8ocDneXnyGBLme7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qJnw0LnK/O473EDNdQoPNPEgDS2Tr/VdBg820gcc2923S/UaJulH7EoKmmhkCX+0pQxODf2NXlNB0M+sEWjPzAw0wc53G1hXEn5OvjZn2YdGpGl832MdZ1F/S95DamGk8Tx0B/wStzS+j2rlEaW3TNPiz/luIp3QzL9UeCPa5RPvYn53irZO2IP1xtvT1zMXagz4i0xJebrDgDoj/hFeTGdTXWgosvFx70nDs1nSivs5SYOGx1A3I92txqsI20lYhZQ20cNiBhfDvX/sY3Q881RXbJvkU/nVmh+jrqhT4V/LsQ+3bEM2G73jGPr+3tLS0tLS0jGb+B4RO3T2m4Od5AAAAAElFTkSuQmCC";
function $e61084b05bbe370c$export$31434ba8ba4bf7c7() {
const style_id = "lykdat-img-search-button-style";
if (!document.querySelector(`style#${style_id}`)) {
const style = document.createElement("style");
style.id = style_id;
style.innerHTML = $e61084b05bbe370c$var$CSS;
document.head.appendChild(style);
}
const button = document.createElement("button");
button.classList.add("lykdat-button");
button.innerHTML = `
<img src="${$e61084b05bbe370c$var$IMG_SEARCH_ICON}" alt="Image Search" class='icon' />
<span class='text'>Image Search</span>`;
const floatBtn = document.createElement("div");
floatBtn.classList.add("lykdat-float-btn");
floatBtn.appendChild(button);
document.body.appendChild(floatBtn);
return button;
}
function $765f2eb2164e2bfc$export$747c2e4081d1d5aa(config) {
const { publishableApiKey: apiKey , catalogName: catalogName } = config;
const context = window.btoa(`${apiKey}${catalogName}:${apiKey.length}`);
const button = $765f2eb2164e2bfc$var$getOrCreateButton(config.triggerSelector);
const iframeContainerId = "lykdat-iframe-container";
const iframeURL = "https://easyuse.lykdat.com";
$765f2eb2164e2bfc$var$attachIFrameStyle(iframeContainerId);
button.addEventListener("click", ()=>{
document.body.classList.add("no-scroll");
const iframeContainer = document.querySelector(`#${iframeContainerId}`);
iframeContainer.classList.remove("hidden");
});
window.addEventListener("message", (event)=>{
if (event.origin !== iframeURL) return;
if (event.data === "close--lykdat--drawer") {
iframeContainer.classList.add("hidden");
document.body.classList.remove("no-scroll");
}
}, false);
let iframeContainer = document.querySelector(`div#${iframeContainerId}`);
if (!iframeContainer) iframeContainer = document.createElement("div");
iframeContainer.classList.add("hidden");
iframeContainer.id = iframeContainerId;
iframeContainer.style.position = "fixed";
const iframe = document.createElement("iframe");
iframe.src = `${iframeURL}?context=${context}`;
iframe.style.height = "100%";
iframe.style.width = "100%";
iframe.style.border = "none";
iframe.style.display = "flex";
iframeContainer.appendChild(iframe);
document.body.appendChild(iframeContainer);
}
function $765f2eb2164e2bfc$var$attachIFrameStyle(iframeId) {
const css = `
#${iframeId} {
position: fixed;
top: 0;
left: 0;
right: 0;
width: 100vw;
height: 100vh;
z-index: 9999;
}
#${iframeId}.hidden {
display: none;
}
.no-scroll {
height: 100% !important;
overflow: hidden !important;
}`;
const style_id = "lykdat-img-search-iframe-container-style";
if (!document.querySelector(`style#${style_id}`)) {
const style = document.createElement("style");
style.id = style_id;
style.innerHTML = css;
document.head.appendChild(style);
}
}
function $765f2eb2164e2bfc$var$getOrCreateButton(selector) {
if (selector) return document.querySelector(selector);
return (0, $e61084b05bbe370c$export$31434ba8ba4bf7c7)();
}
/**
* Configuration object used to initialise the In-Stock Alert UI
*/ function $3117eed9cedc4516$export$4d839386772a9115(config) {
const productUrl = window.location.href;
const apiKey = config.publishableApiKey;
const context = window.btoa(`${apiKey}${config.websiteName}:${apiKey.length}`);
const iframeContainerId = "lykdat-alert-iframe-container";
const iframeURL = "https://easyuse.lykdat.com/stock-alert";
const iframeContainer = document.createElement("div");
iframeContainer.id = iframeContainerId;
const iframe = document.createElement("iframe");
iframe.src = `${iframeURL}?url=${encodeURIComponent(productUrl)}&context=${context}`;
iframe.style.width = "100%";
iframe.style.border = "none";
iframeContainer.appendChild(iframe);
const targetContainer = document.querySelector(config.targetSelector);
if (targetContainer) targetContainer.appendChild(iframeContainer);
}
function $3117eed9cedc4516$export$b10ea90dec0bb8a9(config, emailAddress, productUrl) {
const body = {
website_name: config.websiteName,
subscriber_email: emailAddress,
product_url: productUrl,
type: "availability"
};
return fetch("https://cloudapi.lykdat.com/v1/products/alerts/subscribe", {
method: "POST",
body: JSON.stringify(body),
headers: {
"X-Api-Key": config.publishableApiKey,
"Content-Type": "application/json"
}
}).then((resp)=>{
return resp.json().then((jsonData)=>{
if (resp.status !== 200) throw new Error(jsonData.error || `unable able to subscribe: ${resp.status}`);
return {
...jsonData.data
};
});
});
}
function $3117eed9cedc4516$export$548a527e11f75bd6(config, emailAddress, productUrl) {
const body = {
website_name: config.websiteName,
subscriber_email: emailAddress,
product_url: productUrl,
type: "price"
};
return fetch("https://cloudapi.lykdat.com/v1/products/alerts/subscribe", {
method: "POST",
body: JSON.stringify(body),
headers: {
"X-Api-Key": config.publishableApiKey,
"Content-Type": "application/json"
}
}).then((resp)=>{
return resp.json().then((jsonData)=>{
if (resp.status !== 200) throw new Error(jsonData.error || `unable able to subscribe: ${resp.status}`);
return {
...jsonData.data
};
});
});
}
function $3117eed9cedc4516$export$a6859be1cfc01481(config, productUrl) {
const body = {
product_url: productUrl
};
return fetch("https://cloudapi.lykdat.com/v1/products/extract", {
method: "POST",
body: JSON.stringify(body),
headers: {
"X-Api-Key": config.publishableApiKey,
"Content-Type": "application/json"
}
}).then((resp)=>{
return resp.json().then((jsonData)=>{
if (resp.status !== 200) throw new Error(jsonData.error || `unable able to fetch product: ${resp.status}`);
return {
...jsonData.data
};
});
});
}
/**
* Configuration object used to communicate with the Text Search API
*/ function $26b36787cad62ef8$export$ac10a6cd936a598a(query, config, options) {
let queryParams = `query=${query}&catalog_name=${config.catalogName}`;
if (options) for(const key in options){
const val = options[key];
queryParams += Array.isArray(val) ? `&${key}=${val.join(",")}` : `&${key}=${val}`;
}
return fetch(`https://cloudapi.lykdat.com/v1/products/search/text?${queryParams}`, {
method: "GET",
headers: {
"X-Api-Key": config.publishableApiKey
}
}).then((resp)=>{
return resp.json().then((jsonData)=>{
if (resp.status !== 200) throw new Error(jsonData.error || `search request failed: ${resp.status}`);
return {
...jsonData.data
};
});
});
}
export {$765f2eb2164e2bfc$export$747c2e4081d1d5aa as initImageSearchUI, $3117eed9cedc4516$export$4d839386772a9115 as initInStockAlertUI, $3117eed9cedc4516$export$b10ea90dec0bb8a9 as subscribeToInStockAlert, $3117eed9cedc4516$export$548a527e11f75bd6 as subscribeToPriceAlert, $3117eed9cedc4516$export$a6859be1cfc01481 as extractProduct, $26b36787cad62ef8$export$ac10a6cd936a598a as searchText};
//# sourceMappingURL=module.js.map