-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathoptions.js
274 lines (254 loc) · 10.5 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* global chrome, analytics */
const window_title = document.getElementById('appTxtSettings'),
alertEl = document.getElementById('alert'),
formUrl = document.getElementById('request-url-form'),
inputUrl = document.getElementById('input-request-url'),
buttonUrlSave = document.getElementById('request-url-form').getElementsByTagName('button')[0],
txtUrlHelpText = document.getElementById('url-help-text'),
checkboxImproveByTracking = document.getElementById('improve-by-tracking'),
txtTrackingHelpText = document.getElementById('tracking-help-text'),
resizeOption = document.getElementById('resize-option'),
stayOnTopOption = document.getElementById('stay-on-top-option'),
disappearTimeoutOption = document.getElementById('titlebartimeout-option'),
disappearTimeoutValue = document.getElementById('titlebartimeout-option-value'),
localeObjects = document.querySelectorAll('.locale'),
versionNumber = document.getElementById('version-number'),
locale_appAlertSomethingWrong = chrome.i18n.getMessage('appAlertSomethingWrong'),
validUrlCharacters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&\'()*+,;=';
// Set locale texts
localeObjects.forEach(function(locale){ locale.innerText = chrome.i18n.getMessage(locale.id); });
// Exception for element who can't use .locale
document.title = chrome.i18n.getMessage('appLabelSettings');
window_title.innerText = chrome.i18n.getMessage('appLabelSettings');
if (inputUrl) inputUrl.placeholder = chrome.i18n.getMessage('appPlaceholderUrl');
// add version number
versionNumber.innerText = chrome.runtime.getManifest().version;
window.alert = function(message, className) {
if(typeof alertEl === 'undefined')
var alertEl = document.getElementById('alert');
if(typeof message === 'undefined' || message === ''){
alertEl.hidden = true;
} else {
alertEl.innerHTML = message;
alertEl.className = (typeof className!=='undefined' && className!=='' ? className : '');
alertEl.hidden = false;
}
return;
}
// Fill default data
chrome.storage.sync.get(function(items) {
if (items.url !== undefined && items.url !== '')
inputUrl.value = items.url;
if (items.dontresize !== undefined)
resizeOption.checked = !items.dontresize;
else
resizeOption.checked = true;
if (items.stayontop !== undefined)
stayOnTopOption.checked = items.stayontop;
else
stayOnTopOption.checked = true;
if (items.titlebartimeout !== undefined){
disappearTimeoutOption.value = items.titlebartimeout;
disappearTimeoutValue.innerText = disappearTimeoutOption.value;
} else {
disappearTimeoutOption.value = 1500;
disappearTimeoutValue.innerText = disappearTimeoutOption.value;
}
});
// redirect focus
inputUrl.addEventListener('focus', function(e) {
this.select();
});
// Validate URL
function validURL(str) {
// Validating scheme:[//[user:password@]host[:port]]path[?query][#fragment]
const pattern = new RegExp(
'^'+ // start
'(https?:\\/\\/)?'+ // scheme
'(\\w+:\\w+@)?'+ // user:password
'('+
'(([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
'([\\d\\w-_~][^.]+)|'+ // OR custom hosts domain
'((\\d{1,3}\\.){3}\\d{1,3})'+ // OR ip (v4) address
')'+
'(\\:\\d+)?'+ // port
'(\\/[-a-z\\d%_.~+]*)*'+ // path
'(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
'(\\#[-a-z\\d&%_.~+=]*)?'+ // fragment locator
'$' // end
,'is');
return !!pattern.test(str);
}
function validateEnteredUrl(elm, event) {
const eventCheck = (event == 'keyup' && !Boolean(txtUrlHelpText.innerText))
if (!validURL(elm.value)) {
if (!eventCheck) {
txtUrlHelpText.innerText = chrome.i18n.getMessage('appAlertWrongUrl');
txtUrlHelpText.style.backgroundColor = "#fff3c6";
buttonUrlSave.disabled = true;
Boolean(elm.value) && elm.focus();
}
} else {
txtUrlHelpText.innerText = '';
txtUrlHelpText.style.backgroundColor = null;
buttonUrlSave.disabled = false;
}
return buttonUrlSave.disabled;
}
inputUrl.addEventListener('blur', function(e) {
this.value = autofixUrl(this.value);
return validateEnteredUrl(this);
});
inputUrl.addEventListener('keyup', function(e) {
return validateEnteredUrl(this, 'keyup');
});
// Try to auto fix url
function autofixUrl(str) {
const splitted = str.split('//');
if (splitted.length != 2) {
str = 'https://' + str;
}
return str;
}
inputUrl.addEventListener('keypress', function(e) {
// Block invalid characters
if (e.key != "Enter" && validUrlCharacters.indexOf(String.fromCharCode(e.which)) < 0)
e.preventDefault();
});
formUrl.addEventListener("submit", function(e){
e.preventDefault();
var newUrl = inputUrl.value;
window.tracker.sendEvent('Browser', 'Set URL', newUrl);
chrome.storage.sync.set({ url: newUrl });
chrome.storage.sync.get(function(items) {
if (items.url === undefined || items.url === '') {
var AlertSomethingWrong = analytics.EventBuilder.builder()
.category('Errors')
.action('Alert')
.dimension(1, 'Something went wrong');
window.tracker.send(AlertSomethingWrong);
alert(locale_appAlertSomethingWrong);
}
else {
chrome.runtime.sendMessage({'open': 'window'});
chrome.runtime.sendMessage({'close': 'options'});
}
});
});
resizeOption.onchange = function() {
let resizeOptionSet, option;
if(resizeOption.checked) {
resizeOptionSet = analytics.EventBuilder.builder()
.category('App')
.action('Switch resize webview')
.dimension(1, 'on');
option = false;
} else {
resizeOptionSet = analytics.EventBuilder.builder()
.category('App')
.action('Switch resize webview')
.dimension(2, 'off');
option = true;
}
window.tracker.send(resizeOptionSet);
chrome.storage.sync.set({ dontresize: option });
chrome.runtime.sendMessage({'dontresize': option});
};
stayOnTopOption.onchange = function() {
let stayOnTopOptionSet, option;
if(stayOnTopOption.checked) {
stayOnTopOptionSet = analytics.EventBuilder.builder()
.category('App')
.action('Switch stay on top')
.dimension(1, 'on');
option = true;
} else {
stayOnTopOptionSet = analytics.EventBuilder.builder()
.category('App')
.action('Switch stay on top')
.dimension(2, 'off');
option = false;
}
window.tracker.send(stayOnTopOptionSet);
chrome.storage.sync.set({ stayontop: option });
};
disappearTimeoutOption.oninput = function() {
disappearTimeoutValue.innerText = this.value;
};
disappearTimeoutOption.onchange = function() {
chrome.storage.sync.set({ titlebartimeout: this.value });
chrome.runtime.sendMessage({'titlebartimeout': this.value});
disappearTimeoutValue.innerText = this.value;
const trackOptionSet = analytics.EventBuilder.builder()
.category('App')
.action('Set titlebar timeout')
.dimension(1, disappearTimeoutValue.innerText);
window.tracker.send(trackOptionSet);
};
// Allow opt-out
function initTrackingSettings(config) {
// Release loading text
document.getElementById('improve-by-tracking-opt-in').hidden = false;
checkboxImproveByTracking.checked = config.isTrackingPermitted();
checkboxImproveByTracking.onchange = function() {
let trackOptionSet;
if (checkboxImproveByTracking.checked) {
trackOptionSet = analytics.EventBuilder.builder()
.category('App')
.action('Switch Tracking')
.dimension(1, 'on');
console.info('%c' + chrome.i18n.getMessage('appMiscYouAreAmazing'), 'font-size:20px;background-color:#d6ff97');
txtTrackingHelpText.innerText = chrome.i18n.getMessage('appMiscYouAreAmazing');
txtTrackingHelpText.style.backgroundColor = "#d6ff97";
} else {
trackOptionSet = analytics.EventBuilder.builder()
.category('App')
.action('Switch Tracking')
.dimension(2, 'off');
console.info('%c' + chrome.i18n.getMessage('appMiscConsiderTracking'), 'font-size:20px;background-color:#fff3c6');
txtTrackingHelpText.innerText = chrome.i18n.getMessage('appMiscConsiderTracking');
txtTrackingHelpText.style.backgroundColor = "#fff3c6";
}
window.tracker.send(trackOptionSet);
config.setTrackingPermitted(checkboxImproveByTracking.checked);
};
}
// Learn and improve from app usage
window.addEventListener('load', function() {
// Initialize the Analytics service object with the name of your app.
const service = analytics.getService('cornips_fbw');
service.getConfig().addCallback(initTrackingSettings);
// Get a Tracker using your Google Analytics app Tracking ID.
window.tracker = service.getTracker('UA-84858849-3');
// Record an "appView" each time the user launches the app
window.tracker.sendAppView('OptionsView');
// Track locale
var locale = chrome.i18n.getUILanguage();
var InitLanguage;
switch(locale) {
case 'pl':
InitLanguage = analytics.EventBuilder.builder()
.category('Language')
.action('OptionsView')
.dimension(4, 'Polish');
break;
case 'de':
InitLanguage = analytics.EventBuilder.builder()
.category('Language')
.action('OptionsView')
.dimension(3, 'German');
break;
case 'nl':
InitLanguage = analytics.EventBuilder.builder()
.category('Language')
.action('OptionsView')
.dimension(2, 'Dutch');
break;
default:
InitLanguage = analytics.EventBuilder.builder()
.category('Language')
.action('OptionsView')
.dimension(1, 'English');
}
window.tracker.send(InitLanguage);
});