-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
107 lines (100 loc) · 3.12 KB
/
background.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
101
102
103
104
105
106
107
// Default settings
const defaultSettings = {
enabled: false,
action: "Keep Active",
interval: 60,
};
appData = {
siteSettings: [],
};
// Refresh or Keep Active enabled sites at specified intervals
function refreshOrScrollSites(sites) {
sites.forEach(function (site) {
if (site.timerId != 0) {
clearInterval(site.timerId);
site.timerId = 0;
}
if (site.enabled) {
console.log(site.siteHost + " is enabled for " + site.action);
var isDiscardableTab = site.action === "Keep Active" ? false : true;
updateChromeTabsDiscardStatus(isDiscardableTab, site.siteHost);
if (site.action === "Refresh") {
site.timerId = setInterval(function () {
reloadChromeTabByHostname(site.siteHost);
}, site.interval * 1000);
}
} else {
updateChromeTabsDiscardStatus(true, site.siteHost);
}
});
chrome.storage.local.set({ settings: sites }, function () {});
}
function reloadChromeTabByHostname(hostName) {
var query = "*://" + hostName + "/*";
chrome.tabs.query({ url: query }, function (tabs) {
tabs.forEach(function (tab) {
chrome.tabs.reload(tab.id);
});
});
}
function updateChromeTabsDiscardStatus(isDiscardable, hostName) {
var query = "*://" + hostName + "/*";
chrome.tabs.query({ url: query }, function (tabs) {
tabs.forEach(function (tab) {
chrome.tabs.update(tab.id, { autoDiscardable: isDiscardable });
});
});
}
// Retrieve settings from storage
function getSettings(callback) {
chrome.storage.local.get(
{ settings: this.appData.siteSettings },
function (data) {
callback(data);
}
);
}
// Save settings to storage
function saveSettings(settings, callback) {
this.appData.siteSettings = [];
getSettings(function (data) {
if (data && data.settings) {
var oldData = data.settings.find((x) => x.siteHost == settings.siteHost);
if (oldData != undefined) {
var oldTimerId = oldData.timerId;
settings.timerId = oldTimerId;
this.appData.siteSettings = data.settings.filter(
(x) => x.siteHost != settings.siteHost
);
} else {
this.appData.siteSettings = data.settings;
}
this.appData.siteSettings.push(settings);
} else {
this.appData.siteSettings.push(settings);
}
chrome.storage.local.set(
{ settings: this.appData.siteSettings },
function () {
refreshOrScrollSites(this.appData.siteSettings || []);
callback({ success: true, message: "Settings saved successfully" });
}
);
});
}
// Message handler
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.action === "saveSettings") {
saveSettings(request.settings, sendResponse);
return true; // Indicates the response will be sent asynchronously
} else if (request.action === "getSettings") {
getSettings(sendResponse);
return true; // Indicates the response will be sent asynchronously
}
});
// Initialize settings on extension installation
chrome.runtime.onInstalled.addListener(function (details) {
if (details.reason === "install") {
// Need to to on install..
}
});