Skip to content

Commit 6676ba8

Browse files
committed
Ensured global state is preserved when background page is unloaded
1 parent 5d7ba58 commit 6676ba8

File tree

1 file changed

+55
-17
lines changed

1 file changed

+55
-17
lines changed

browser-extension/background.js

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222
* • totalRedacted {count: int}
2323
*/
2424

25-
var isCensoringActive = false;
25+
///// CONFIG /////
26+
27+
var state = {
28+
isCensoringActive: false
29+
};
2630

2731
var colors = {
2832
good: 'hsl(126, 93%, 33%)',
@@ -35,6 +39,9 @@ var actionTitle = {
3539
inactive: 'Censor all emoji'
3640
};
3741

42+
43+
///// DEBUG LOGGING /////
44+
3845
function pad(n) {
3946
return ('00' + n).substr(-2);
4047
}
@@ -51,6 +58,9 @@ function log(...args) {
5158
console.log(...args);
5259
}
5360

61+
62+
///// ICON BADGING /////
63+
5464
function getCountText(count) {
5565
if (count <= 999) {
5666
return count.toString();
@@ -76,36 +86,43 @@ function setBadgeColor(tabId, color) {
7686
function setTotalCount(tabId, count) {
7787
log('setTotalCount', tabId, count);
7888
setBadgeColor(tabId, colors.good);
79-
setBadgeCount(tabId, isCensoringActive ? count : '');
89+
setBadgeCount(tabId, state.isCensoringActive ? count : '');
8090
}
8191

92+
93+
///// LIFECYCLE /////
94+
8295
function ensureTabStatus(tabId) {
8396
log('ensureTabStatus', tabId);
8497
chrome.tabs.sendMessage(tabId, {
85-
msg: isCensoringActive ? 'setActive' : 'setInactive'
98+
msg: state.isCensoringActive ? 'setActive' : 'setInactive'
8699
});
87100
}
88101

89-
function toggleStatus() {
90-
isCensoringActive = !isCensoringActive;
91-
log('toggleStatus, active =', isCensoringActive);
92-
chrome.tabs.query({ active: true }, function (tabs) {
93-
tabs.forEach(function (tab) {
94-
ensureTabStatus(tab.id);
95-
});
96-
});
97-
chrome.browserAction.setTitle({
98-
title: actionTitle[isCensoringActive ? 'active' : 'inactive']
99-
});
102+
function saveState() {
103+
log('saveState', state);
104+
localStorage.setItem('state', JSON.stringify(state));
105+
}
106+
107+
function restoreState() {
108+
try {
109+
var savedState = JSON.parse(localStorage.getItem('state'));
110+
log('restoreState', savedState);
111+
if (savedState) {
112+
state = savedState;
113+
}
114+
} catch (e) {
115+
log('restoreState FAILED, state =', state);
116+
}
100117
}
101118

102119
function contentScriptMessageHandler(request, sender, sendResponse) {
103120
if (sender.tab && request.msg) {
104121
log('%cruntime.onMessage', 'color:green;font-weight:bold', request, sender);
105122
switch (request.msg) {
106123
case 'isGlobalActive':
107-
log(' [response]:', isCensoringActive);
108-
sendResponse({ isActive: isCensoringActive });
124+
log(' [response]:', state.isCensoringActive);
125+
sendResponse({ isActive: state.isCensoringActive });
109126
break;
110127
case 'totalRedacted':
111128
setTotalCount(sender.tab.id, request.count);
@@ -114,8 +131,29 @@ function contentScriptMessageHandler(request, sender, sendResponse) {
114131
}
115132
}
116133

117-
chrome.browserAction.onClicked.addListener(toggleStatus);
118134
chrome.runtime.onMessage.addListener(contentScriptMessageHandler);
135+
chrome.runtime.onSuspend.addListener(saveState);
119136
chrome.tabs.onActivated.addListener(function (activeInfo) {
120137
ensureTabStatus(activeInfo.tabId);
121138
});
139+
140+
log('--- event page loaded ---');
141+
restoreState();
142+
143+
144+
///// USER ACTIONS /////
145+
146+
function toggleStatus() {
147+
state.isCensoringActive = !state.isCensoringActive;
148+
log('toggleStatus, active =', state.isCensoringActive);
149+
chrome.tabs.query({ active: true }, function (tabs) {
150+
tabs.forEach(function (tab) {
151+
ensureTabStatus(tab.id);
152+
});
153+
});
154+
chrome.browserAction.setTitle({
155+
title: actionTitle[state.isCensoringActive ? 'active' : 'inactive']
156+
});
157+
}
158+
159+
chrome.browserAction.onClicked.addListener(toggleStatus);

0 commit comments

Comments
 (0)