forked from edibudimilic/Keep-Alive-Pro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
102 lines (92 loc) · 2.67 KB
/
background.js
File metadata and controls
102 lines (92 loc) · 2.67 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
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
// set default state to OFF
let extState = 'OFF';
const EXPIRY_IN_MS = 8 * 60 * 60 * 1000; // 8 hours
// on URL change reset the extension state to OFF
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
extState = 'OFF';
chrome.action.setIcon({
path: {
"16": "icons/icon_16.png",
"32": "icons/icon_32.png",
"48": "icons/icon_48.png",
"128": "icons/icon_128.png"
}
});
});
// user clicks on the extension icon
chrome.action.onClicked.addListener(async (tab) => {
extState = extState === 'ON' ? 'OFF' : 'ON';
if (extState === "ON") {
// When user turns the extension on
// add iframe to the current tab
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: () => {
const iframe = document.createElement('iframe');
// use the same URL of the tab to keep the session alive
iframe.src = window.location.href;
iframe.style.cssText = `
position: fixed;
top: 0;
left: 0;
width: 1px;
height: 1px;
border: none;
z-index: 999999;
`;
iframe.name = 'KeepAlivePro';
iframe.dataset.expiry = Date.now() + EXPIRY_IN_MS;
document.body.appendChild(iframe);
}
});
// make the iframe autorefresh every 60 seconds
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: () => {
setInterval(() => {
const iframe = document.querySelector('iframe[name="KeepAlivePro"]');
if (iframe) {
iframe.src = iframe.src;
//If Expired
if (iframe.dataset.expiry < Date.now())
{
clearInterval();
iframe.remove();
extState = 'OFF';
}
}
}, 60000);
}
});
// change the extension icon to green
chrome.action.setIcon({
path: {
"16": "icons/icon_16_on.png",
"32": "icons/icon_32_on.png",
"48": "icons/icon_48_on.png",
"128": "icons/icon_128_on.png"
}
});
} else if (extState === "OFF") {
// When user turns the extension off
// remove iframe 'KeepAlivePro' from the current tab
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: () => {
const iframe = document.querySelector('iframe[name="KeepAlivePro"]');
if (iframe) {
iframe.remove();
}
}
});
// change the extension icon back
chrome.action.setIcon({
path: {
"16": "icons/icon_16.png",
"32": "icons/icon_32.png",
"48": "icons/icon_48.png",
"128": "icons/icon_128.png"
}
});
}
});