Installing widget on selected pages
1
Step 1: Define Included/Excluded Pages
const projectId = "63b711fa2ef2e4001a5e4977";
let attributes = {};
if (
(window.location.href.indexOf('/cds') >= 0) ||
(window.location.href.indexOf('%2Fcds') >= 0) ||
(window.location.href.indexOf('/dashboard') >= 0) ||
(window.location.href.indexOf('%2Fdashboard') >= 0)
) {
// Pages to exclude: do not start the widget
} else if (
((window.location.href.indexOf('/login') >= 0) ||
(window.location.href.indexOf('%2Flogin') >= 0) ||
(window.location.href.indexOf('/signup') >= 0) ||
(window.location.href.indexOf('%signup') >= 0)
) && screen.width < 800
) {
// Also exclude these pages on mobile devices
} else {
// startWidget()
}2
Step 2: Define the startWidget Function
function startWidget(){
window.tiledeskSettings = {
projectid: projectId,
autoStart: true
};
(function (d, s, id) {
var w = window; var d = document; var i = function () { i.c(arguments); };
i.q = []; i.c = function (args) { i.q.push(args); }; w.Tiledesk = i;
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s);
js.id = id; js.async = true; js.src = "https://widget.tiledesk.com/v6/launch.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'tiledesk-jssdk'));
}4
Step 4: Custom Authentication
function customAuth(callback) {
const storedUser = localStorage.getItem('user');
let user = storedUser ? JSON.parse(storedUser) : null;
if (!user) {
callback(null);
return;
}
const remote_support_project_userId = projectId + "_" + user._id;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "https://tiledesk-custom-jwt-authentication.replit.app/tiledeskauth", true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.onreadystatechange = function () {
if (callback && xmlhttp.readyState == 4 && xmlhttp.status == 200 && xmlhttp.responseText) {
callback(xmlhttp.responseText);
}
};
xmlhttp.send("id=" + remote_support_project_userId + "&firstname=" + user.firstname + "&lastname=" + user.lastname + "&email=" + user.email);
}Last updated