Debian Codegraph @

browserpass-extension/src/popup/interface.js

raw
1 module.exports = Interface;
2
3 const m = require("mithril");
4 const Moment = require("moment");
5 const SearchInterface = require("./searchinterface");
6 const helpers = require("../helpers");
7
8 const LATEST_NATIVE_APP_VERSION = 3000003;
9
10 /**
11 * Popup main interface
12 *
13 * @since 3.0.0
14 *
15 * @param object settings Settings object
16 * @param array logins Array of available logins
17 * @return void
18 */
19 function Interface(settings, logins) {
20 // public methods
21 this.attach = attach;
22 this.view = view;
23 this.search = search;
24
25 // fields
26 this.settings = settings;
27 this.logins = logins;
28 this.results = [];
29 this.currentDomainOnly = !settings.tab.url.match(/^(chrome|about):/);
30 this.searchPart = new SearchInterface(this);
31
32 // initialise with empty search
33 this.search("");
34 }
35
36 /**
37 * Attach the interface on the given element
38 *
39 * @since 3.0.0
40 *
41 * @param DOMElement element Target element
42 * @return void
43 */
44 function attach(element) {
45 m.mount(element, this);
46 }
47
48 /**
49 * Generates vnodes for render
50 *
51 * @since 3.0.0
52 *
53 * @param function ctl Controller
54 * @param object params Runtime params
55 * @return []Vnode
56 */
57 function view(ctl, params) {
58 var nodes = [];
59 nodes.push(m(this.searchPart));
60
61 nodes.push(
62 m(
63 "div.logins",
64 this.results.map(function (result) {
65 const storeBgColor = result.store.bgColor || result.store.settings.bgColor;
66 const storeColor = result.store.color || result.store.settings.color;
67
68 return m(
69 "div.part.login",
70 {
71 key: result.index,
72 tabindex: 0,
73 onclick: function (e) {
74 var action = e.target.getAttribute("action");
75 if (action) {
76 result.doAction(action);
77 } else {
78 result.doAction("fill");
79 }
80 },
81 onkeydown: keyHandler.bind(result),
82 },
83 [
84 m("div.name", { title: "Fill username / password | <Enter>" }, [
85 m("div.line1", [
86 m(
87 "div.store.badge",
88 {
89 style: `background-color: ${storeBgColor};
90 color: ${storeColor}`,
91 },
92 result.store.name
93 ),
94 m("div.path", [m.trust(result.path)]),
95 result.recent.when > 0
96 ? m("div.recent", {
97 title:
98 "Used here " +
99 result.recent.count +
100 " time" +
101 (result.recent.count > 1 ? "s" : "") +
102 ", last " +
103 Moment(new Date(result.recent.when)).fromNow(),
104 })
105 : null,
106 ]),
107 m("div.line2", [m.trust(result.display)]),
108 ]),
109 m("div.action.copy-user", {
110 tabindex: 0,
111 title: "Copy username | <Ctrl+Shift+C>",
112 action: "copyUsername",
113 }),
114 m("div.action.copy-password", {
115 tabindex: 0,
116 title: "Copy password | <Ctrl+C>",
117 action: "copyPassword",
118 }),
119 m("div.action.details", {
120 tabindex: 0,
121 title: "Open Details | <Ctrl+O>",
122 action: "getDetails",
123 }),
124 ]
125 );
126 })
127 )
128 );
129
130 if (this.settings.version < LATEST_NATIVE_APP_VERSION) {
131 nodes.push(
132 m("div.updates", [
133 m("span", "Update native host app: "),
134 m(
135 "a",
136 {
137 href: "https://github.com/browserpass/browserpass-native#installation",
138 target: "_blank",
139 },
140 "instructions"
141 ),
142 ])
143 );
144 }
145
146 return nodes;
147 }
148
149 /**
150 * Run a search
151 *
152 * @param string searchQuery Search query
153 * @return void
154 */
155 function search(searchQuery) {
156 this.results = helpers.filterSortLogins(this.logins, searchQuery, this.currentDomainOnly);
157 }
158
159 /**
160 * Handle result key presses
161 *
162 * @param Event e Keydown event
163 * @param object this Result object
164 * @return void
165 */
166 function keyHandler(e) {
167 e.preventDefault();
168 var login = e.target.classList.contains("login") ? e.target : e.target.closest(".login");
169 switch (e.code) {
170 case "Tab":
171 var partElement = e.target.closest(".part");
172 var targetElement = e.shiftKey ? "previousElementSibling" : "nextElementSibling";
173 if (partElement[targetElement] && partElement[targetElement].hasAttribute("tabindex")) {
174 partElement[targetElement].focus();
175 } else {
176 document.querySelector(".part.search input[type=text]").focus();
177 }
178 break;
179 case "ArrowDown":
180 if (login.nextElementSibling) {
181 login.nextElementSibling.focus();
182 }
183 break;
184 case "ArrowUp":
185 if (login.previousElementSibling) {
186 login.previousElementSibling.focus();
187 } else {
188 document.querySelector(".part.search input[type=text]").focus();
189 }
190 break;
191 case "ArrowRight":
192 if (e.target.classList.contains("login")) {
193 e.target.querySelector(".action").focus();
194 } else if (e.target.nextElementSibling) {
195 e.target.nextElementSibling.focus();
196 } else {
197 this.doAction("getDetails");
198 }
199 break;
200 case "ArrowLeft":
201 if (e.target.previousElementSibling.classList.contains("action")) {
202 e.target.previousElementSibling.focus();
203 } else {
204 login.focus();
205 }
206 break;
207 case "Enter":
208 if (e.target.hasAttribute("action")) {
209 this.doAction(e.target.getAttribute("action"));
210 } else {
211 this.doAction("fill");
212 }
213 break;
214 case "KeyC":
215 if (e.ctrlKey) {
216 if (e.shiftKey || document.activeElement.classList.contains("copy-user")) {
217 this.doAction("copyUsername");
218 } else {
219 this.doAction("copyPassword");
220 }
221 }
222 break;
223 case "KeyG":
224 if (e.ctrlKey) {
225 this.doAction(e.shiftKey ? "launchInNewTab" : "launch");
226 }
227 break;
228 case "KeyO":
229 if (e.ctrlKey) {
230 this.doAction("getDetails");
231 }
232 break;
233 case "Home": {
234 document.querySelector(".part.search input[type=text]").focus();
235 document.querySelector(".logins").scrollTo(0, 0);
236 window.scrollTo(0, 0);
237 break;
238 }
239 case "End": {
240 let logins = document.querySelectorAll(".login");
241 if (logins.length) {
242 let target = logins.item(logins.length - 1);
243 target.focus();
244 target.scrollIntoView();
245 }
246 break;
247 }
248 }
249 }