-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsessionbox.js
100 lines (88 loc) · 2.55 KB
/
sessionbox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* SessionBox
*
* Javascript
*
* @author Emmanuel ROECKER
* @license MIT
*
* Created : 18/01/2021
* File : sessionbox.js
*
*/
var SessionBox = (function () {
function SessionBox(cookies) {
if ((cookies === null) || (typeof cookies !== 'object')) {
console.error("Parameters must be an object (Example : {'cookie1':true,'cookie2':false})");
return;
}
var hidden, visibilityChange;
if (typeof document.hidden !== "undefined") {
hidden = "hidden";
visibilityChange = "visibilitychange";
} else if (typeof document.msHidden !== "undefined") {
hidden = "msHidden";
visibilityChange = "msvisibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
hidden = "webkitHidden";
visibilityChange = "webkitvisibilitychange";
}
if (typeof document.addEventListener === "undefined" ||
typeof document.hidden === "undefined" ||
typeof sessionStorage === "undefined") {
console.error("Requires a browser that supports Session Storage and Page Visibility API.");
return;
}
if (Cookies === null) {
console.error('Javacript Cookie library dependancy (https://github.com/js-cookie/js-cookie)');
return;
}
this.prefix = "sessionbox-";
this.cookies = cookies;
if (sessionStorage.getItem(this.prefix + 'closed')) { // a tab cannot be reused if logout
this.restore();
document.location.reload();
}
for (var cookiename in cookies) {
if (cookies[cookiename])
this.save(cookiename);
}
var _this = this;
window.onbeforeunload = function () { // for cookies deleted or expired
_this.restore();
};
document.addEventListener("visibilitychange", function () { // for ajax requests
if (!document.hidden) {
_this.restore();
}
}, false);
this.restore(); // deleted unused cookies
}
SessionBox.prototype.save = function (cookiename) {
if (!(cookiename in this.cookies)) {
console.error(cookiename + " not exist");
return;
}
var sessionid = Cookies.get(cookiename);
if (sessionid)
sessionStorage.setItem(this.prefix + cookiename, sessionid);
};
SessionBox.prototype.close = function () {
sessionStorage.setItem(this.prefix + 'closed', 'true');
for (var cookiename in this.cookies) {
sessionStorage.removeItem(this.prefix + cookiename);
};
this.restore();
};
SessionBox.prototype.restore = function () {
for (var cookiename in this.cookies) {
var sessionid = sessionStorage.getItem(this.prefix + cookiename);
if (sessionid) {
Cookies.set(cookiename, sessionid);
} else {
Cookies.remove(cookiename);
}
};
};
return SessionBox;
})();