Issues (71)

js/background/service/contextMenu.js (1 issue)

Severity
1
/* global API */
2
3
/**
4
 * Nextcloud - passman
5
 *
6
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
7
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
window.contextMenu = (function () {
26
    'use strict';
27
    var storage = new API.Storage();
28
    var exportContextMenu = {
29
        setContextItems: function (logins) {
30
            var i,f, field;
31
            var fields = [
32
                {menu: 'autoFill:', field: 'autoFill', found: false},
33
                {field: 'username', menu: 'copy:User', found: false},
34
                {field: 'password', menu: 'copy:Pass', found: false},
35
                {field: 'url', menu: 'copy:Url', found: false},
36
                {field: 'totp', menu: 'copy:OTP', found: false}
37
            ];
38
            API.contextMenus.removeAll();
39
            initMenus();
40
41
            for (i = 0; i < logins.length; i++) {
42
                var login = logins[i];
43
                login.autoFill = (!login.hasOwnProperty('autoFill')) ? true : login.autoFill;
44
                for (f = 0; f < fields.length; f++) {
45
                    field = fields[f];
46
                    if (field.field === 'totp' && login.otp) {
47
                        login.totp = login.otp.secret;
48
                    }
49
                    if (login[field.field]) {
50
                        fields[f].found = true;
51
                        /* jshint ignore:start */
52
                        createMenuItem(field.menu, field.menu + ':' + login.guid, login.label, (function (field, login) {
53
                            return function () {
54
                                itemClickCallback(field, login);
55
                            };
56
                        })(field, login));
57
                        /* jshint ignore:end */
58
                    }
59
                }
60
            }
61
62
            for (f = 0; f < fields.length; f++) {
63
                field = fields[f];
64
                if(field.found === false){
65
                    API.contextMenus.remove(field.menu);
66
                }
67
            }
68
69
        },
70
        addPasswordGenerator: function(){
71
            createMenuItem('generatePassword', 'copyGen', 'And copy to clipboard', function(){
72
                generatePass(function (generated_password) {
73
                    API.tabs.query({active: true, currentWindow: true}).then(function (tabs) {
74
                        API.tabs.sendMessage(tabs[0].id, {method: "copyText", args: generated_password});
75
                    });
76
                });
77
            });
78
79
            createMenuItem('generatePassword', 'fill', 'And fill fields', function(){
80
                generatePass(function (generated_password) {
81
                    var login = {
82
                        password: generated_password
83
                    };
84
                    API.tabs.query({active: true, currentWindow: true}).then(function (tabs) {
85
                        API.tabs.sendMessage(tabs[0].id, {method: "enterLoginDetails", args: login}).then(function (response) {
0 ignored issues
show
The parameter response 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...
86
                        });
87
                    });
88
                });
89
90
            });
91
        }
92
    };
93
94
95
    function generatePass(cb){
96
        var default_settings = {
97
            'length': 12,
98
            'useUppercase': true,
99
            'useLowercase': true,
100
            'useDigits': true,
101
            'useSpecialChars': true,
102
            'minimumDigitCount': 3,
103
            'avoidAmbiguousCharacters': false,
104
            'requireEveryCharType': true
105
        };
106
        storage.get('password_generator_settings').then(function (_settings) {
107
            if (!_settings) {
108
                _settings = default_settings;
109
            }
110
            /* jshint ignore:start */
111
            var password = generatePassword(_settings['length'],
112
                _settings.useUppercase,
113
                _settings.useLowercase,
114
                _settings.useDigits,
115
                _settings.useSpecialChars,
116
                _settings.minimumDigitCount,
117
                _settings.avoidAmbiguousCharacters,
118
                _settings.requireEveryCharType);
119
            /* jshint ignore:end */
120
            cb(password);
121
        }).error(function () {
122
            /* jshint ignore:start */
123
            var password = generatePassword(default_settings['length'],
124
                default_settings.useUppercase,
125
                default_settings.useLowercase,
126
                default_settings.useDigits,
127
                default_settings.useSpecialChars,
128
                default_settings.minimumDigitCount,
129
                default_settings.avoidAmbiguousCharacters,
130
                default_settings.requireEveryCharType);
131
            /* jshint ignore:end */
132
            cb(password);
133
        });
134
    }
135
136
    function initMenus() {
137
        API.contextMenus.create({
138
            id: 'autoFill:',
139
            title: 'Auto fill',
140
            contexts: ['page']
141
        });
142
143
        API.contextMenus.create({
144
            id: 'generatePassword',
145
            title: 'Generate password',
146
            contexts: ['page']
147
        });
148
149
        API.contextMenus.create({
150
            id: 'copy:User',
151
            title: 'Copy username',
152
            contexts: ['page']
153
        });
154
155
        API.contextMenus.create({
156
            id: 'copy:Pass',
157
            title: 'Copy password',
158
            contexts: ['page']
159
        });
160
161
162
        API.contextMenus.create({
163
            id: 'copy:Url',
164
            title: 'Copy URL',
165
            contexts: ['page']
166
        });
167
168
        API.contextMenus.create({
169
            id: 'copy:OTP',
170
            title: 'Copy OTP',
171
            contexts: ['page']
172
        });
173
        exportContextMenu.addPasswordGenerator();
174
    }
175
176
    function createMenuItem(parentId, id, label, clickcb) {
177
        API.contextMenus.create({
178
            id: id,
179
            title: label,
180
            contexts: ["page"],
181
            parentId: parentId,
182
            onclick: clickcb
183
        });
184
    }
185
186
    function itemClickCallback(menu_action, login) {
187
        var action = menu_action.menu.split(':', 1)[0];
188
189
        if (action === 'copy') {
190
191
            API.tabs.query({active: true, currentWindow: true}).then(function (tabs) {
192
                var text = login[menu_action.field];
193
                if(menu_action.menu.indexOf('OTP') !== -1){
194
                    window.OTP.secret = login.totp;
195
                    text =  window.OTP.getOTP();
196
                }
197
                API.tabs.sendMessage(tabs[0].id, {method: "copyText", args: text});
198
            });
199
            return;
200
        }
201
202
        if (action === 'autoFill') {
203
            API.tabs.query({active: true, currentWindow: true}).then(function (tabs) {
204
                API.tabs.sendMessage(tabs[0].id, {method: "enterLoginDetails", args: login});
205
            });
206
        }
207
    }
208
209
210
    API.contextMenus.removeAll();
211
    initMenus();
212
213
    return exportContextMenu;
214
215
}());