Skip to content

Instantly share code, notes, and snippets.

@b4oshany
Last active August 29, 2015 14:16
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 b4oshany/7a5d3e77bbe3dd43b8cc to your computer and use it in GitHub Desktop.
Save b4oshany/7a5d3e77bbe3dd43b8cc to your computer and use it in GitHub Desktop.
Prepopulate PureChat form with the current logged in user.
// JSON data of your current logged in user.
current_user = {"first_name": "Oshane", "last_name": "Bailey", "email": "test@example.com"};
var purechat_user_interval = {"obj": undefined, "num_tries": 0};
// Initiate the PureChat plugin onload (async). In addition, set the user
// name and email as soon as PureChat initiation process is completed.
(function () {
var done = false;
var script = document.createElement('script');
script.async = true;
script.type = 'text/javascript';
script.src = 'https://app.purechat.com/VisitorWidget/WidgetScript';
document.getElementsByTagName('HEAD').item(0).appendChild(script);
script.onreadystatechange = script.onload = function (e) {
if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
var w = new PCWidget({ c: '30b4aa45-1783-4932-94ca-5d66669d41fe', f: true });
done = true;
// Set the user name and email if a user is logged in..
if(current_user != undefined){
// If the user name and email has not been set and the number of retries has not reached its
// limit, try setting the user name and email.
if(purechat_user_interval.num_tries < 60 && purechat_user_interval.obj == undefined){
purechat_user_interval.obj = setInterval(function() {
var purechat_username = document.querySelector("input[name='purechat-name-input']");
var purechat_useremail = document.querySelector("input[name='purechat-email-input']");
if(purechat_username != undefined){
// Set user name and email.
purechat_username.value = (current_user.first_name +" "+current_user.last_name).trim();
purechat_useremail.value = current_user.email.trim();
purechat_username.style.display = "none"
purechat_useremail.style.display = "none"
}
purechat_user_interval.num_tries += 1
}, 1000);
}else{
// if user name and email has been set, then clear the time interval.
if(purechat_user_interval.obj != undefined){
clearInterval(purechat_user_interval.obj);
}
}
}
}
};
})();
@b4oshany
Copy link
Author

It's working now. At first, it was giving me errors due to whitespaces (%20) for the username and email in the URL,
https://app.purechat.com/VisitorWidget/Chat/Test%20/30b4aa45-1783-4932-94ca-5d66669d41fe?origin=http%3A%2F%2Fb4oshany.koding.io%3A8080%2F&visitorEmail=test%40example.com&callback=jQuery1720012312922393903136_1426203589097&_=1426203749670

I trimed the whitespace and it's working.

The format of the url is, https://app.purechat.com/VisitorWidget/Chat/<user_name>/<token>?<query_string>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment