forked from mulbc/vaultPass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
99 lines (87 loc) · 2.65 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
/* eslint-disable no-console */
/* global browser, chrome */
// 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;
}
});
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, visible) {
const matches = [
'[autocomplete="email"]',
'[autocomplete="username"]',
'[autocomplete="nickname"]',
'[type="email"]',
'[name="user_name"]',
'[name="auth[username]"]',
'[type="text"][name="email"]',
'[type="text"][name="mail"]',
'[type="text"][name="nickname"]',
'[type="text"][name="nick"]',
'[type="text"][name="username"]',
'[type="text"][name="login"]',
'[type="text"]',
];
for (let selector of matches) {
const usernameNode = parentNode.querySelector(selector);
if (usernameNode && (visible ? usernameNode.offsetParent : true )) {
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) {
const passwordNode = document.querySelector('input[type=\'password\']');
if (!passwordNode) return;
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);
fillIn(passwordNode, request.password);
}
function fillForm() {
chrome.runtime.sendMessage({
type: 'auto_fill_secrets',
});
}
fillForm();