(() => { const { EventManager, Handlebars, utils } = ShopbySkin; const { createPageScriptInstance, setGlobalsVariableBy, isMobileDevice } = utils; const ARR_LOCALHOST = [ 'http://localhost:4000', 'http://localhost:4001', 'http://localhost:4002', 'http://localhost:4003', 'http://localhost:4004', 'http://localhost:4005', 'http://localhost:4006', 'http://localhost:4007', 'http://localhost:4008', 'http://localhost:4009', ] const REAL_DOMAIN = 'https://energy-scoop.com'; // const KAKAO_APPKEY = 'ae1965aba257d69fccdd0afe0f378bec'; var scriptElm = document.createElement('script'); scriptElm.src = '//developers.kakao.com/sdk/js/kakao.js'; document.head.appendChild(scriptElm); setTimeout(() => { //Kakao.init('ae1965aba257d69fccdd0afe0f378bec'); }, 150); Handlebars.registerHelper({ getKmTweetLink(snsTitle, snsUrl) { // 제목과 링크가 공유됨 let rtnLink = 'https://twitter.com/intent/tweet?'; rtnLink += 'text=' + encodeURIComponent(snsTitle); // snsUrl 설정 안된 경우 현재 링크를 공유링크로 설정 let link = typeof snsUrl === 'object' ? window.location.href : snsUrl; link = replaceLocalUrl(link); // localhost code rtnLink += '&url=' + encodeURIComponent(link); return rtnLink; }, getKmFacebookLink(snsUrl) { // 링크만 공유됨 let rtnLink = 'https://www.facebook.com/sharer/sharer.php?'; // snsUrl 설정 안된 경우 현재 링크를 공유링크로 설정 let link = typeof snsUrl === 'object' ? window.location.href : snsUrl; link = replaceLocalUrl(link); // localhost code rtnLink += 'u=' + encodeURIComponent(link); return rtnLink; }, // getKmKakaoAppKey() { // return KAKAO_APPKEY; // }, // getKmKakaoLink(snsUrl) { // return ''; // }, getKmCurrnetLink() { let link = window.location.href; link = replaceLocalUrl(link); // localhost code return link; }, }); const replaceLocalUrl = (link) => { let resultUrl = link; for ( const localhost of ARR_LOCALHOST ) { resultUrl = resultUrl.replace(localhost, REAL_DOMAIN) } return resultUrl; } const el = document.querySelector('kaimen-share-box'); const pageType = document.currentScript.getAttribute('shopby-page-type'); const kakaoData = { title: '', description: '', imageUrl: '', commerceData: {}, }; // 클릭이벤트 맵 const actionMap = { SHARE_SHOW: () => { document.querySelector(".share_box").style.display = "block"; Kakao.init('ae1965aba257d69fccdd0afe0f378bec'); //real let link = window.location.href; const currentLink = replaceLocalUrl(link); switch(pageType){ case "product" : Kakao.Share.createDefaultButton({ container: '#kakaotalk-sharing-btn', objectType: 'commerce', content: { title: kakaoData.productName, imageUrl: "https:"+kakaoData.imageUrl, link: { mobileWebUrl: currentLink, webUrl: currentLink, }, }, commerce: kakaoData.commerceData, }); break; case "event" : Kakao.Share.createDefaultButton({ container: '#kakaotalk-sharing-btn', objectType: 'feed', content: { title: kakaoData.title, description: kakaoData.description, imageUrl: "https:"+kakaoData.imageUrl, link: { mobileWebUrl: currentLink, webUrl: currentLink, }, }, }); break; } }, SHARE_CLOSE: () => { document.querySelector(".share_box").style.display = "none"; }, SHARE_URL: () => { var urlToCopy = document.querySelector("#urls").innerText.trim(); // 임시 텍스트 에어리어 생성 및 설정 var temp = document.createElement("textarea"); document.querySelector("body").append(temp); temp.value = urlToCopy; temp.select(); // 텍스트 복사 document.execCommand("copy"); temp.remove(); // 임시 텍스트 에어리어 삭제 alert("URL을 복사했습니다."); }, }; // 클릭이벤트 핸들러 const clickEventListener = (e) => { const action = e?.target?.closest('[shopby-action]')?.getAttribute('shopby-action'); actionMap[action]?.(e); }; el?.addEventListener('click', clickEventListener); EventManager.on('PAGE_LOAD_COMPLETED', ({ profile }) => { if(pageType === 'product'){ EventManager.on('QUERY_PRODUCT_DETAIL', (data) => { kakaoData.productName = data.baseInfo.productName; kakaoData.originPrice = data.price.salePrice; kakaoData.salePrice = data.price.salePrice - (data.price.additionDiscountAmt + data.price.immediateDiscountAmt); kakaoData.title = data.baseInfo.productName; kakaoData.imageUrl = data.baseInfo.imageUrls[0]; kakaoData.commerceData = { productName: kakaoData.productName, regularPrice: parseInt(kakaoData.originPrice), } if (parseInt(kakaoData.salePrice) < parseInt(kakaoData.originPrice)) { kakaoData.commerceData.discountPrice = parseInt(kakaoData.salePrice); kakaoData.commerceData.discountRate = parseInt(100 - (100 * kakaoData.commerceData.discountPrice / kakaoData.commerceData.regularPrice)); } const template = Handlebars.compile(el.innerHTML); let box = template({ pageType: pageType, ...kakaoData }); document.querySelector("kaimen-share-box").innerHTML = box; document.querySelector("product-summary .prd_nm").after(document.querySelector("sns-share").querySelector("kaimen-share-box")); }); }else if(pageType === 'event'){ EventManager.on('QUERY_DISPLAY_EVENTS_EVENT_NO', (data) => { kakaoData.title = data.label; kakaoData.description = data.label; kakaoData.imageUrl = data.pcImageUrl; document.querySelector("kaimen-share-box").setAttribute('shopby-title', data.label); const template = Handlebars.compile(el.innerHTML); let box = template({ pageType: pageType, productName: data.label, ...kakaoData, }); document.querySelector("kaimen-share-box").innerHTML = box; }); } }); })();