Completed
Pull Request — master (#50)
by Sander
01:16
created

$(document).ready   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 1
b 0
f 0
1
$(document).ready(function () {
2
    function closeDoorhanger() {
3
        API.runtime.sendMessage(API.runtime.id, {
4
            method: "passToParent",
5
            args: {'injectMethod': 'closeDoorhanger'}
6
        });
7
    }
8
    var dh = $('#password-doorhanger');
9
    var btn_config = {
10
        'cancel': function () {
11
            return {
12
                text: API.i18n.getMessage('cancel'),
13
                onClickFn: function () {
14
                    API.runtime.sendMessage(API.runtime.id, {
15
                        method: "passToParent",
16
                        args: {'injectMethod': 'closeDoorhanger'}
17
                    });
18
                    API.runtime.sendMessage(API.runtime.id, {method: "clearMined"});
19
                }
20
            };
21
        },
22
        'save': function (data) {
23
            var save = API.i18n.getMessage('save');
24
            var update = API.i18n.getMessage('update');
25
            var btnText = (data.guid === null) ? save : update;
26
            return {
27
                text: btnText,
28
                onClickFn: function () {
29
                    API.runtime.sendMessage(API.runtime.id, {method: "saveMined"});
30
                    dh.find('.toolbar-text').text(API.i18n.getMessage('saving') + '...');
31
                    dh.find('.passman-btn').hide();
32
                }
33
            };
34
        },
35
        'updateUrl': function (data) {
36
            return {
37
                text: 'Update',
38
                onClickFn: function () {
39
                    API.runtime.sendMessage(API.runtime.id, {method: "updateCredentialUrl", args: data.data});
40
                    dh.find('.toolbar-text').text('Saving...');
41
                    dh.find('.passman-btn').hide();
42
                }
43
            };
44
        },
45
        'ignore': function (data) {
46
            return {
47
                text: API.i18n.getMessage('ignore_site'),
48
                onClickFn: function () {
49
                    //closeToolbar();
50
                    API.runtime.sendMessage(API.runtime.id, {method: "ignoreSite", args: data.currentLocation});
51
                    dh.find('.toolbar-text').text(API.i18n.getMessage('site_ignored'));
52
                    dh.find('.passman-btn').hide();
53
                    setTimeout(function () {
54
                        closeDoorhanger();
55
                    }, 3000);
56
                }
57
            };
58
        }
59
    };
60
61
62
    API.runtime.sendMessage(API.runtime.id, {method: "getDoorhangerData"}).then(function (data) {
63
        var buttons = data.buttons;
64
        data = data.data;
65
66
        var doorhanger_div = $('<div id="password-toolbar">');
67
        $('<span>', {
68
            class: 'toolbar-text',
69
            text: data.title + ' ' + data.username + ' at ' + data.url
70
        }).appendTo(doorhanger_div);
71
72
73
        $.each(buttons, function (k, button) {
74
            button = btn_config[button](data);
75
            var html_button = $('<button class="passman-btn passnman-btn-success"></button>').text(button.text);
76
            html_button.click(button.onClickFn);
77
            doorhanger_div.append(html_button);
78
        });
79
        dh.html(doorhanger_div);
80
    });
81
82
    var _this = {};
83
84
    function minedLoginSaved(args) {
85
        // If the login added by the user then this is true
86
        if ($('#password-doorhanger').is(':visible')) {
87
            var saved = API.i18n.getMessage('credential_saved');
88
            var updated = API.i18n.getMessage('credential_updated');
89
            var action = (args.updated) ? updated : saved;
90
            $('#password-toolbar').html(action + '!');
91
            //@TODO update
92
            setTimeout(function () {
93
                closeDoorhanger();
94
            }, 2500);
95
        }
96
    }
97
98
    _this.minedLoginSaved = minedLoginSaved;
99
    API.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
0 ignored issues
show
Unused Code introduced by
The parameter sendResponse 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...
100
        //console.log('Method call', msg.method);
101
        if (_this[msg.method]) {
102
            _this[msg.method](msg.args, sender);
103
        }
104
    });
105
});