Completed
Push — master ( b362d9...2b4a65 )
by Sander
50s
created

$(document).ready   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 25
rs 8.8571
c 2
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});
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
        if(!data){
64
            return;
65
        }
66
        var buttons = data.buttons;
67
        data = data.data;
68
        var displayUrl = data.url;
69
        if(displayUrl.length > 512){
70
            displayUrl = displayUrl.substring(0,8);
71
        }
72
        var doorhanger_div = $('<div id="password-toolbar">');
73
        $('<span>', {
74
            class: 'toolbar-text',
75
            text: data.title + ' ' + data.username + ' at ' + displayUrl
76
        }).appendTo(doorhanger_div);
77
78
79
        $.each(buttons, function (k, button) {
80
            button = btn_config[button](data);
81
            var html_button = $('<button class="passman-btn passnman-btn-success"></button>').text(button.text);
82
            html_button.click(button.onClickFn);
83
            doorhanger_div.append(html_button);
84
        });
85
        dh.html(doorhanger_div);
86
    });
87
88
    var _this = {};
89
90
    function minedLoginSaved(args) {
91
        // If the login added by the user then this is true
92
93
        var saved = API.i18n.getMessage('credential_saved');
94
        var updated = API.i18n.getMessage('credential_updated');
95
        var action = (args.updated) ? updated : saved;
96
        $('#password-toolbar').html(action + '!');
97
        //@TODO update
98
        setTimeout(function () {
99
            closeDoorhanger();
100
        }, 2500);
101
102
    }
103
104
    _this.minedLoginSaved = minedLoginSaved;
105
    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...
106
        //console.log('Method call', msg.method);
107
        if (_this[msg.method]) {
108
            _this[msg.method](msg.args, sender);
109
        }
110
    });
111
});