Welcome to mirror list, hosted at ThFree Co, Russian Federation.

username-field.js « content « keepassxc-browser - github.com/keepassxreboot/keepassxc-browser.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4db5f980bf709b2ae067ca284090e09a5066147e (plain)
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
'use strict';

const kpxcUsernameFields = {};
kpxcUsernameFields.icons = [];

kpxcUsernameFields.newIcon = function(field, databaseClosed = true) {
    kpxcUsernameFields.icons.push(new UsernameFieldIcon(field, databaseClosed));
};

kpxcUsernameFields.switchIcon = function(locked) {
    kpxcUsernameFields.icons.forEach(u => u.switchIcon(locked));
};


class UsernameFieldIcon extends Icon {
    constructor(field, databaseClosed = true) {
        super();
        this.icon = null;
        this.inputField = null;

        this.initField(field, databaseClosed);
        kpxcUI.monitorIconPosition(this);
    }

    switchIcon(locked) {
        if (!this.icon) {
            return;
        }
    
        if (locked) {
            this.icon.classList.remove(getIconClassName());
            this.icon.classList.add(getIconClassName(true));
            this.icon.title = tr('usernameLockedFieldText');
        } else {
            this.icon.classList.remove(getIconClassName(true));
            this.icon.classList.add(getIconClassName());
            this.icon.title = tr('usernameFieldText');
        }
    }
};

UsernameFieldIcon.prototype.initField = function(field, databaseClosed = true) {
    if (!field || field.getAttribute('kpxc-username-field') === 'true') {
        return;
    }

    field.setAttribute('kpxc-username-field', 'true');

    // Observer the visibility
    if (this.observer) {
        this.observer.observe(field);
    }

    this.createIcon(field, databaseClosed);
    this.inputField = field;
};

UsernameFieldIcon.prototype.createIcon = function(target, databaseClosed) {
    // Remove any existing password generator icons from the input field
    if (target.getAttribute('kpxc-password-generator')) {
        kpxcPasswordDialog.removeIcon(target);
    }

    const field = target;
    const className = getIconClassName(databaseClosed);

    // Size the icon dynamically, but not greater than 24 or smaller than 14
    const size = Math.max(Math.min(24, field.offsetHeight - 4), 14);
    
    // Don't create the icon if the input field is too small
    if (field.offsetWidth < (size * 1.5) || field.offsetHeight < size) {
        this.observer.unobserve(field);
        return;
    }

    let offset = Math.floor((field.offsetHeight - size) / 3);
    offset = (offset < 0) ? 0 : offset;

    const icon = kpxcUI.createElement('div', 'kpxc kpxc-username-icon ' + className,
        {
            'title': databaseClosed ? tr('usernameLockedFieldText') : tr('usernameFieldText'),
            'alt': tr('usernameFieldIcon'),
            'size': size,
            'offset': offset,
            'kpxc-pwgen-field-id': field.getAttribute('data-kpxc-id')
        });
    icon.style.zIndex = '10000000';
    icon.style.width = Pixels(size);
    icon.style.height = Pixels(size);

    icon.addEventListener('click', function(e) {
        if (!e.isTrusted) {
            return;
        }

        e.preventDefault();
        iconClicked(field, icon);
    });

    kpxcUI.setIconPosition(icon, field);
    this.icon = icon;
    document.body.appendChild(icon);
};

const iconClicked = async function(field, icon) {
    if (!kpxcFields.isVisible(field)) {
        document.body.removeChild(icon);
        field.removeAttribute('kpxc-username-field');
        return;
    }

    const connected = await browser.runtime.sendMessage({ action: 'is_connected' });
    if (!connected) {
        kpxcUI.createNotification('error', tr('errorNotConnected'));
        return;
    }

    const databaseHash = await browser.runtime.sendMessage({ action: 'check_database_hash' });
    if (databaseHash === '') {
        // Triggers database unlock
        _called.manualFillRequested = ManualFill.BOTH;
        await browser.runtime.sendMessage({
            action: 'get_database_hash',
            args: [ false, true ] // Set triggerUnlock to true
        });
    }

    if (icon.className.includes('unlock')) {
        fillCredentials(field);
    }
};

const getIconClassName = function(locked = false) {
    if (locked) {
        return (isFirefox() ? 'lock-moz' : 'lock');
    }
    return (isFirefox() ? 'unlock-moz' : 'unlock');
};

const fillCredentials = function(field) {
    const fieldId = field.getAttribute('data-kpxc-id');
    kpxcFields.prepareId(fieldId);

    const givenType = field.type === 'password' ? 'password' : 'username';
    const combination = kpxcFields.getCombination(givenType, fieldId);

    kpxc.fillInCredentials(combination, givenType === 'password', false);
};