Skip to content

Commit 5657744

Browse files
authored
Create Duplicate.js
1 parent 0d66a13 commit 5657744

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

Duplicate.js

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
jQuery plugin to check if current window is duplicate window
3+
BASED ON https://stackoverflow.com/a/45717724/223752
4+
*/
5+
6+
(function ($) {
7+
$.fn.DuplicateWindow = function () {
8+
var localStorageTimeout = (5) * 1000; // 15,000 milliseconds = 15 seconds.
9+
var localStorageResetInterval = (1/2) * 1000; // 10,000 milliseconds = 10 seconds.
10+
var localStorageTabKey = 'my-application-browser-tab';
11+
var sessionStorageGuidKey = 'browser-tab-guid';
12+
13+
var ItemType = {
14+
Session: 1,
15+
Local: 2
16+
};
17+
18+
function setCookie(name, value, days) {
19+
var expires = "";
20+
if (days) {
21+
var date = new Date();
22+
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
23+
expires = "; expires=" + date.toUTCString();
24+
}
25+
document.cookie = name + "=" + (value || "") + expires + "; path=/";
26+
}
27+
function getCookie(name) {
28+
var nameEQ = name + "=";
29+
var ca = document.cookie.split(';');
30+
for (var i = 0; i < ca.length; i++) {
31+
var c = ca[i];
32+
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
33+
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
34+
}
35+
return null;
36+
}
37+
38+
function GetItem(itemtype) {
39+
var val = "";
40+
switch (itemtype) {
41+
case ItemType.Session:
42+
val = window.name;
43+
break;
44+
case ItemType.Local:
45+
val = decodeURIComponent(getCookie(localStorageTabKey));
46+
if (val == undefined)
47+
val = "";
48+
break;
49+
}
50+
return val;
51+
}
52+
53+
function SetItem(itemtype, val) {
54+
switch (itemtype) {
55+
case ItemType.Session:
56+
window.name = val;
57+
break;
58+
case ItemType.Local:
59+
setCookie(localStorageTabKey, val);
60+
break;
61+
}
62+
}
63+
64+
function createGUID() {
65+
this.s4 = function () {
66+
return Math.floor((1 + Math.random()) * 0x10000)
67+
.toString(16)
68+
.substring(1);
69+
};
70+
return this.s4() + this.s4() + '-' + this.s4() + '-' + this.s4() + '-' + this.s4() + '-' + this.s4() + this.s4() + this.s4();
71+
}
72+
73+
/**
74+
* Compare our tab identifier associated with this session (particular tab)
75+
* with that of one that is in window name Storage (the active one for this browser).
76+
* This browser tab is good if any of the following are true:
77+
* 1. There is no cookie Storage Guid yet (first browser tab).
78+
* 2. The window name Storage Guid matches the cookie Guid. Same tab, refreshed.
79+
* 3. The window name Storage timeout period has ended.
80+
*
81+
* If our current session is the correct active one, an interval will continue
82+
* to re-insert the window name Storage value with an updated timestamp.
83+
*
84+
* Another thing, that should be done (so you can open a tab within 15 seconds of closing it) would be to do the following (or hook onto an existing onunload method):
85+
* window.onunload = () => {
86+
localStorage.removeItem(localStorageTabKey);
87+
};
88+
*/
89+
function TestIfDuplicate() {
90+
//console.log("In testTab");
91+
var sessionGuid = GetItem(ItemType.Session) || createGUID();
92+
SetItem(ItemType.Session, sessionGuid);
93+
94+
var val = GetItem(ItemType.Local);
95+
var tabObj = (val == "" ? null : JSON.parse(val)) || null;
96+
console.log(val);
97+
console.log(sessionGuid);
98+
console.log(tabObj);
99+
100+
// If no or stale tab object, our session is the winner. If the guid matches, ours is still the winner
101+
if (tabObj === null || (tabObj.timestamp < (new Date().getTime() - localStorageTimeout)) || tabObj.guid === sessionGuid) {
102+
function setTabObj() {
103+
//console.log("In setTabObj");
104+
var newTabObj = {
105+
guid: sessionGuid,
106+
timestamp: new Date().getTime()
107+
};
108+
SetItem(ItemType.Local, JSON.stringify(newTabObj));
109+
}
110+
setTabObj();
111+
setInterval(setTabObj, localStorageResetInterval);//every x interval refresh timestamp in cookie
112+
return false;
113+
} else {
114+
// An active tab is already open that does not match our session guid.
115+
return true;
116+
}
117+
}
118+
119+
window.IsDuplicate = function () {
120+
var duplicate = TestIfDuplicate();
121+
//console.log("Is Duplicate: "+ duplicate);
122+
return duplicate;
123+
};
124+
125+
$(window).on("beforeunload", function () {
126+
if (TestIfDuplicate() == false) {
127+
SetItem(ItemType.Local, "");
128+
}
129+
})
130+
}
131+
$(window).DuplicateWindow();
132+
}(jQuery));

0 commit comments

Comments
 (0)