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

AutofillManager.js « Manager « js « src - github.com/marius-wieschollek/passwords-webextension.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e4238d6b0320b7ebe7b555baaf4afa31eed99f26 (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
import RecommendationManager from "@js/Manager/RecommendationManager";
import MessageService from "@js/Services/MessageService";
import TabManager from "@js/Manager/TabManager";
import ErrorManager from "@js/Manager/ErrorManager";
import SettingsService from "@js/Services/SettingsService";

export default new class AutofillManager {

    constructor() {
        this._recommendationListener = (recommendations) => {
            this.recommendations = recommendations;
            this.currentURL = TabManager.getAll()[TabManager.currentTabId].url;
        };
        this.recommendations = [];
        this.currentURL = null;
    }

    /**
     *
     */
    async init() {
        RecommendationManager.listen.on(this._recommendationListener);
        MessageService.listen(
            'autofill.page.ready',
            (message, reply) => {
                if(message.url = this.currentURL) {
                    this._sendAutofillPassword(this.recommendations);
                }
            }
        );
    }

    /**
     *
     * @param {Password[]} recommendations
     * @private
     */
    async _sendAutofillPassword(recommendations) {
        if(recommendations.length === 0 || await SettingsService.getValue('paste.autofill') === false) return;
        let password = recommendations[0];

        setTimeout(() => {
            let ids = TabManager.get('autofill.ids', []);
            if(ids.indexOf(password.getId()) === -1) {
                ids.push(password.getId());
                TabManager.set('autofill.ids', ids);
            }
            this._sendPwdToMessageService(password);
        }, 500);
    }

    /**
     *
     * @param {Password[]} recommendations
     * @private
     */
    _sendPwdToMessageService(password) {
        MessageService.send(
            {
                type    : 'autofill.password',
                receiver: 'client',
                channel : 'tabs',
                tab     : TabManager.currentTabId,
                silent  : true,
                payload : {
                    user      : password.getUserName(),
                    password  : password.getPassword(),
                    formFields: this.getCustomFormFields(password),
                    submit    : false
                }
            }
        ).catch(ErrorManager.catchEvt);
    }

    /**
     *
     * @param {Password} password
     * @returns {Array}
     */
    getCustomFormFields(password) {
        let formFields = [],
            customFields = password.getCustomFields();

        customFields._elements.forEach((e) => {
            if(e.getType() === 'data' && e.getLabel().startsWith('ext:field/')) {
                formFields.push(
                    {
                        id   : e.getLabel().replace('ext:field/', ''),
                        value: e.getValue()
                    }
                );
            }
        });

        return formFields;
    }
};