-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
240 lines (216 loc) · 7.45 KB
/
background.js
File metadata and controls
240 lines (216 loc) · 7.45 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
Made with <3 by Tabernacle8
This project is on github! :)
https://github.com/tabernacle8/AutoReloadTab
*/
//Checks to see if we need to start reloading the tab
function refreshData() {
//console.log("refresh")
//Get refreshing true or false data
chrome.storage.sync.get(["refreshing"], function (result) {
for (let data of Object.keys(result)) {
//console.log(result)
if (result[data] == 1) {
//Let's start refreshing!
beginReloading();
} else {
//Do not refresh or stop refreshing!
setTimeout(refreshData, 1000);
}
}
});
}
/*
nextReload: Time until the page reloads
reloadSeconds: Time in seconds between each reload
refreshing: 0 if not refreshing, 1 if refreshing
tabid: id of tab to refresh
*/
chrome.runtime.onInstalled.addListener(function () {
chrome.storage.sync.set({
reloadSeconds: "10",
},
function () {
//null
}
);
chrome.storage.sync.set({
reloadMinutes: "0",
},
function () {
//null
}
);
chrome.storage.sync.set({
reloadHours: "0",
},
function () {
//null
}
);
chrome.storage.sync.set({
refreshing: "0",
},
function () {
//null
}
);
chrome.storage.sync.set({
tabid: "0",
},
function () {
//null
}
);
chrome.storage.local.set({
nextReload: "10",
},
function () {
//null
}
);
chrome.storage.local.set({
//Current time
experimentalGlobalTime: new Date().getTime() / 1000,
},
function () {
//null
}
);
console.log("Thanks for installing! First time setup is complete");
});
//Establish extention for all tabs
//Runs only on first time setup
chrome.runtime.onInstalled.addListener(function () {
chrome.declarativeContent.onPageChanged.removeRules(undefined, function () {
chrome.declarativeContent.onPageChanged.addRules([{
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: {
//Here is the tab whitelist, currently it is empty
urlMatches: ".",
},
}),
],
actions: [new chrome.declarativeContent.ShowPageAction()],
}, ]);
});
console.log("Hook with whitelist is done");
console.log(
"Everything is done! Booting up the main background script in 5 seconds..."
);
});
//Listen for alarm from popup
chrome.alarms.onAlarm.addListener(function (alarm) {
var refreshing = 0;
console.log("Alarm triggered");
if (alarm.name == "reload") {
//console.log("reload")
//Get the ID of the tab that must be reloaded
chrome.storage.sync.get(["tabid"], function (result) {
for (let data of Object.keys(result)) {
tabId = result[data];
}
//Reload the tab we want, from memory
chrome.storage.sync.get(["refreshing"], function (result) {
for (let data of Object.keys(result)) {
refreshing = result[data];
}
if (refreshing == 1) {
console.log("Reloading!");
chrome.tabs.reload(tabId);
restartAlarm();
}
});
});
}
});
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.command === "restartAlarm") {
// Call your restartAlarm function here.
restartAlarm();
sendResponse({message: "Alarm restarted!"});
}
});
let reloadInterval;
function restartAlarm() {
console.log("Restarting alarm");
// Cancel any existing interval
if (reloadInterval) {
clearInterval(reloadInterval);
reloadInterval = null;
}
chrome.alarms.clearAll(function() {
console.log("All previous alarms cleared");
});
chrome.storage.local.get(["nextReload"], function (result) {
for (let data of Object.keys(result)) {
const nextReload = parseInt(result[data]);
// Ensure nextReload is a positive integer
if (!Number.isInteger(nextReload) || nextReload <= 0) {
console.error(`Invalid nextReload value: ${nextReload}`);
return;
}
// Decide whether to use setInterval or alarms based on the reload interval
if (nextReload < 60) {
// Schedule the next reload with setInterval
reloadInterval = setInterval(function() {
// Before reloading, check if 'refreshing' is still 1
//Get tab id from storage
chrome.storage.sync.get(["tabid"], function (result) {
for (let data of Object.keys(result)) {
tabId = result[data];
}
chrome.storage.sync.get(["refreshing"], function (result) {
if (result.refreshing == 1) {
console.log("Reloading from setInterval");
chrome.tabs.reload(tabId);
updateGlobalTime(nextReload);
} else {
console.log("Refreshing is not enabled. Stopping reloads.");
clearInterval(reloadInterval);
reloadInterval = null;
}
});
});
}, nextReload * 1000); // Convert to milliseconds
} else {
//Get tab id from storage and reload
chrome.storage.sync.get(["tabid"], function (result) {
for (let data of Object.keys(result)) {
tabId = result[data];
}
chrome.storage.sync.get(["refreshing"], function (result) {
if (result.refreshing == 1) {
console.log("Reloading from alarms");
chrome.tabs.reload(tabId);
updateGlobalTime(nextReload);
} else {
console.log("Refreshing is not enabled. Stopping reloads.");
clearInterval(reloadInterval);
reloadInterval = null;
}
});
});
// Schedule the next reload with alarms
chrome.alarms.create("reload", {
delayInMinutes: nextReload / 60,
});
updateGlobalTime(nextReload);
}
}
});
}
// Function to update experimentalGlobalTime
function updateGlobalTime(nextReload) {
// Get current time in second form
var currentTime = new Date().getTime() / 1000;
// Calculate the time until the next reload
var globalNextReload = currentTime + nextReload;
// Update the experimentalGlobalTime
chrome.storage.sync.set({
experimentalGlobalTime: `${globalNextReload}`,
}, function () {
console.log("Updated experimental global time to " + globalNextReload);
});
}