Completed
Push — master ( bba01d...8c82ac )
by Sander
01:20
created

window.contextMenu.setContextItems   A

Complexity

Conditions 1
Paths 6

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
nc 6
nop 1
dl 0
loc 5
rs 9.4285
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
    function initMenus() {
28
        API.contextMenus.create({
29
            id: 'autoFill:',
30
            title: 'Auto fill',
31
            contexts: ['all']
32
        });
33
34
        API.contextMenus.create({
35
            id: 'copy:User',
36
            title: 'Copy username',
37
            contexts: ['all']
38
        });
39
40
        API.contextMenus.create({
41
            id: 'copy:Pass',
42
            title: 'Copy password',
43
            contexts: ['all']
44
        });
45
46
47
        API.contextMenus.create({
48
            id: 'copy:Url',
49
            title: 'Copy URL',
50
            contexts: ['all']
51
        });
52
       /* API.contextMenus.create({
53
            id: 'copy:OTP',
54
            title: 'Copy OTP',
55
            contexts: ['all']
56
        });*/
57
    }
58
59
    function createMenuItem(parentId, id, label, clickcb) {
60
        API.contextMenus.create({
61
            id: id,
62
            title: label,
63
            contexts: ["all"],
64
            parentId: parentId,
65
            onclick: clickcb
66
        });
67
    }
68
69
    function itemClickCallback(menu_action, login) {
70
        var action = menu_action.menu.split(':', 1)[0];
71
72
        if (action === 'copy') {
73
            copyTextToClipboard(login[menu_action.field]);
74
            return;
75
        }
76
77
        if (action === 'autoFill') {
78
            API.tabs.query({active: true, currentWindow: true}).then(function (tabs) {
79
                API.tabs.sendMessage(tabs[0].id, {method: "enterLoginDetails", args: login}).then(function (response) {
0 ignored issues
show
Unused Code introduced by
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...
80
                });
81
            });
82
        }
83
    }
84
85
    function copyTextToClipboard(text) {
86
        var copyFrom = document.createElement("textarea");
87
        copyFrom.textContent = text;
88
        var body = document.getElementsByTagName('body')[0];
89
        body.appendChild(copyFrom);
90
        copyFrom.select();
91
        document.execCommand('copy');
92
        body.removeChild(copyFrom);
93
    }
94
    API.contextMenus.removeAll();
95
    initMenus();
96
97
    return {
98
        setContextItems: function (logins) {
99
100
            var fields = [
101
                {menu: 'autoFill:', field: 'autoFill'},
102
                {field: 'username', menu: 'copy:User'},
103
                {field: 'password', menu: 'copy:Pass'},
104
                {field: 'url', menu: 'copy:Url'},
105
               // {field: 'totp', menu: 'copy:OTP'}
106
            ];
107
            API.contextMenus.removeAll();
108
            initMenus();
109
110
            for (var i = 0; i < logins.length; i++) {
111
                var login = logins[i];
112
                login.autoFill = true;
113
                for (var f = 0; f < fields.length; f++) {
114
                    var field = fields[f];
115
                    if (field['field'] === 'totp' && login.otp) {
116
                        login.totp = login.otp.secret;
117
                    }
118
                    if (login[field['field']]) {
119
                        createMenuItem(field['menu'], field['menu'] + ':' + login.guid, login.label, (function (field, login) {
120
                            return function () {
121
                                itemClickCallback(field, login);
122
                            };
123
                        })(field, login));
124
                    }
125
                }
126
            }
127
        }
128
    }
129
130
}());