1
|
|
|
<?php namespace GeneaLabs\LaravelCaffeine; |
2
|
|
|
|
3
|
|
|
use Jenssegers\Model\Model; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @property string $html |
7
|
|
|
* @property string $interval |
8
|
|
|
* @property string $url |
9
|
|
|
*/ |
10
|
|
|
class Dripper extends Model |
11
|
|
|
{ |
12
|
|
|
public function getHtmlAttribute() : string |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
return '<script>' |
16
|
|
|
. "let ld = new Date();" |
17
|
|
|
. "function caffeineSendDrip () {" |
18
|
|
|
. " let e = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject('Microsoft.XMLHTTP');" |
19
|
|
|
. " e.onreadystatechange = function () {" |
20
|
|
|
. " if (e.readyState === 4 && e.status === 204) {" |
21
|
|
|
. " ld = new Date();" |
22
|
|
|
. " }" |
23
|
|
|
. " };" |
24
|
|
|
. " e.open('GET', '{$this->url}', !0);" |
25
|
|
|
. " e.setRequestHeader('X-Requested-With', 'XMLHttpRequest');" |
26
|
|
|
. " e.send();" |
27
|
|
|
. "}" |
28
|
|
|
. "setInterval(function () { caffeineSendDrip(); }, $this->interval);" |
29
|
|
|
. "setInterval(function () {" |
30
|
|
|
. " if (new Date() - ld >= $this->interval + $this->threshold) {" |
|
|
|
|
31
|
|
|
. " location.reload(true);" |
32
|
|
|
. " }" |
33
|
|
|
. "}, $this->checkInterval);" |
|
|
|
|
34
|
|
|
. "</script>"; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getIntervalAttribute() : string |
38
|
|
|
{ |
39
|
|
|
return config( |
40
|
|
|
'genealabs-laravel-caffeine.dripIntervalInMilliSeconds', |
41
|
|
|
300000 |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getThresholdAttribute() : int |
46
|
|
|
{ |
47
|
|
|
return config( |
48
|
|
|
'genealabs-laravel-caffeine.thresholdDifference', |
49
|
|
|
10000 |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getCheckIntervalAttribute() : int |
54
|
|
|
{ |
55
|
|
|
return config( |
56
|
|
|
'genealabs-laravel-caffeine.checkLastDripInterval', |
57
|
|
|
2000 |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getUrlAttribute() : string |
62
|
|
|
{ |
63
|
|
|
return trim(config('genealabs-laravel-caffeine.domain', url('/')), '/') |
64
|
|
|
. '/' |
65
|
|
|
. trim(config( |
66
|
|
|
'genealabs-laravel-caffeine.route', |
67
|
|
|
'genealabs/laravel-caffeine/drip' |
68
|
|
|
), '/'); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|