-
Notifications
You must be signed in to change notification settings - Fork 1
/
Keefox.js
353 lines (300 loc) · 10.8 KB
/
Keefox.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
//Set to false if you don't use mapping,or you can set to other keys
//tryLogin:launch KeePass if not launched,directly login if has only one matched login,else list the matched logins
if(true){group.mappings.add(
[modes.NORMAL],
["sl"],
"Security Login",
function({count}){
KeefoxUtil.tryLogin(count || 0)
},{
count: true
}
);}
let keefox=keefox_org;
let KeefoxUtil = {
LaunchToLogin : false,
IsLaunched : function(){
return keefox._keeFoxStorage.get("KeePassRPCActive", false);
},
//Perhaps there is easier way to get all logins?
getAllLogins: function () {
var container=[];
if (keefox._keeFoxStorage.get("KeePassDatabaseOpen", false)) {
// start with the current root group uniqueID
try {
if (!keefox._keeFoxExtension.prefs.getValue("listAllOpenDBs",false))
{
var rootGroup = keefox.KeePassDatabases[keefox.ActiveKeePassDatabaseIndex].root;
if (rootGroup != null && rootGroup != undefined && rootGroup.uniqueID)
{
var dbFileName = keefox.KeePassDatabases[keefox.ActiveKeePassDatabaseIndex].fileName;
KeefoxUtil.getOneLoginsMenu(container, rootGroup, dbFileName);
}
} else
{
for (var i=0; i<keefox.KeePassDatabases.length; i++)
{
var rootGroup = keefox.KeePassDatabases[i].root;
if (rootGroup != null && rootGroup != undefined && rootGroup.uniqueID)
{
var dbFileName = keefox.KeePassDatabases[i].fileName;
if (keefox.KeePassDatabases.length > 1)
{
var dbName = keefox.KeePassDatabases[i].name;
var item = new Object();
item.label= dbName + ' / ' + rootGroup.title;
item.tooltiptext= keefox.locale.$STR("loginsButtonGroup.tip");
item.class= "menu-iconic";
item.uuid = rootGroup.uniqueID;
item.fileName = dbFileName;
item.context= "KeeFox-group-context";
item.image= "data:image/png;base64,"+rootGroup.iconImageData;
container.push(item);
KeefoxUtil.getOneLoginsMenu(container,rootGroup, dbFileName);
} else
{
KeefoxUtil.getOneLoginsMenu(container, rootGroup, dbFileName);
}
}
}
}
} catch (e) {
dactyl.echoerr("getAllLogins exception: " + e);
return;
}
} else {
Cc["@mozilla.org/alerts-service;1"].getService(Ci.nsIAlertsService).showAlertNotification(
null,"Keefox: not launch", ":private launchKeePass ", true
);
}
return container;
},
// add all the logins and subgroups for one KeePass group
getOneLoginsMenu: function(container, group, dbFileName)
{
var foundGroups = group.childGroups;
var foundLogins = group.childLightEntries;
if ((foundGroups == null || foundGroups.length == 0) && (foundLogins == null || foundLogins.length == 0)) {
return;
}
for (var i = 0; i < foundGroups.length; i++) {
var group = foundGroups[i];
var item = new Object();
item.label= group.title;
item.tooltiptext= keefox.locale.$STR("loginsButtonGroup.tip");
item.class= "menu-iconic";
item.uuid = group.uniqueID;
item.fileName = dbFileName;
item.context= "KeeFox-group-context";
item.image= "data:image/png;base64," + group.iconImageData;
container.push(item);
KeefoxUtil.getOneLoginsMenu(container,group,dbFileName);
}
for (var i = 0; i < foundLogins.length; i++) {
var login = foundLogins[i];
var usernameValue = "";
var usernameName = "";
var usernameDisplayValue = "[" + keefox.locale.$STR("noUsername.partial-tip") + "]";
usernameValue = login.usernameValue;
if (usernameValue != undefined && usernameValue != null && usernameValue != "")
usernameDisplayValue = usernameValue;
usernameName = login.usernameName;
var tempButton = new Object();
tempButton.label= login.title;
tempButton.tooltiptext= keefox.locale.$STRF(
"loginsButtonLogin.tip", [login.uRLs[0], usernameDisplayValue]);
tempButton.class= "menuitem-iconic";
tempButton.fileName = dbFileName;
tempButton.context= "KeeFox-login-context";
tempButton.image= "data:image/png;base64," + login.iconImageData;
tempButton.uuid= login.uniqueID;
tempButton.usernameName= usernameName;
tempButton.usernameValue= usernameValue;
tempButton.url= login.uRLs[0];
container.push(tempButton);
}
},
getLogins:function(host,completer){
function filter(all,host,ac){
var i=0;
for each(var item in all) {
if(item==null || item.url==null || item.url.indexOf(host)<0) continue;
i++;
if(completer) ac.push([item.uuid,i+"--"+item.usernameValue+"@"+item.url]);
else ac.push(item);
}
}
var ac=[];
if(host==null) return ac;
var all=KeefoxUtil.getAllLogins();
filter(all,host,ac);
if(ac.length>0) return ac;
var strs=host.split(".");
if(strs.length>3){
var j=strs.length;
var sub=strs[j-3]+"."+strs[j-2]+"."+strs[j-1];
filter(all,sub,ac);
if(ac.length>0) return ac;
}
if(strs.length>2){
var j=strs.length;
var sub=strs[j-2]+"."+strs[j-1];
filter(all,sub,ac);
if(ac.length>0) return ac;
}
return ac;
},
getLogin : function(uuid){
for each(var item in KeefoxUtil.getAllLogins()) {
if(item.uuid==uuid) return item;
}
return null;
},
login : function(login){
keefox_win.ILM.fill(
login.usernameName,login.usernameValue,login.formActionURL,
login.usernameId,login.formId,login.uuid,
buffer.uri.asciiSpec,login.fileName
);
},
//当前页面可用登录
BufferCompleter: function (context) {
context.title=["KeePass UUID","User@Domain"];
context.compare = CompletionContext.Sort.unsorted;
context.completions=KeefoxUtil.getLogins(buffer.uri.host,true);
context.filterFunc=function(items){
var words = context.filter.toLowerCase().split(/\s+/g);
return [item for each(item in items) if((item.text + " " + item.description).toLowerCase().indexOf(words)>=0)];
}
},
//全部登录项
LoadCompleter: function (context) {
context.title=["KeePass UUID","User@Domain"];
context.compare = CompletionContext.Sort.unsorted;
context.completions=KeefoxUtil.getLogins(context.filter,true);
context.filterFunc=function(items){
var words = context.filter.toLowerCase().split(/\s+/g);
return [item for each(item in items) if((item.text + " " + item.description).toLowerCase().indexOf(words)>=0)];
}
},
//尝试登录
tryLogin:function(number){
if(!KeefoxUtil.IsLaunched()){
Cc["@mozilla.org/alerts-service;1"].getService(Ci.nsIAlertsService).showAlertNotification(
null,"Keefox: Enter master password to launch KeePass", "Note: wait for a while to login", true
);
CommandExMode().open("private launchKeePass ");
return;
}
var logins=KeefoxUtil.getLogins(buffer.uri.host,false);
if(logins.length==1){
var login=logins[0];
KeefoxUtil.login(login);
return;
}
number=parseInt(number);
if(number>=1 && number<=logins.length){
var login=logins[number-1];
KeefoxUtil.login(login);
return;
}
CommandExMode().open("login ");
}
}//end of KeefoxUtil
//登录当前页面,根据url匹配记录
group.commands.add(
['login'],
'List Keepass logins,chose account to login current page',
function(args){
if(args.length==0) {
dactyl.echomsg("You must select one account to login.");
return;
}
var login=KeefoxUtil.getLogin(args[0]);
if(login==null) {
dactyl.echomsg("Use Tab to confirm select one account to login.");
return;
}
KeefoxUtil.login(login);
},
{
bang: true,
completer: KeefoxUtil.BufferCompleter,
literal: 0
},
true
);
//列出所有登录项,自动打开登录页并登录
group.commands.add(
['openlogin','olo[gin]'],
'List Keepass logins,choose item to open the url then login',
function(args){
if(args.length==0) {
dactyl.echomsg("You must select one account to login.");
return;
}
var login=KeefoxUtil.getLogin(args[0]);
keefox_win.ILM.loadAndAutoSubmit(
0,false,
login.usernameName,login.usernameValue,login.url,
null,null,login.uuid,login.fileName
);
},
{
bang: true,
completer: KeefoxUtil.LoadCompleter,
literal: 0
},
true
);
//列出所有登录项,选择记录进行编辑
group.commands.add(
['editl[ogin]'],
'List Keepass logins,choose item to edit',
function(args){
if(args.length==0) {
dactyl.echomsg("You must select one account to login.");
return;
}
var login=KeefoxUtil.getLogin(args[0]);
keefox.launchLoginEditor(login.uuid,login.fileName);
},
{
bang: true,
completer: KeefoxUtil.LoadCompleter,
literal: 0
},
true
);
group.commands.add(
['generatePassword','gp'],
'generate password,copy to clipboard',
function(){
keefox.generatePassword();
}
);
group.commands.add(
['launchK[eePass]'],
'Note:use private launchKeepass pwd to protect your pwd',
function(args){
if(KeefoxUtil.IsLaunched()){
dactyl.echomsg('KeePass was launched.');
return;
}
if(args.length==0) keefox.launchKeePass('');
else keefox.launchKeePass(["-pw:"+args[0]]);
KeefoxUtil.LaunchToLogin=true;
}
);
//Try Login Current Page after launch KeePass
//Perhaps there is other way not crack the Keefox?
if(true && keefox.updateKeePassDatabases_org==null){
keefox.updateKeePassDatabases_org=keefox.updateKeePassDatabases;
keefox.updateKeePassDatabases=function(newDatabases){
keefox.updateKeePassDatabases_org(newDatabases);
if(KeefoxUtil.LaunchToLogin && KeefoxUtil.IsLaunched()) {
KeefoxUtil.tryLogin();
KeefoxUtil.LaunchToLogin=false;
}
}
}