-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
119 lines (102 loc) · 3.79 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
108
109
110
111
112
113
114
115
116
117
118
119
const chrome = window.chrome;
import { createStore, applyMiddleware } from 'redux';
import ReduxThunk from 'redux-thunk';
import { alias, wrapStore } from 'react-chrome-redux';
import cleanIfNewDay from 'Actions/cleanIfNewDay';
import updateCurrentDomain from 'Actions/updateCurrentDomain';
import updateDomainCounts from 'Actions/updateDomainCounts';
import updateDomainProperties from 'Actions/updateDomainProperties';
import rootReducer from 'Reducers/rootReducer';
import { extractDomain, getAllDomains, getActiveTabDomain, findByDomain } from 'Util/domainUtil';
import maybeShowAlert from 'Util/maybeShowAlert';
import { PromiseStorage } from 'Util/promiseStorage';
let store;
// react-chrome-redux uses aliases to dispatch actions
// between the stores on the popup and background scripts
const aliases = {
'update-domain-properties': ({domain, propertiesObj}) => {
store.dispatch(updateDomainProperties(domain, propertiesObj));
// Return blank action to prevent redux errors in the popup.js proxy store
// while the main action runs on the background script
return {type: 'NOOP'};
}
};
async function setupStore(){
let records = await getAllDomains();
store = createStore(
rootReducer,
{
domainList: records
},
applyMiddleware(
alias(aliases),
ReduxThunk
)
);
const activeDomain = await getActiveTabDomain();
await store.dispatch(updateCurrentDomain(activeDomain));
if (activeDomain !== 'newtab'){
store.dispatch(updateDomainCounts(activeDomain));
}
wrapStore(store, {portName: 'FLOWFOCUS_APP'});
const currentDay = new Date().getDate();
const { lastDayUpdated } = await PromiseStorage.get('lastDayUpdated');
if (currentDay !== lastDayUpdated) {
store.dispatch(cleanIfNewDay(currentDay));
}
}
setupStore();
async function updateDomainStats(newDomain){
await store.dispatch(updateCurrentDomain(newDomain));
if (newDomain !== 'newtab'){
const {records} = await store.dispatch(updateDomainCounts(newDomain));
const domainObj = findByDomain(records, newDomain);
maybeShowAlert(domainObj);
return domainObj;
}
return {};
}
function updateBadgeCount(domainObj){
if (domainObj && !domainObj.muted && domainObj.count > 0){
return chrome.browserAction.setBadgeText({text: String(domainObj.count)});
}
return chrome.browserAction.setBadgeText({text: ''});
}
async function checkIfShouldCleanStore(){
// Check if this is a new day and reset the store if so
const currentDay = new Date().getDate();
const {lastDayUpdated} = await PromiseStorage.get('lastDayUpdated');
if (currentDay !== lastDayUpdated) {
await store.dispatch(cleanIfNewDay(currentDay));
}
}
// New site visit
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.url){
const newDomain = extractDomain(tab.url);
chrome.storage.sync.get(['currentDomain', 'lastDayUpdated'], async function(result){
if (newDomain !== result.currentDomain) {
await checkIfShouldCleanStore();
const domainObj = await updateDomainStats(newDomain);
updateBadgeCount(domainObj);
}
});
}
});
// Tab switch
chrome.tabs.onActivated.addListener(activeInfo => {
chrome.tabs.get(activeInfo.tabId, async (tab) => {
const domain = extractDomain(tab.url);
await store.dispatch(updateCurrentDomain(domain));
await checkIfShouldCleanStore();
const {domainList} = await PromiseStorage.get('domainList');
const domainObj = findByDomain(domainList, domain);
updateBadgeCount(domainObj);
});
});
chrome.notifications.onButtonClicked.addListener(async (notificationId, buttonIdx) => {
if (notificationId === 'maxVisitsReached' && buttonIdx === 0){
const {currentDomain} = await PromiseStorage.get('currentDomain');
store.dispatch(updateDomainProperties(currentDomain, {isMuted: true}));
}
});