Skip to content

Commit

Permalink
Allow to load JS code after some delay
Browse files Browse the repository at this point in the history
  • Loading branch information
marekgach committed Mar 11, 2020
1 parent a8f52d5 commit a9dbb61
Showing 1 changed file with 54 additions and 6 deletions.
60 changes: 54 additions & 6 deletions src/Smartsupp/ChatGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
*/
class ChatGenerator
{
/**
* @var string default number of miliseconds to delay async load
*/
const DEFAULT_ASYNC_DELAY = 1500;

/**
* @var array Values which are allowed for ratingType param.
*/
Expand All @@ -40,6 +45,16 @@ class ChatGenerator
*/
protected $key = null;

/**
* @var bool If chat code should load async via timer delay.
*/
protected $loadAsync = false;

/**
* @var string Amount of miliseconds to delay async load of JS script.
*/
protected $loadAsyncDelay = self::DEFAULT_ASYNC_DELAY;

/**
* @var null|string By default chat conversation is terminated when visitor opens a sub-domain on your website.
*/
Expand Down Expand Up @@ -182,6 +197,26 @@ public function setKey($key)
$this->key = $key;
}

/**
* Allow to load code async after some delay
*
* @param bool $async should load async
*/
public function setAsync($async = true)
{
$this->loadAsync = $async;
}

/**
* Allow to set delay of async load to override default value.
*
* @param int $delay delay in miliseconds
*/
public function setAsyncDelay($delay)
{
$this->loadAsyncDelay = $delay;
}

/**
* Smartsupp visitor is identified by unique key stored in cookies. By default chat conversation is terminated when
* visitor opens a sub-domain on your website. You should set main domain as cookie domain if you want chat
Expand Down Expand Up @@ -453,19 +488,32 @@ public function render($print_out = false)
$params[] = "_smartsupp.hideWidget = true;";
}

// create basic code and append params
$code = "<script type=\"text/javascript\">
var _smartsupp = _smartsupp || {};
_smartsupp.key = '%key%';\n" .
$codeChatCode = "_smartsupp.key = '%key%';\n" .
implode("\n", $params) . "\n" .
"window.smartsupp||(function(d) {
var s,c,o=smartsupp=function(){ o._.push(arguments)};o._=[];
var s,c,o=smartsupp=function(){ o._.push(arguments)};o._=[];
s=d.getElementsByTagName('script')[0];c=d.createElement('script');
c.type='text/javascript';c.charset='utf-8';c.async=true;
c.src='//www.smartsuppchat.com/loader.js';s.parentNode.insertBefore(c,s);
})(document);"
. implode("\n", $params2) . "
. implode("\n", $params2);

// create basic code and append params
if (!$this->loadAsync) {
$code = "<script type=\"text/javascript\">
var _smartsupp = _smartsupp || {};
$codeChatCode
</script>";
} else {
// create code to be load async
$code = "<script type=\"text/javascript\">
setTimeout(function() {
window._smartsupp = window._smartsupp || {};
var _smartsupp = window._smartsupp;
$codeChatCode
}, {$this->loadAsyncDelay});
</script>";
}

$code = str_replace('%key%', self::javascriptEscape($this->key), $code);
$code = str_replace('%cookie_domain%', self::javascriptEscape($this->cookie_domain), $code);
Expand Down

0 comments on commit a9dbb61

Please sign in to comment.