forked from Dmitry1987/vault-chrome-extension
-
Notifications
You must be signed in to change notification settings - Fork 38
/
content.js
194 lines (172 loc) · 4.85 KB
/
content.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* eslint-disable no-console */
/* global browser */
// We can only access the TABs DOM with this script.
// It will get the credentials via message passing from the popup
// It is also responsible to copy strings to the clipboard
browser.runtime.onMessage.addListener((request) => {
switch (request.message) {
case 'copy_to_clipboard':
handleCopyToClipboard(request);
break;
case 'fill_creds':
if (!request.isUserTriggered) {
setTimeout(handleFillCredits.bind(null, request), 800);
} else {
handleFillCredits(request);
}
break;
case 'fetch_token':
handleFetchToken();
break;
}
});
function handleCopyToClipboard(request) {
const el = document.createElement('textarea');
el.value = request.string;
el.setAttribute('readonly', '');
el.style.position = 'absolute';
el.style.left = '-9999px';
document.body.appendChild(el);
const selected =
document.getSelection().rangeCount > 0
? document.getSelection().getRangeAt(0)
: false;
el.select();
document.execCommand('copy');
document.body.removeChild(el);
if (selected) {
document.getSelection().removeAllRanges();
document.getSelection().addRange(selected);
}
}
function findUsernameNodeIn(
parentNode,
checkVisibility,
isUserTriggered = false
) {
const matches = [
'[autocomplete="email"]',
'[autocomplete="username"]',
'[autocomplete="nickname"]',
'input[id="username"]',
'input[id="userid"]',
'input[id="login"]',
'input[id="email"]',
'textarea[id="username"]',
'textarea[id="userid"]',
'textarea[id="login"]',
'textarea[id="email"]',
'[type="email"]',
'[name="user_name"]',
'[name="user"]',
'[name="auth[username]"]',
'[type="text"][name="username"]',
'[type="text"][name="userid"]',
'[type="text"][name="login"]',
'[type="text"][name="email"]',
'[type="text"][name="mail"]',
'[type="text"][name="nickname"]',
'[type="text"][name="nick"]',
];
if (parentNode instanceof HTMLFormElement || isUserTriggered) {
matches.push('[type="text"]');
}
for (const selector of matches) {
const allUsernameNodes = parentNode.querySelectorAll(selector);
let usernameNode = null;
for (const node of allUsernameNodes) {
if (checkVisibility ? isVisible(node) : true) {
usernameNode = node;
break;
}
}
if (usernameNode) {
return usernameNode;
}
}
return null;
}
function createEvent(name) {
const event = document.createEvent('Events');
event.initEvent(name, true, true);
return event;
}
function fillIn(node, value) {
node.focus();
node.value = value;
node.dispatchEvent(createEvent('input'));
node.dispatchEvent(createEvent('change'));
node.blur();
}
function isVisible(node) {
if (!node.offsetParent) {
return false;
}
if (node.style.display === 'none') {
return false;
}
if (node.style.visibility === 'hidden') {
return false;
}
const computedStyle = window.getComputedStyle(node);
const visibility = computedStyle.getPropertyValue('visibility');
return visibility !== 'hidden';
}
function findPasswordInput() {
// eslint-disable-next-line quotes
const passwordNodes = document.querySelectorAll("input[type='password']");
for (const node of passwordNodes) {
if (isVisible(node)) return node;
}
return passwordNodes.length > 0 ? passwordNodes[0] : null;
}
function handleFillCredits(request) {
const passwordNode = findPasswordInput();
// A number of websites now prompt for the password separately
if (passwordNode) {
fillIn(passwordNode, request.password);
}
const formNode = passwordNode?.closest('form');
// Go completely crazy and wild guess any visible input field for the username if empty formNode
// https://stackoverflow.com/a/21696585
const usernameNode = formNode
? findUsernameNodeIn(formNode, true)
: findUsernameNodeIn(document, true, request.isUserTriggered);
if (!usernameNode) return;
fillIn(usernameNode, request.username);
}
function handleFetchToken() {
let element = '';
for (const [, value] of Object.entries(window.localStorage)) {
try {
element = JSON.parse(value);
} catch {
continue;
}
if (
Object.prototype.hasOwnProperty.call(element, 'token') &&
Object.prototype.hasOwnProperty.call(element, 'ttl') &&
Object.prototype.hasOwnProperty.call(element, 'policies')
) {
browser.runtime.sendMessage({
type: 'fetch_token',
token: element.token,
policies: element.policies,
address: window.location.origin,
});
return;
}
}
browser.runtime.sendMessage({
type: 'token_missing',
token: element.token,
policies: element.policies,
address: window.location.origin,
});
}
function fillForm() {
browser.runtime.sendMessage({
type: 'auto_fill_secrets',
});
}
fillForm();