-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingle-tab.js
More file actions
48 lines (48 loc) · 1.31 KB
/
Copy pathsingle-tab.js
File metadata and controls
48 lines (48 loc) · 1.31 KB
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
const storageKey = 'one-tab-enforcer';
const tabId = Math.random().toString(36).substring(2, 15);
const checkInterval = 1000;
const expirationTime = 5000;
function claimOwnership() {
localStorage.setItem(storageKey, JSON.stringify({
id: tabId,
timestamp: Date.now()
}));
}
function checkOwnership() {
const storedData = localStorage.getItem(storageKey);
if (!storedData) return true;
try {
const { id, timestamp } = JSON.parse(storedData);
const isExpired = Date.now() - timestamp > expirationTime;
if (id !== tabId && !isExpired) {
alert('Already open in another tab');
window.close();
return false;
}
if (isExpired) {
claimOwnership();
}
} catch (e) {
claimOwnership();
}
return true;
}
if (!checkOwnership()) {
window.location.href = "https://www.youtube.com/embed/hiRacdl02w4?autoplay=1"
}
const intervalId = setInterval(() => {
claimOwnership();
}, checkInterval);
window.addEventListener('beforeunload', () => {
localStorage.removeItem(storageKey);
clearInterval(intervalId);
});
window.addEventListener('load', () => {
const storedData = localStorage.getItem(storageKey);
if (storedData) {
const { timestamp } = JSON.parse(storedData);
if (Date.now() - timestamp > expirationTime) {
localStorage.removeItem(storageKey);
}
}
});