-
Notifications
You must be signed in to change notification settings - Fork 2
/
browser_action.js
163 lines (152 loc) · 6.25 KB
/
browser_action.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const filterDomainCheckbox = document.getElementById('filterDomain');
const filterURLCheckbox = document.getElementById('filterURL');
const customFormatCheckbox = document.getElementById('customFormat');
const currentDomainLabel = document.getElementById('currentDomainLabel');
const currentURLLabel = document.getElementById('currentURLLabel');
const customFormatRow = document.getElementById('customFormatRow');
const telLinkFormat = document.getElementById('telLinkFormat');
const linkTextFormat = document.getElementById('linkTextFormat');
const saveButton = document.getElementById('saveButton');
const settingsButton = document.getElementById('settings-button');
let currentTab;
// Entry point
// Get active tab and initialize the popup DOM.
chrome.tabs.query({ active: true, currentWindow: true }, ([activeTab]) => initializeDOM(activeTab));
// Listen for tab switches to update the popup DOM.
chrome.tabs.onActivated.addListener(activeInfo => chrome.tabs.get(activeInfo.tabId, initializeDOM));
// Add popup settings DOM event listeners.
filterURLCheckbox.addEventListener("change", onUrlCheckboxChanged);
filterDomainCheckbox.addEventListener("change", onDomainCheckboxChanged);
customFormatCheckbox.addEventListener("change", onFormatCheckboxChanged);
saveButton.addEventListener("click", onSaveButtonClicked);
settingsButton.addEventListener("click", () => chrome.runtime.openOptionsPage());
/**
* Initialize the pop-up window with the settings for the tab activeTab.
* @param {Object} activeTab The tab to update the pop-up settings for.
*/
function initializeDOM(activeTab) {
currentTab = activeTab;
const domain = encodeURI(activeTab.url.match(regexDomain)[1]);
const url = encodeURI(activeTab.url);
currentDomainLabel.textContent = domain;
currentURLLabel.textContent = activeTab.url;
chrome.storage.local.get({
ignoredDomains: [],
ignoredURLS: [],
useCustom: [],
customTel: [],
customText: []
}, function(settings) {
filterURLCheckbox.checked = settings.ignoredURLS.includes(url);
filterDomainCheckbox.checked = settings.ignoredDomains.includes(domain);
// Update custom format elements.
let customIndex = settings.useCustom.indexOf(domain);
customFormatCheckbox.checked = customIndex > -1;
if (customIndex > -1) {
customFormatRow.style.display = "block";
telLinkFormat.value = settings.customTel[customIndex];
linkTextFormat.value = settings.customText[customIndex];
} else {
customFormatRow.style.display = "none";
}
});
}
/**
* Update the URL settings based on the popup checkbox.
* Runs when the 'URL filter' checkbox is changed.
*/
function onUrlCheckboxChanged() {
const activeTab = currentTab;
const url = encodeURI(activeTab.url);
chrome.storage.local.get({
ignoredURLS: []
}, function(settings) {
let checked = filterURLCheckbox.checked;
let urlIndex = settings.ignoredURLS.indexOf(url);
if (checked && urlIndex < 0)
settings.ignoredURLS.push(url);
else if (!checked && urlIndex > -1)
settings.ignoredURLS.splice(urlIndex, 1);
chrome.storage.local.set({
ignoredURLS: settings.ignoredURLS
}, chrome.tabs.reload);
});
}
/**
* Update the domain settings based on the popup checkbox.
* Runs when the 'domain filter' checkbox is changed.
*/
function onDomainCheckboxChanged() {
const activeTab = currentTab;
const domain = encodeURI(activeTab.url.match(regexDomain)[1]);
chrome.storage.local.get({
ignoredDomains: []
}, function(settings) {
let checked = filterDomainCheckbox.checked;
let domainIndex = settings.ignoredDomains.indexOf(domain);
if (checked && domainIndex < 0)
settings.ignoredDomains.push(domain);
else if (!checked && domainIndex > -1)
settings.ignoredDomains.splice(domainIndex, 1);
chrome.storage.local.set({
ignoredDomains: settings.ignoredDomains
}, chrome.tabs.reload);
});
}
/**
* Update the domain custom format settings based on the popup.
* Runs when the 'custom format' checkbox is changed.
*/
function onFormatCheckboxChanged() {
const activeTab = currentTab;
const domain = encodeURI(activeTab.url.match(regexDomain)[1]);
chrome.storage.local.get({
useCustom: [],
customTel: [],
customText: [],
telLinkFormat: defaultTelFormat,
linkTextFormat: defaultTextFormat
}, function(settings) {
let checked = customFormatCheckbox.checked;
let customIndex = settings.useCustom.indexOf(domain);
if (checked && customIndex < 0) {
customIndex = settings.useCustom.push(domain) - 1;
customFormatRow.style.display = "block";
settings.customTel.splice(customIndex, 0, settings.telLinkFormat);
settings.customText.splice(customIndex, 0, settings.linkTextFormat);
telLinkFormat.value = settings.customTel[customIndex];
linkTextFormat.value = settings.customText[customIndex];
} else if (!checked && customIndex > -1) {
settings.customTel.splice(customIndex, 1);
settings.customText.splice(customIndex, 1);
settings.useCustom.splice(customIndex, 1);
customFormatRow.style.display = "none";
}
chrome.storage.local.set({
useCustom: settings.useCustom,
customTel: settings.customTel,
customText: settings.customText
}, chrome.tabs.reload);
});
}
/**
* Update the domain custom format settings based on the popup.
* Runs when the 'custom format' save button is pressed.
*/
function onSaveButtonClicked() {
const activeTab = currentTab;
const url = encodeURI(activeTab.url);
chrome.storage.local.get({
ignoredURLS: []
}, function(settings) {
let checked = filterURLCheckbox.checked;
let urlIndex = settings.ignoredURLS.indexOf(url);
if (checked && urlIndex < 0)
settings.ignoredURLS.push(url);
else if (!checked && urlIndex > -1)
settings.ignoredURLS.splice(urlIndex, 1);
chrome.storage.local.set({
ignoredURLS: settings.ignoredURLS
}, chrome.tabs.reload);
});
}