Skip to content

Instantly share code, notes, and snippets.

@phanngoc
Last active October 22, 2018 02:50
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 phanngoc/e5f441f4d1c955f640e90903414de664 to your computer and use it in GitHub Desktop.
Save phanngoc/e5f441f4d1c955f640e90903414de664 to your computer and use it in GitHub Desktop.
var fnInsertIframe = function(base_url, _args) {
var dfIframe = $.Deferred();
_args.param = "top"
var url = base_url + "/web?" + $.param(_args)
if ($("#hidden-chatbot").length == 0) {
var iframe = document.createElement("iframe");
iframe.style.display = "none";
iframe.setAttribute("id", "hidden-chatbot");
iframe.setAttribute('width', '0');
iframe.setAttribute('height', '0');
iframe.setAttribute('border', '0');
iframe.setAttribute('src', url);
var t = document.getElementsByTagName('body')[0];
t.appendChild(iframe);
} else {
$("iframe").attr('src', url);
}
dfIframe.resolve(true)
return $.when(dfIframe.promise())
}
var checkInitLogin = function(base_url, membId) {
var dfd = $.Deferred();
var loginFunc = function(base_url, membId) {
var credentials = {membid: membId}
$.ajax({
method: "POST",
url: base_url + "/external-login",
data: JSON.stringify(credentials),
contentType: "application/json",
dataType: "json",
success: function(response) {
if (response.success) {
localStorage.setItem("sessionid", response.data.sessionid)
var _args = {
param : "top",
sessionid: response.data.sessionid,
page_url: window.location.href,
referrer_url: document.referrer,
}
fnInsertIframe(base_url, _args).then((response) => {
dfd.resolve({success: true})
})
} else {
dfd.resolve({success: false})
}
},
error: function() {
dfd.resolve({success: false, messages: ["Unauthorized"]})
}
});
}
var fnCheckLogin = function(sessionid) {
return $.ajax({
method: "POST",
url: base_url + "/check-external-login",
data: JSON.stringify({sessionid: sessionid}),
contentType: "application/json",
dataType: "json",
})
}
var sessionId = localStorage.getItem("sessionid")
if (sessionId) {
fnCheckLogin(sessionId).done(function(response) {
if (response.status == 401 && membId) {
loginFunc(base_url, membId)
} else {
dfd.resolve(response)
}
}).fail(function(jqXHR, textStatus, errorThrown) {
dfd.resolve({success: false, messages: [textStatus]})
});
} else {
if (membId) {
loginFunc(base_url, membId)
} else {
// init, if is anonymous user
dfd.resolve({success: true})
}
}
return $.when(dfd.promise());
}
// Cookies
window.createCookie = function(name, value, days) {
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "; expires=" + date.toGMTString();
} else var expires = "";
document.cookie = name + "=" + value + expires + "; path=/";
}
window.readCookie = function(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
window.eraseCookie = function(name) {
createCookie(name, "", -1);
}
var setBaseUrlChatbot = function(base_url) {
localStorage.setItem("base_url_chatbot", base_url);
}
var getBaseUrlChatbot = function() {
return localStorage.getItem("base_url_chatbot");
}
var processTemplateIcon = function(base_url) {
if ($('#btnChatFix').length == 0) {
var link = base_url + "/web?param=top"
var template = ' \
<p class="event-open" id="btnChatFix" data-messageid="" data-type_action=""> \
<a href="'+link+'" target="_blank"> \
<span id="chatbot-number-noti" style="display: none"> \
<img src="'+base_url+'/img/icon_push-notifications_1.png" alt="チャットで質問"/> \
</span> \
<img id="chatbot-img-icon" src="https://secure.lovecosmetic.net/images/btn_chat.png" alt="チャットで質問" /> \
</a> \
</p> \
'
$("body").append(template)
}
}
window.sequenceCheck = function (base_url, name_cookie) {
var deferSequence = $.Deferred()
var fnAppendJquery = function() {
var defJquery = jQuery.Deferred();
if (typeof $ === "undefined") {
var e = document.createElement("script");
var t = document.getElementsByTagName("head")[0];
e.async = 1;
e.src = base_url + "/js/jquery.min.js";
t.parentNode.appendChild(e);
var fnCheckJq = () => {
window.setTimeout(() => {
if (typeof $ === "undefined") {
fnCheckJq()
} else {
defJquery.resolve(true)
}
}, 50 );
}
} else {
defJquery.resolve(true)
}
return $.when(defJquery.promise());
}
var membId = null
var initCall = true
var processCheck = function() {
fnAppendJquery().then(function(response) {
if (response) {
return checkInitLogin(base_url, membId)
} else {
deferSequence.resolve(false)
}
}).then(function(response) {
if (response.success) {
var args = {page_url: window.location.href, referrer_url: document.referrer}
fnInsertIframe(base_url, args).then(function(response) {
var e = document.createElement("script");
var t = document.getElementsByTagName("head")[0];
var body = document.getElementsByTagName("body")[0];
// Import process event in page
e = document.createElement("script");
e.async = 1;
e.src = base_url + "/js/event_page.js";
t.appendChild(e);
// Import file css to style banner
e = document.createElement("link")
e.async = 1
e.rel = "stylesheet"
e.type = "text/css"
e.href = base_url + "/css/track_user.css";
t.appendChild(e);
processTemplateIcon(base_url)
deferSequence.resolve(true)
});
} else {
deferSequence.resolve(false)
}
})
}
return {
init: function() {
fnAppendJquery().then(function(response) {
if (response) {
setInterval(() => {
membId = readCookie(name_cookie)
if (membId) {
if (initCall) {
processCheck()
initCall = false
}
} else {
if (!initCall) {
logoutChatbot()
initCall = true
}
}
}, 60000);
}
})
return this
},
success: function(callback) {
deferSequence.promise().then(callback)
return this
},
login: function() {
membId = readCookie(name_cookie)
processCheck()
initCall = false
return this
}
}
}
window.logoutChatbot = function() {
var windowPush = null
if ($('#hidden-chatbot')[0]) {
windowPush = $('#hidden-chatbot')[0].contentWindow;
}
if (windowPush != null) {
windowPush.postMessage({type: 'logout', data: ''}, '*');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment