-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoptions.js
More file actions
101 lines (90 loc) · 3.37 KB
/
options.js
File metadata and controls
101 lines (90 loc) · 3.37 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
// Populate time zone options
const timeZoneSelect = document.getElementById('timeZone');
Intl.supportedValuesOf('timeZone').forEach(tz => {
const option = document.createElement('option');
option.value = tz;
option.textContent = tz;
timeZoneSelect.appendChild(option);
});
// Validate domain format
function isValidDomain(domain) {
const domainPattern = /^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
return domainPattern.test(domain) || domain.startsWith('chrome://') || domain.startsWith('chrome-extension://');
}
// Show status message
function showStatus(message, isError = false) {
const status = document.getElementById('status');
status.textContent = message;
status.style.color = isError ? '#f44336' : '#4CAF50';
setTimeout(() => {
status.textContent = '';
status.style.color = '';
}, 3000);
}
// Saves options to chrome.storage
function save_options() {
const excludedDomainsText = document.getElementById('excludedDomains').value;
const timeZone = document.getElementById('timeZone').value;
const enableContentAnalysis = document.getElementById('enableContentAnalysis').checked;
// Validate and process excluded domains
const excludedDomains = excludedDomainsText
.split('\n')
.map(s => s.trim())
.filter(Boolean);
// Validate each domain
const invalidDomains = excludedDomains.filter(domain => !isValidDomain(domain));
if (invalidDomains.length > 0) {
showStatus(`Invalid domains: ${invalidDomains.join(', ')}`, true);
return;
}
// Validate timezone
if (!timeZone || !Intl.supportedValuesOf('timeZone').includes(timeZone)) {
showStatus('Invalid timezone selected', true);
return;
}
// Save to storage
chrome.storage.local.set({
config: {
excludedDomains: excludedDomains,
enableContentAnalysis: enableContentAnalysis
},
userTimeZone: timeZone
}, () => {
if (chrome.runtime.lastError) {
showStatus(`Save failed: ${chrome.runtime.lastError.message}`, true);
return;
}
showStatus('Options saved successfully');
// Send message to background script to update config and time zone
chrome.runtime.sendMessage({ action: 'updateConfig', config: {
excludedDomains: excludedDomains,
enableContentAnalysis: enableContentAnalysis
} }, () => {
if (chrome.runtime.lastError) {
console.error('Failed to update config:', chrome.runtime.lastError);
}
});
chrome.runtime.sendMessage({ action: 'updateTimeZone', timeZone: timeZone }, () => {
if (chrome.runtime.lastError) {
console.error('Failed to update timezone:', chrome.runtime.lastError);
}
});
});
}
// Restores select box and checkbox state using the preferences
// stored in chrome.storage.
function restore_options() {
chrome.storage.local.get({
config: {
excludedDomains: [],
enableContentAnalysis: false // Default to false for privacy
},
userTimeZone: 'UTC'
}, (items) => {
document.getElementById('excludedDomains').value = items.config.excludedDomains.join('\n');
document.getElementById('timeZone').value = items.userTimeZone;
document.getElementById('enableContentAnalysis').checked = items.config.enableContentAnalysis || false;
});
}
document.addEventListener('DOMContentLoaded', restore_options);
document.getElementById('save').addEventListener('click', save_options);