-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathpopup.js
More file actions
149 lines (135 loc) · 4.45 KB
/
popup.js
File metadata and controls
149 lines (135 loc) · 4.45 KB
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
//------------------------------------- Initialisation --------------------------------------//
"use strict";
require("chrome-extension-async");
const Interface = require("./interface");
const DetailsInterface = require("./detailsInterface");
const helpers = require("../helpers");
const m = require("mithril");
run();
//----------------------------------- Function definitions ----------------------------------//
/**
* Handle an error
*
* @since 3.0.0
*
* @param Error error Error object
* @param string type Error type
*/
function handleError(error, type = "error") {
if (type == "error") {
console.log(error);
}
var node = { view: () => m(`div.part.${type}`, error.toString()) };
m.mount(document.body, node);
}
/**
* Run the main popup logic
*
* @since 3.0.0
*
* @return void
*/
async function run() {
try {
var response = await chrome.runtime.sendMessage({ action: "getSettings" });
if (response.status != "ok") {
throw new Error(response.message);
}
var settings = response.settings;
var root = document.getElementsByTagName("html")[0];
root.classList.remove("colors-dark");
root.classList.add(`colors-${settings.theme}`);
if (settings.hasOwnProperty("hostError")) {
throw new Error(settings.hostError.params.message);
}
if (typeof settings.origin === "undefined") {
throw new Error("Unable to retrieve current tab information");
}
// get list of logins
response = await chrome.runtime.sendMessage({ action: "listFiles" });
if (response.status != "ok") {
throw new Error(response.message);
}
const logins = helpers.prepareLogins(response.files, settings);
for (let login of logins) {
login.doAction = withLogin.bind({ settings: settings, login: login });
}
var popup = new Interface(settings, logins);
popup.attach(document.body);
} catch (e) {
handleError(e);
}
}
/**
* Do a login action
*
* @since 3.0.0
*
* @param string action Action to take
* @return void
*/
async function withLogin(action) {
try {
// replace popup with a "please wait" notice
switch (action) {
case "fill":
handleError("Filling login details...", "notice");
break;
case "launch":
handleError("Launching URL...", "notice");
break;
case "launchInNewTab":
handleError("Launching URL in a new tab...", "notice");
break;
case "copyPassword":
handleError("Copying password to clipboard...", "notice");
break;
case "copyUsername":
handleError("Copying username to clipboard...", "notice");
break;
case "copyOTP":
handleError("Copying OTP token to clipboard...", "notice");
break;
case "getDetails":
handleError("Loading entry details...", "notice");
break;
case "clearRecent":
// do nothing
break;
default:
handleError("Please wait...", "notice");
break;
}
// Firefox requires data to be serializable,
// this removes everything offending such as functions
const login = JSON.parse(JSON.stringify(this.login));
// hand off action to background script
var response = await chrome.runtime.sendMessage({
action: action,
login: login,
});
if (response.status != "ok") {
throw new Error(response.message);
} else {
if (response.login && typeof response.login === "object") {
response.login.doAction = withLogin.bind({
settings: this.settings,
login: response.login,
});
}
if (action === "getDetails") {
var details = new DetailsInterface(this.settings, response.login);
details.attach(document.body);
} else
switch (action) {
case "clearRecent":
// don't close the popup
break;
default:
window.close();
}
}
} catch (e) {
handleError(e);
}
}