-
Notifications
You must be signed in to change notification settings - Fork 41
/
content.js
63 lines (58 loc) · 2.26 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
/* global chrome */
// We can only access the TABs DOM with this script.
// It will get the credentials via message passing from the popup
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
if (request.message === 'fill_creds') {
var inputs = document.getElementsByTagName('input')
var passwordNode, usernameNode
for (let i = 0; i < inputs.length; i++) {
if (inputs[i].type === 'password') {
passwordNode = inputs[i]
break
}
}
if (passwordNode === null) {
console.error('Could not find passwordNode')
return
}
// Find the username field next to the password field
for (let testNode = passwordNode.previousSibling; testNode !== null; testNode = testNode.previousSibling) {
if (testNode && testNode.tagName && testNode.tagName === 'INPUT') {
usernameNode = testNode
break
}
}
// Go upwards until we find the form node - then check all input nodes
if (usernameNode !== null) {
for (let form = passwordNode.parentElement; form !== null; form = form.parentElement) {
if (form && form.tagName && form.tagName === 'FORM') {
let inputElements = form.getElementsByTagName('input')
for (let i = 0; i < inputElements.length; i++) {
if (inputElements[i].type === 'text' || inputElements[i].type === 'email') {
usernameNode = inputElements[i]
break
}
}
break
}
}
}
// Go completely crazy and wild guess any visible input field for the username
// https://stackoverflow.com/a/21696585
if (usernameNode !== null) {
let inputElements = document.getElementsByTagName('input')
for (let i = 0; i < inputElements.length; i++) {
if (inputElements[i].offsetParent && (inputElements[i].type === 'text' || inputElements[i].type === 'email')) {
usernameNode = inputElements[i]
break
}
}
}
if (usernameNode !== null) {
usernameNode.value = request.username
passwordNode.value = request.password
}
}
}
)