Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* tidio close button with x days memory (no jQuery needed) */
- (function() {
- tidio_close_data = {
- key: 'tidio-close-stamp'
- , ele_parent: '#button.chat-closed #button-body'
- , ele_id: 'close_button'
- };
- tidio_close_data.ele_select = tidio_close_data.ele_parent + ' #' + tidio_close_data.ele_id;
- // close, and set timestamp
- var close_tidio_chat = function(e) {
- e.stopPropagation();
- window.tidioChatApi.close();
- window.tidioChatApi.hide();
- if (localStorage) {
- localStorage.setItem(tidio_close_data.key, Date.now());
- }
- }
- // inject close button as a span positioned above and right of floating chat button
- function addCloseButton() {
- var $doc = document.getElementById("tidio-chat-iframe").contentDocument;
- var $parent = $doc.querySelector(tidio_close_data.ele_parent);
- $parent.insertAdjacentHTML('beforeend', '<span id="' + tidio_close_data.ele_id + '">X</span>');
- // now get this element we just inserted
- tidio_close_data.ele = $doc.querySelector(tidio_close_data.ele_select);
- tidio_close_data.ele.addEventListener("click", close_tidio_chat, true);
- // styling for button offset, style as you want
- var $css = `{
- font-size: 16px;
- font-weight: bold;
- position: absolute;
- width: 22px;
- background-color: #D66166;
- color: white;
- border-radius: 12px;
- top: -20px;
- right: -20px;
- }`;
- var $tidio = '<style type="text/css">' + tidio_close_data.ele_select + ' ' + $css + '</style>';
- $doc.head.insertAdjacentHTML('beforeend', $tidio);
- }
- function checkAndHide() {
- if (localStorage) {
- $stamp = localStorage.getItem(tidio_close_data.key);
- if ($stamp) {
- // if stamp exists and diff < duration, we hide the button.
- $duration_days = 7;
- $duration = $duration_days * (60 * 60 * 24 * 1000);
- if (Date.now() - $stamp < $duration) {
- tidio_close_data.ele.click();
- }
- }
- }
- }
- function onTidioChatApiReady() {
- addCloseButton();
- checkAndHide();
- }
- if (window.tidioChatApi) {
- window.tidioChatApi.on('ready', onTidioChatApiReady);
- } else {
- document.addEventListener('tidioChat-ready', onTidioChatApiReady);
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement