-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoptions.js
111 lines (99 loc) · 3.65 KB
/
options.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
const init = (function init({ utils, enums, constants }) {
return () => {
let configureUI;
const objectifyForm = (form) => {
const hash = utils.formToHash(form, {
suspendSwitch: input => input.checked,
focusSwitch: input => input.checked,
});
hash.expiration = hash.suspendSwitch ?
utils.get2amTomorrowInMsFrom(new Date()) :
0;
return hash;
};
const savePrefsForHostname = (hostname, prefsForm) => (e) => {
e.preventDefault();
const [subdomain, domain] = hostname;
utils.sendMessage(enums.SAVE_OPTIONS_REQUESTED, {
hostname,
prefs: prefsForm ? objectifyForm(prefsForm) : utils.getDefaultPrefs(),
}, ({ payload }) => {
const settings = {
hostname,
prefs: utils.getPrefs(payload, subdomain, domain),
};
configureUI(settings);
utils.updateIcon(settings);
});
};
const disableForHostname = hostname => () => {
const [subdomain, domain] = hostname;
utils.sendMessage(enums.DISABLE_REQUESTED, { hostname },
({ payload }) => {
const settings = {
hostname,
prefs: payload[domain][subdomain],
};
configureUI(settings);
utils.updateIcon(settings);
});
};
configureUI = (settings) => {
const { hostname, prefs } = settings;
const title = document.querySelector('#hostname');
title.textContent = hostname.filter(x => x !== '*').join('.');
const optionsForm = document.querySelector('#optionsForm');
const button = document.querySelector('#optBtn');
if (prefs && prefs.enabled) {
utils.setFormValues(optionsForm, prefs, {
suspendSwitch: (input) => {
const status = document.querySelector('.status');
input.checked = utils.isSleepTimerEnabled(prefs.expiration, Date.now());
if (input.checked) {
status.textContent = `Extension will resume at ${utils.getReadableTimeFrom(prefs.expiration)}`;
} else {
status.textContent = '';
}
},
focusSwitch: (input) => {
input.checked = prefs.focusSwitch;
},
});
optionsForm.addEventListener('change', savePrefsForHostname(hostname, optionsForm), { once: true });
button.addEventListener('click', disableForHostname(hostname), { once: true });
button.textContent = constants.DISABLE_BUTTON_TEXT;
optionsForm.classList.remove('hidden');
} else {
optionsForm.classList.add('hidden');
button.addEventListener('click', savePrefsForHostname(hostname, optionsForm), { once: true });
button.textContent = constants.ENABLE_BUTTON_TEXT;
}
return settings;
};
Promise.all([utils.getOptions(), utils.getActiveTabInCurrentWindow()])
.then(([options, activeTab]) => {
const hostname = utils.getSubDomainOfUrl(activeTab.url);
const [subdomain, domain] = hostname;
return {
hostname,
prefs: utils.getPrefs(options, subdomain, domain),
};
})
.then(configureUI)
.then(utils.updateIcon)
.then(() => {
const sendFeedbackLinkClicked = () => {
utils.getActiveTabInCurrentWindow()
.then(tab => chrome.tabs.sendMessage(tab.id, {
type: enums.FEEDBACK_CLICKED,
payload: true,
}));
};
document
.getElementById('feedbackLink')
.addEventListener('click', sendFeedbackLinkClicked);
})
.catch(console.log);
};
}(window));
document.addEventListener('DOMContentLoaded', init);