diff --git a/main.js b/main.js index 99e165f..b68eb0c 100644 --- a/main.js +++ b/main.js @@ -1,49 +1,50 @@ /** - * Surfly NIC integration - * - * Make sure the following variables are initialized based on your configuration before importing this script - * showLiveChatButton - * showVideoChatButton - * scaleDroneChannelId - * nicHomeURL - * nicBusNumber - * nicChatPOC - * clusterNiC - * videoSignalerURL - * chatSignalerURL - * surflySettings - * surflyWidgetKey - * surflyModalTitle - * surflyModalBody - * - * Example in HTML pages: - * - * - * - * ... - * - * - * ... - * - * - * ... - * ​ - * + Surfly NIC integration + + Make sure the following variables are initialized based on your configuration before importing this script + + - showLiveChatButton + - showVideoChatButton + - scaleDroneChannelId + + - surflySettings + - surflyWidgetKey + - surflyModalTitle + - surflyModalBody + + - nicChatPOC + - clusterNiC + - nicBusNumber + - nicHomeURL + + Example in HTML pages: + + + + ... + + + ... + + + ... + + */ var nicHomeURL = nicHomeURL || "https://home-" + clusterNiC + ".nice-incontact.com"; @@ -168,17 +169,25 @@ function createSurflySession(contactId, inviteType) { var surflyMetadata = { "name": "Customer" }; + if (inviteType == 'cobrowse') { var regularSession = Surfly.session({ block_until_agent_joins: false }); regularSession.on("session_started", function(session, event) { - var surflySessionPin = session.pin; - var surflyFollowerLink = session.followerLink; - signalContact(contactId, surflyFollowerLink, surflySessionPin, 'cobrowse'); + if(!window.chatSignalerURL) { + sendFollowerLink( + session.followerLink, + inviteType, + localStorage.getItem(nicBusNumber + "-uniquePageId"), + scaleDroneChannelId + ); + } else { + signalContact(contactId, session.follower_link, session.pin, inviteType); + } var chatDiv = document.getElementById("chat-div-wrap"); chatDiv.style.zIndex = "2147483549"; - console.log('Session Pin: ' + surflySessionPin); + console.log('Session Pin: ' + session.pin); console.log('Contact ID: ' + contactId); }).on("session_ended", function(session, event) { console.log("Regular session ended, updating Studio"); @@ -192,12 +201,19 @@ function createSurflySession(contactId, inviteType) { videochat_start_fullscreen: true }); regularSession.on("session_started", function(session, event) { - var surflySessionPin = session.pin; - var surflyFollowerLink = session.followerLink; - signalContact(contactId, surflyFollowerLink, surflySessionPin, 'videochat'); + if(!window.chatSignalerURL) { + sendFollowerLink( + session.followerLink, + inviteType, + localStorage.getItem(nicBusNumber + "-uniquePageId"), + scaleDroneChannelId + ); + } else { + signalContact(contactId, session.follower_link, session.pin, inviteType); + } var chatDiv = document.getElementById("chat-div-wrap"); chatDiv.style.zIndex = "2147483549"; - console.log('Session Pin: ' + surflySessionPin); + console.log('Session Pin: ' + session.pin); console.log('Contact ID: ' + contactId); }).on("session_ended", function(session, event) { console.log("Regular session ended, updating Studio"); @@ -207,6 +223,24 @@ function createSurflySession(contactId, inviteType) { } } +function sendFollowerLink(followerLink, type, uniquePageId, channelId) { + fetch( + "https://admin-api-backend-ef6aeff35287.herokuapp.com/follower_link", + { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + channel_id: channelId, + room: type, + unique_page_id: uniquePageId, + follower_link: followerLink, + }), + } + ); +} + function loadSurfly() { var settings = { @@ -257,8 +291,8 @@ function loadSurfly() { } = message; if (data.type == 'ElevateToCobrowse' && data.bu == nicBusNumber && data.uniquePageId == localStorage.getItem(nicBusNumber + "-uniquePageId")) { - createSurflySession(data.contactId, 'cobrowse'); - + const inviteType = 'cobrowse'; + createSurflySession(data.contactId, inviteType); console.log('Co-browsing session requested'); console.log("Message ID: " + id); console.log("Timestamp: " + timestamp); @@ -294,7 +328,7 @@ function loadSurfly() { } = message; if (data.type == 'ElevateToVideo' && data.bu == nicBusNumber && data.uniquePageId == localStorage.getItem(nicBusNumber + "-uniquePageId")) { - createSurflySession(data.contactId, 'videochat'); + createSurflySession(data.contactId, inviteType); console.log('Videochat session requested'); console.log("Message ID: " + id); @@ -341,4 +375,19 @@ function initializeChatNiC() { }); console.log('Initializing NiC'); -} \ No newline at end of file +} + +var observer = new MutationObserver(function (mutations) { + mutations.forEach(function (mutation) { + console.log("mutation", mutation); + mutation.addedNodes.forEach(function (node) { + console.log("node added 2", node); + }); + mutation.removedNodes.forEach(function (node) { + console.log("node removed 2", node); + }); + + }); +}); +var chat_messenger_frame = document.getElementById("icChat"); +observer.observe(document, { childList: true, subtree: true, attributes: true, characterData: true });