-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathoptions.js
206 lines (194 loc) · 6.83 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"use strict";
const defaultOptions = {
shrunkMode: {
value: false
},
onDuplicateTabDetected: {
value: "N"
},
onRemainingTab: {
value: "A"
},
keepTabBasedOnAge: {
value: "O"
},
keepTabWithHttps: {
value: true
},
keepPinnedTab: {
value: true
},
keepTabWithHistory: {
value: false
},
scope: {
value: "C"
},
ignoreHashPart: {
value: false
},
ignoreSearchPart: {
value: false
},
ignorePathPart: {
value: false
},
ignore3w: {
value: false
},
caseInsensitive: {
value: false
},
compareWithTitle: {
value: false
},
onDuplicateTabDetectedPinned: {
value: true
},
tabPriorityPinned: {
value: true
},
matchingRulesPinned: {
value: true
},
scopePinned: {
value: true
},
customizationPinned: {
value: true
},
whiteList: {
value: ""
},
blackList: {
value: ""
},
badgeColorDuplicateTabs: {
value: "#f22121"
},
badgeColorNoDuplicateTabs: {
value: "#1e90ff"
},
showBadgeIfNoDuplicateTabs: {
value: true
},
closePopup: {
value: false
},
environment: {
value: "firefox"
}
};
const setupDefaultOptions = async () => {
const environment = await getEnvironment();
const options = Object.assign({}, defaultOptions);
options.environment.value = environment;
return options;
};
const getEnvironment = async () => {
const info = await getPlatformInfo();
const environment = (info.os === "android") ? "android" : (typeof InstallTrigger !== "undefined") ? "firefox" : "chrome";
return environment;
};
const getNotInReferenceKeys = (referenceKeys, keys) => {
const setKeys = new Set(keys);
return Array.from(referenceKeys).filter(key => !setKeys.has(key));
};
// eslint-disable-next-line no-unused-vars
const initializeOptions = async () => {
const options = await getStoredOptions();
let storedOptions = options.storedOptions;
if (storedOptions.length === 0) {
const intialOptions = await setupDefaultOptions();
storedOptions = await saveStoredOptions(intialOptions);
} else {
const storedKeys = Object.keys(storedOptions).sort();
const defaultKeys = Object.keys(defaultOptions).sort();
if (JSON.stringify(storedKeys) != JSON.stringify(defaultKeys)) {
const obsoleteKeys = getNotInReferenceKeys(storedKeys, defaultKeys);
obsoleteKeys.forEach(key => delete storedOptions[key]);
const missingKeys = getNotInReferenceKeys(defaultKeys, storedKeys);
// eslint-disable-next-line no-return-assign
missingKeys.forEach(key => storedOptions[key] = { value: defaultOptions[key].value });
const environment = await getEnvironment();
storedOptions.environment.value = environment;
storedOptions = await saveStoredOptions(storedOptions, true);
}
}
setOptions(storedOptions);
setEnvironment(storedOptions);
};
// eslint-disable-next-line no-unused-vars
const setStoredOption = async (name, value, refresh) => {
const options = await getStoredOptions();
const storedOptions = options.storedOptions;
storedOptions[name].value = value;
saveStoredOptions(storedOptions);
setOptions(storedOptions);
if (refresh) refreshGlobalDuplicateTabsInfo();
else if (name === "onDuplicateTabDetected") setBadgeIcon();
else if (name === "showBadgeIfNoDuplicateTabs" || name === "badgeColorNoDuplicateTabs" || name === "badgeColorDuplicateTabs") updateBadgeStyle();
};
const options = {};
const setOptions = (storedOptions) => {
options.autoCloseTab = storedOptions.onDuplicateTabDetected.value === "A";
options.defaultTabBehavior = storedOptions.onRemainingTab.value === "B";
options.activateKeptTab = storedOptions.onRemainingTab.value === "A";
options.keepNewerTab = storedOptions.keepTabBasedOnAge.value === "N";
options.keepReloadOlderTab = storedOptions.keepTabBasedOnAge.value === "R";
options.keepTabWithHttps = storedOptions.keepTabWithHttps.value;
options.keepPinnedTab = storedOptions.keepPinnedTab.value;
options.ignoreHashPart = storedOptions.ignoreHashPart.value;
options.ignoreSearchPart = storedOptions.ignoreSearchPart.value;
options.ignorePathPart = storedOptions.ignorePathPart.value;
options.compareWithTitle = storedOptions.compareWithTitle.value;
options.ignore3w = storedOptions.ignore3w.value;
options.caseInsensitive = storedOptions.caseInsensitive.value;
options.searchInAllWindows = storedOptions.scope.value === "A" || storedOptions.scope.value === "CA";
options.searchPerContainer = storedOptions.scope.value === "CC" || storedOptions.scope.value === "CA";
options.whiteList = whiteListToPattern(storedOptions.whiteList.value);
options.badgeColorDuplicateTabs = storedOptions.badgeColorDuplicateTabs.value;
options.badgeColorNoDuplicateTabs = storedOptions.badgeColorNoDuplicateTabs.value;
options.showBadgeIfNoDuplicateTabs = storedOptions.showBadgeIfNoDuplicateTabs.value;
};
const environment = {
isAndroid: false,
isFirefox: false,
isChrome: false
};
const setEnvironment = (storedOptions) => {
if (storedOptions.environment.value === "android") {
environment.isAndroid = true;
environment.isFirefox = false;
} else if (storedOptions.environment.value === "firefox") {
environment.isAndroid = false;
environment.isFirefox = true;
environment.isChrome = false;
}
else if (storedOptions.environment.value === "chrome") {
environment.isAndroid = false;
environment.isFirefox = false;
environment.isChrome = true;
}
};
// eslint-disable-next-line no-unused-vars
const isPanelOptionOpen = () => {
const popups = chrome.extension.getViews({ type: "popup" });
if (popups.length) return true;
const tabs = chrome.extension.getViews({ type: "tab" });
return tabs.length > 0;
};
const whiteListToPattern = (whiteList) => {
const whiteListPatterns = new Set();
const whiteListLines = whiteList.split("\n").map(line => line.trim());
whiteListLines.forEach(whiteListLine => {
const length = whiteListLine.length;
let pattern = "^";
for (let index = 0; index < length; index += 1) {
const character = whiteListLine.charAt(index);
pattern = (character === "*") ? `${pattern}.*` : pattern + character;
}
whiteListPatterns.add(new RegExp(`${pattern}$`));
});
return Array.from(whiteListPatterns);
};