-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
88 lines (81 loc) · 2.84 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
// function to verify if logged in
function verifyAuth(callback) {
getAuthCreds(function (creds) {
// async for local storage, ensure both have loaded prior to get request
var getting = $.get("https://securis-debug.herokuapp.com/accounts.json", {
"auth_token": creds.authToken,
"auth_email": creds.authEmail
}, function (data) {
// login successful
callback(true);
});
// if errors, incorrect auth_token or email, log in again
getting.fail(function (data) {
callback(false);
});
});
}
// function to send POST request to create account
function createAccount(account_attributes, field_attributes, callback) {
getAuthCreds(function (creds) {
$.post("https://securis-debug.herokuapp.com/accounts.json", {
"account": {
"name": account_attributes['name'],
"account_fields_attributes": field_attributes
},
"auth_token": creds.authToken,
"auth_email": creds.authEmail
},
function (data) {
callback(data);
}, "JSON");
});
}
// function to send POST request to create autofill
function createAutofill(attributes, callback) {
getAuthCreds(function (creds) {
$.post("https://securis-debug.herokuapp.com/account_autofills.json", {
"account_autofill": attributes,
"auth_token": creds.authToken,
"auth_email": creds.authEmail
},
function (data) {
callback(data);
}, "JSON");
});
}
// function to retrieve auth_token and auth_email from local storage
function getAuthCreds(callback) {
chrome.storage.local.get(['authToken', 'authEmail'], function (items) {
if (chrome.runtime.lastError) {
console.log(chrome.runtime.lastError);
return;
}
callback(items);
});
}
// function to call createAccount and createAutofill with account_id
function createAutofillCombination(account_attributes, field_attributes) {
createAccount(account_attributes, field_attributes, function (account_data) {});
}
// Message Listener
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.action_name == "create") {
createAutofillCombination(request.account, request.fields);
//return true;
}
else if (request.action_name == "checkAuth") {
verifyAuth(function(auth){
sendResponse(auth);
});
// "return true" keeps connection open for response
return true;
}
else if (request.action_name == "openLogin") {
chrome.tabs.create({'url': 'background.html', 'active':true});
}
else {
// default
sendResponse({});
}
});