Completed
Push — master ( ce292f...c48f51 )
by Sander
14s
created

$(document).ready   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
c 3
b 0
f 0
nc 1
nop 0
dl 0
loc 50
rs 9.3333
1
$(document).ready(function () {
2
    var _this = this;
3
    var storage = new API.Storage();
4
    var runtimeSettings = {};
5
6
    API.runtime.sendMessage(API.runtime.id, {'method': 'getRuntimeSettings'}).then(function (settings) {
7
        var accounts = settings.accounts;
8
        runtimeSettings = settings;
9
        for(var i = 0; i < accounts.length; i++) {
10
            $('#savepw-vault').append('<option value=' + i + '>' + accounts[i].vault.name + '</option>');
11
        }
12
        storage.get('activeTab').then(function (name) {
13
            if (name && name !== '') {
14
                makeTabActive(name);
15
                API.runtime.sendMessage(API.runtime.id, {method: "getActiveTab", args: {returnFn: "returnActiveTab"}});
16
            }
17
        });
18
19
    });
20
21
    $('[t]').each(function () {
22
        var string = $(this).attr('t');
23
        var startChar = string[0];
24
        var endChar = string[string.length - 1];
25
        var attribute;
26
        if (startChar === '[' && endChar === ']') {
27
            var data = string.replace('[', '').replace(']', '').split(',');
28
            attribute = data[1].trim();
29
            string = data[0].trim();
30
        }
31
        var translated = API.i18n.getMessage(string);
32
        if (attribute) {
33
            $(this).attr(attribute, translated);
34
        } else {
35
            $(this).text(translated);
36
        }
37
    });
38
39
    function fillLogin(login) {
40
        API.runtime.sendMessage(API.runtime.id, {
41
            method: 'passToParent',
42
            args: {
43
                injectMethod: 'enterLoginDetails',
44
                args: login
45
            }
46
        }).then(function () {
47
            removePasswordPicker();
48
        });
49
    }
50
51
    function removePasswordPicker(login) {
0 ignored issues
show
Unused Code introduced by brantje
The parameter login is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
52
        API.runtime.sendMessage(API.runtime.id, {
53
            method: 'passToParent',
54
            args: {
55
                injectMethod: 'removePasswordPicker'
56
            }
57
        });
58
    }
59
60
    function copyTextToClipboard(text) {
61
        var copyFrom = document.createElement("textarea");
62
        copyFrom.textContent = text;
63
        var body = document.getElementsByTagName('body')[0];
64
        body.appendChild(copyFrom);
65
        copyFrom.select();
66
        document.execCommand('copy');
67
        body.removeChild(copyFrom);
68
    }
69
70
    _this.copyTextToClipboard = copyTextToClipboard;
71
72
73
    function setupAddCredentialFields() {
74
        var labelfield = $('#savepw-label');
75
        labelfield.val(document.title);
76
        var userfield = $('#savepw-username');
77
        var pwfield = $('#savepw-password');
78
        var vaultfield = $('#savepw-vault');
79
        $('.togglePw').click(function () {
80
            $('.togglePw').find('.fa').toggleClass('fa-eye').toggleClass('fa-eye-slash');
81
            if (pwfield.attr('type') === 'password') {
82
                pwfield.attr('type', 'text');
83
            } else {
84
                pwfield.attr('type', 'password');
85
            }
86
        });
87
88
        $('#savepw-save').click(function (e) {
89
            var fields = [labelfield, pwfield];
90
            var hasErrors = false;
91
            $.each(fields, function (k, field) {
92
               field.removeClass('error');
93
                if(!$(field).val()){
94
                   field.addClass('error');
95
                   hasErrors = true;
96
                }
97
            });
98
            e.preventDefault();
99
            if(hasErrors){
100
                return;
101
            }
102
            $(this).text(API.i18n.getMessage("saving"));
103
            $(this).attr('disabled', true);
104
            API.runtime.sendMessage(API.runtime.id, {
105
                method: "injectCreateCredential",
106
                args: {
107
                    label: labelfield.val(),
108
                    username: userfield.val(),
109
                    password: pwfield.val(),
110
                    vaultIndex: vaultfield.val()
111
                }
112
            }).then(removePasswordPicker);
113
        });
114
115
        $('#savepw-cancel').click(function () {
116
            labelfield.val(document.title);
117
            userfield.val('');
118
            pwfield.val('');
119
            removePasswordPicker();
120
        });
121
122
    }
123
124
    function toggleFieldType(field) {
125
        if ($(field).attr('type').toLowerCase() === 'text') {
126
            $(field).attr('type', 'password');
127
        } else {
128
            $(field).attr('type', 'text');
129
        }
130
    }
131
132
    function genPwd(settings) {
133
        /* jshint ignore:start */
134
        var password = generatePassword(settings['length'],
135
            settings.useUppercase,
136
            settings.useLowercase,
137
            settings.useDigits,
138
            settings.useSpecialChars,
139
            settings.minimumDigitCount,
140
            settings.avoidAmbiguousCharacters,
141
            settings.requireEveryCharType);
142
        /* jshint ignore:end */
143
        return password;
144
    }
145
146
    function getPasswordGenerationSettings(cb) {
147
        var default_settings = {
148
            'length': 12,
149
            'useUppercase': true,
150
            'useLowercase': true,
151
            'useDigits': true,
152
            'useSpecialChars': true,
153
            'minimumDigitCount': 3,
154
            'avoidAmbiguousCharacters': false,
155
            'requireEveryCharType': true
156
        };
157
        storage.get('password_generator_settings').then(function (_settings) {
158
            if (!_settings) {
159
                _settings = default_settings;
160
            }
161
162
            cb(_settings);
163
        }).error(function () {
164
            cb(default_settings);
165
        });
166
    }
167
168
    function setupPasswordGenerator() {
169
        //getPasswordGeneratorSettings
170
        getPasswordGenerationSettings(function (settings) {
171
            var round = 0;
172
173
            function generate_pass(inputId) {
174
                var new_password = genPwd(settings);
175
                $('#' + inputId).val(new_password);
176
                setTimeout(function () {
177
                    if (round < 10) {
178
                        generate_pass(inputId);
179
                        round++;
180
                    } else {
181
                        round = 0;
182
                    }
183
                }, 10);
184
            }
185
186
            $.each(settings, function (setting, val) {
187
                if (typeof(val) === "boolean") {
188
                    $('[name="' + setting + '"]').prop('checked', val);
189
                } else {
190
                    $('[name="' + setting + '"]').val(val);
191
                }
192
            });
193
194
            $('form[name="advancedSettings"]').change(function () {
195
                var pw_settings_form = $(this);
196
                settings = pw_settings_form.serializeObject();
197
                storage.set('password_generator_settings', settings);
198
            });
199
200
            $('.renewpw').click(function () {
201
                generate_pass('generated_password');
202
            });
203
            $('.renewpw_newac').click(function () {
204
                generate_pass('savepw-password');
205
206
            });
207
            $('.renewpw').click();
208
            $('.renewpw_newac').click();
209
210
            $('.usepwd').click(function () {
211
                $('#savepw-password').val($('#generated_password').val());
212
                $('.tab.add').click();
213
            });
214
215
            $('.togglePwVis').click(function () {
216
                toggleFieldType('#generated_password');
217
                $(this).find('.fa').toggleClass('fa-eye-slash').toggleClass('fa-eye');
218
            });
219
220
            $('.adv_opt').click(function () {
221
222
                var adv_settings = $('.pw-setting-advanced');
223
                $(this).find('i').toggleClass('fa-angle-right').toggleClass('fa-angle-down');
224
                if (adv_settings.is(':visible')) {
225
                    adv_settings.slideUp();
226
                } else {
227
                    adv_settings.slideDown();
228
                }
229
            });
230
        });
231
    }
232
233
    var picker = $('#password_picker');
234
    var makeTabActive = function (name) {
235
        picker.find('.tab').removeClass('active');
236
        picker.find('.tab-content').children().hide();
237
        picker.find('.tab-' + name + '-content').show();
238
        picker.find('.tab.' + name).addClass('active');
239
    };
240
241
    picker.find('.tab').click(function () {
242
        var name = $(this).attr('data-name');
243
        storage.set('activeTab', name).then(function (r) {
0 ignored issues
show
Unused Code introduced by brantje
The parameter r is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
244
            makeTabActive(name);
245
        });
246
    });
247
    
248
249
250
    $('.tab.close').click(function () {
251
        removePasswordPicker();
252
    });
253
254
255
    function disablePassman(where, url){
256
        var whereFn = (where === 'site') ? 'Site' : 'URL';
257
        API.runtime.sendMessage(API.runtime.id, {
258
            method: "ignore"+ whereFn,
259
            args: url
260
        }).then(function () {
261
            var text = (where === 'site') ? 'site_ignored' : 'url_ignored';
262
            $('.tab-ignore-content').find('.text').text(API.i18n.getMessage(text));
263
            setTimeout(function () {
264
                removePasswordPicker();
265
            }, 2500);
266
        });
267
    }
268
269
270
271
    function returnActiveTab(tab) {
272
273
        $('.disable-site').on('click', function () {
274
            disablePassman('site', tab.url);
275
        });
276
277
        $('.disable-page').on('click', function () {
278
            disablePassman('url', tab.url);
279
        });
280
281
        API.runtime.sendMessage(API.runtime.id, {
282
            method: "getCredentialsByUrl",
283
            args: [tab.url]
284
        }).then(function (logins) {
285
            if (logins.length === 0) {
286
                API.runtime.sendMessage(API.runtime.id, {
287
                    'method': 'getSetting',
288
                    args: 'no_results_found_tab'
289
                }).then(function (value) {
290
                    makeTabActive(value);
291
                });
292
                return;
293
            }
294
            if (logins.length !== 0) {
295
                picker.find('.tab-list-content').html('');
296
                if(runtimeSettings.passwordPickerGotoList){
297
                    makeTabActive('list');
298
                }
299
            }
300
            for (var i = 0; i < logins.length; i++) {
301
                var login = logins[i];
302
                var div = $('<div>', {class: 'account', text: login.label});
303
                $('<br>').appendTo(div);
304
                var username = (login.username !== '' ) ? login.username : login.email;
305
                $('<small>').text(username).appendTo(div);
306
                /* jshint ignore:start */
307
                div.click((function (login) {
308
                    return function () {
309
                        //enterLoginDetails(login);
310
                        //API.runtime.sendMessage(API.runtime.id, {method: 'getMasterPasswordSet'})
311
                        fillLogin(login)
312
                    };
313
                })(login));
314
                /* jshint ignore:end*/
315
316
                picker.find('.tab-list-content').append(div);
317
            }
318
        });
319
    }
320
321
    _this.returnActiveTab = returnActiveTab;
322
323
324
    $('.no-credentials .save').on('click', function () {
325
        $('.tab.add').click();
326
    });
327
    $('.no-credentials .search').on('click', function () {
328
        $('.tab.search').click();
329
    });
330
    $('.no-credentials .gen').on('click', function () {
331
        $('.tab.generate').click();
332
    });
333
    setupAddCredentialFields();
334
    setupPasswordGenerator();
335
336
337
    API.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
0 ignored issues
show
Unused Code introduced by brantje
The parameter sendResponse is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
338
        if (_this[msg.method]) {
339
            _this[msg.method](msg.args, sender);
340
        }
341
    });
342
343
344
    $('#password_search').keypress(function (e) {
345
        if (e.which === 13) {
346
            searchCredentials();
347
        }
348
    });
349
350
    function url_domain(data) {
351
        if(!data){
352
            return '';
353
        }
354
        var matches = data.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i);
355
        return matches && matches[1];  // domain will be null if no match is found
356
    }
357
358
359
    function searchCredentials() {
360
        $('#searchResults').html('');
361
        var searchText = $('#password_search').val();
362
        if (searchText === '') {
363
            return;
364
        }
365
        API.runtime.sendMessage(API.runtime.id, {
366
            'method': 'searchCredential',
367
            args: searchText
368
        }).then(function (result) {
369
            if (result.length === 0 || !result) {
370
                $('#searchResults').html(API.i18n.getMessage('no_credentials_found'));
371
            }
372
            for (var i = 0; i < result.length; i++) {
373
                var login = result[i];
374
                var div = $('<div>', {class: 'account', text: login.label});
375
                $('<br>').appendTo(div);
376
377
                var username = (login.username !== '' ) ? login.username : login.email;
378
                $('<small>').text(username).appendTo(div);
379
                $('<br>').appendTo(div);
380
                $('<small>').text(url_domain(login.url)).appendTo(div);
381
                /* jshint ignore:start */
382
                div.click((function (login) {
383
                    return function () {
384
                        //enterLoginDetails(login);
385
                        //API.runtime.sendMessage(API.runtime.id, {method: 'getMasterPasswordSet'})
386
                        fillLogin(login);
387
                        //@TODO Ask to update the url of the login
388
                        API.runtime.sendMessage(API.runtime.id, {
389
                            'method': 'updateCredentialUrlDoorhanger',
390
                            args: login
391
                        })
392
                    };
393
                })(login));
394
                /* jshint ignore:end*/
395
396
                picker.find('#searchResults').append(div);
397
            }
398
        });
399
    }
400
401
});
402