forked from gustavomondron/twik-for-chrome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
244 lines (218 loc) · 6.68 KB
/
content.js
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* Copyright (C) 2014 Red Dye No. 2
*
* This file is part of Twik.
*
* Twik is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Twik is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Twik. If not, see <http://www.gnu.org/licenses/>.
*/
var settings; // Current password generation settings
var passwordActivators = []; // List of passwords activators (1 per pw. field)
var inputIndex = 0;
var url = location.href;
// --------------------------
// START OF PasswordActivator
//
// Object which manages a password input
function PasswordActivator(passwordInput) {
this.passwordInput = passwordInput;
if (passwordInput.attr('id') == null || passwordInput.attr('id') == "") {
this.id = "twik_" + inputIndex++;
} else {
this.id = passwordInput.attr('id');
}
this.twikCheckbox = null;
enabled_inputs = settings['enabled_inputs'];
this.twikEnabled = enabled_inputs.indexOf(this.id) >= 0;
this.masterKey = '';
this.inputBackground = $(passwordInput).css('background');
this.tipTextDisabled = '<div class="twik-tip"><label><input type="checkbox" class="twik-checkbox"/> twik</label></div>';
this.tipTextEnabled = '<div class="twik-tip"><label><input type="checkbox" class="twik-checkbox" checked="checked"/> twik</label></div>';
this.init();
}
PasswordActivator.prototype.init = function() {
// Get reference to password activator, because jQuery overloads "this"
var activator = this;
// Add Twik qtip
$(this.passwordInput).qtip ({
content: {
text: activator.twikEnabled ?
activator.tipTextEnabled :
activator.tipTextDisabled
},
position: { my: 'top right', at: 'bottom right' },
show: {
event: 'focus mouseenter',
solo: true
},
hide: {
fixed: true,
event: 'unfocus'
},
style: {
classes: 'qtip-green'
},
events: {
visible: function (event, api) {
if (null != activator.twikCheckbox) {
return;
}
activator.twikCheckbox = $(".twik-checkbox", api.elements.content).get(0);
activator.twikCheckbox.addEventListener("click", function () {
activator.toggleTwik(true);
});
}
}
});
$(this.passwordInput).focus(function() {
$(this).val(activator.masterKey);
});
$(this.passwordInput).keyup(function() {
activator.masterKey = $(this).val();
});
$(this.passwordInput).keydown(function(e) {
if (e.which == 13 && activator.twikEnabled) {
// Submitting form
activator.updatePassword();
return true;
}
})
$(this.passwordInput).blur(function() {
if (activator.twikEnabled) {
activator.updatePassword();
}
});
if (this.twikEnabled) {
this.twikEnabled = false; // Not really enabled at the beginning :-)
this.toggleTwik(false);
}
}
PasswordActivator.prototype.toggleTwik = function(sendMessage) {
this.twikEnabled = !this.twikEnabled;
if (this.twikEnabled) {
this.passwordInput.get(0).style.setProperty('background', PROFILE_COLORS[settings.color], 'important');
$(this.passwordInput).addClass('twik-enabled');
this.currentPassword = this.updatePassword();
// Save the site in the case that this is the first use
chrome.runtime.sendMessage(
{
type: MESSAGES.SAVE_PROFILE,
url: url
},
null);
if (sendMessage) {
chrome.runtime.sendMessage(
{
type: MESSAGES.ENABLE_INPUT,
url: url,
id: this.id
},
null
);
}
} else {
this.passwordInput.get(0).style.setProperty('background', this.inputBackground, 'important');
$(this.passwordInput).removeClass('twik-enabled');
$(this.passwordInput).val(this.masterKey);
if (sendMessage) {
chrome.runtime.sendMessage(
{
type: MESSAGES.DISABLE_INPUT,
url: url,
id: this.id
},
null
);
}
}
}
PasswordActivator.prototype.updatePassword = function() {
if (this.masterKey != '') {
var passwordHasher = new PasswordHasher();
var sitePassword = passwordHasher.hashPassword(
settings.tag,
this.masterKey,
settings.private_key,
settings.password_length,
settings.password_type
);
$(this.passwordInput).val(sitePassword);
}
}
PasswordActivator.prototype.isEnabled = function() {
return this.twikEnabled;
}
PasswordActivator.prototype.updateBackgroundColor = function() {
if (this.twikEnabled) {
$(this.passwordInput).css('background-color', PROFILE_COLORS[settings.color]);
}
}
// END OF PasswordActivator
// ------------------------
function activatePasswordInputs() {
$("input[type=password]").each (function(index) {
activator = new PasswordActivator($(this));
passwordActivators[passwordActivators.length] = activator;
activator.init();
});
}
function updatePasswordInputs() {
for (i = 0; i < passwordActivators.length; i++) {
activator = passwordActivators[i];
if (activator.isEnabled()) {
activator.updateBackgroundColor();
activator.updatePassword();
}
}
}
function addNewNodeListener() {
document.addEventListener('DOMNodeInserted', activateElement, false);
}
function removeNewNodeListener() {
document.addEventListener('DOMNodeInserted', activateElement, false);
}
function activateElement(event) {
// We're going to add a new DOM element (qtip) so we should remove the listener first
removeNewNodeListener();
// Create the new activator in the case that it is a password input
$('input[type=password]', event.srcElement).each(function() {
activator = new PasswordActivator($(this));
passwordActivators[passwordActivators.length] = activator;
activator.init();
});
// Enable the new DOM element listener
addNewNodeListener();
}
// SCRIPT START
// Ask for site settings
chrome.runtime.sendMessage(
{
type: MESSAGES.GET_SETTINGS,
url: url
},
function(response) {
settings = response.settings;
activatePasswordInputs();
// Listen for new password inputs added to the document
addNewNodeListener();
}
);
// Listen for updates in settings
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.type == MESSAGES.UPDATE_SETTINGS) {
settings = request.settings;
updatePasswordInputs();
}
}
);