forked from keithamus/LinkyPass
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackground.js
executable file
·93 lines (87 loc) · 2.73 KB
/
background.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
//Get our passwords
var v = 1.1;
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse)
{
var passes = JSON.parse(localStorage['passwords'] || '[]');
console.log(request,sender,sendResponse);
//Our message has a tab id or extension id
if(sender.tab || sender.id)
{
//The content script is just asking for options
if(request.message == 'options')
{
sendResponse(JSON.parse(localStorage['options'] || '{}'));
}
//The content script is asking us what to do!
else if (request.message == 'init' || request.message == 'request')
{
//No passwords in db, so send a quick message to tell the page.
if(passes.length==0 && request.message=='init')
{
sendResponse({length: 0});
}
//We've only got one password, or we're only being asked for one...
else if(passes.length==1 || (request.id && request.id.length>0))
{
id = parseInt(passes.length)==1?passes[0]:request.id;
sendPass = Pass.init(id);
if(request.password && request.password.length>0)
{
//We've been sent a password, so we should apply it (potentially).
sendPass.password(request.password);
}
//Send back a generated password
sendResponse(sendPass.generate(sender.tab.url,request.disabletld));
}
//We've got lots of passwords, so present the user with a list of selections:
else if(passes.length>1 && request.message=='init')
{
var response = {};
passes.forEach(function(id)
{
response[id] =
{
id: id,
name: localStorage['password_'+id+'_name'],
hash: localStorage['password_'+id+'_type']=='hash'?
localStorage['password_'+id+'_password']:false
}
});
sendResponse({length: passes.length, passes: response})
}
//Add a catch for when there are no profiles but a password needs generating
else if(passes.length==0 && request.message=='request')
{
sendPass = Pass.none;
sendPass.password(request.password);
sendResponse(sendPass.generate(sender.tab.url,request.disabletld));
}
}
else if(request.message == 'versioncheck')
{
/* Version check! */
if(!localStorage['version'] || localStorage['version']<v)
{
if(request.notify)
{
localStorage['version'] = v.toFixed(1);
}
else
{
sendResponse(
{
notify: true,
message: 'EnigmaPass has been updated to the latest version ('+
v.toFixed(1).toString()+'). Click the options button for more:'
});
}
}
}
else
{
console.warn('Snubbing due to bad message:');
sendResponse({});
}
}
});