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: 16f122cab6eb371d76b4af46fe3f8b0379e49ebe (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
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);
                }
            }
        );
    }

    /**
     *
     */
    initClient() {
        if (document.readyState === "complete" 
            || document.readyState === "loaded" 
            || document.readyState === "interactive") {
            this._sendBrowserPageReadyMessage()
       } else {
        window.addEventListener("domcontentloaded", this._sendBrowserPageReadyMessage());
       }
    }

    /**
     *
     */
    _sendBrowserPageReadyMessage() {
        MessageService.send(
            {
                type    : 'autofill.page.ready',
                payload : {
                    url: window.location.href
                },
                receiver: 'background'
            }
        );
    }

    /**
     *
     * @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(),
                    submit  : false
                }
            }
        ).catch(ErrorManager.catchEvt);
    }
};