-
Notifications
You must be signed in to change notification settings - Fork 3
/
background.js
71 lines (63 loc) · 2.8 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
//opens popup on key entry dialog
function openPopup(){
//first ask if the popup is open
chrome.runtime.sendMessage( //a positive answer will cause data to be sent. No receiving end opens popup
{message: 'ready?'},
function(){
if(chrome.runtime.lastError.message.includes("Could not")){
chrome.windows.create({url:'/html/popup.html#' + myEmail, width:816, height:366, type:'popup', focused:true})
}
}
);
}
//opens tabs as directed by the extension and communicates with popup
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
if (request.newtab == "helpTab") { //open Help page in new tab
chrome.tabs.create({url: '/html/help.html'})
}else if(request.newtab == "chatTab") { //open chat page in new tab
chrome.tabs.create({url: 'https://passlok.com/chat/chat.html#' + request.typetoken})
}else if(request.message == "read_data"){ //got data from existing email, now send it to popup
myEmail = request.myEmail;
theirEmail = request.theirEmail;
text2decrypt = request.bodyText;
soleRecipient = request.soleRecipient;
serviceName = request.serviceName;
activeTab = sender.tab;
popupType = 'read';
openPopup() //needed to wake up the popup
}else if(request.message == "compose_data"){ //data from compose box, to be sent to popup
myEmail = request.myEmail;
emailList = request.emailList;
text2decrypt = request.bodyText;
serviceName = request.serviceName;
activeTab = sender.tab;
popupType = 'compose';
openPopup()
}else if(request.message == "popup_ready"){ //needed so the popup can get relayed data after it loads or responds
popupOpen = true;
if(popupType == 'read'){
chrome.runtime.sendMessage({message: 'read_bgdata', myEmail: myEmail, theirEmail: theirEmail, bodyText: text2decrypt, soleRecipient: soleRecipient, activeTab: activeTab, serviceName: serviceName})
}else if(popupType == 'compose'){
chrome.runtime.sendMessage({message: 'compose_bgdata', myEmail: myEmail, emailList: emailList, bodyText: text2decrypt, activeTab: activeTab, serviceName: serviceName})
}
}else if(request.message == "popup_closed"){ //clear cached password etc. Likely never triggered
chrome.storage.session.clear()
}
}
);
//resets cached data after 5 minutes of inactivity
chrome.alarms.onAlarm.addListener(function(result){
if(result.name == "PLEAlarm"){
chrome.storage.session.clear();
chrome.runtime.sendMessage({message: "delete_keys"}, function(response) {
var lastError = chrome.runtime.lastError;
if (lastError) {
console.log(lastError.message);
// 'Could not establish connection. Receiving end does not exist.'
return;
}
// Success, do something with response...
})
}
});