forked from mulbc/vaultPass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
151 lines (137 loc) · 3.87 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
/* 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':
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) {
const matches = [
'[autocomplete="email"]',
'[autocomplete="username"]',
'[autocomplete="nickname"]',
'[id="username"]',
'[id="userid"]',
'[id="login"]',
'[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"]',
'[type="text"]',
];
for (let selector of matches) {
const allUsernameNodes = parentNode.querySelectorAll(selector);
let usernameNode = null;
for (let node of allUsernameNodes) {
if (checkVisibility ? node.offsetParent : 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 handleFillCredits(request) {
// eslint-disable-next-line quotes
const passwordNode = document.querySelector("input[type='password']");
// 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)
: findUsernameNodeIn(document, true);
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();