-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent.js
79 lines (74 loc) · 2.73 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
// Detect username + password fields
// Borrowed from https://stackoverflow.com/a/687874
function getLoginFields() {
console.log("ITFlow: Loaded getloginFields()")
var fieldPairs = [],
pswd = (function(){
var inputs = document.getElementsByTagName('input'),
len = inputs.length,
ret = [];
while (len--) {
if (inputs[len].type === 'password') {
ret[ret.length] = inputs[len];
}
}
return ret;
})(),
pswdLength = pswd.length,
parentForm = function(elem) {
while (elem.parentNode) {
if(elem.parentNode.nodeName.toLowerCase() === 'form') {
return elem.parentNode;
}
elem = elem.parentNode;
}
};
while (pswdLength--) {
var curPswdField = pswd[pswdLength],
parentForm = parentForm(curPswdField),
curField = curPswdField;
if (parentForm) {
var inputs = parentForm.getElementsByTagName('input');
for (var i = 0; i < inputs.length; i++) {
if (inputs[i] !== curPswdField && inputs[i].type === 'text') {
fieldPairs[fieldPairs.length] = [inputs[i], curPswdField];
break;
}
}
}
}
return fieldPairs;
}
// Call the above function & check if we've found login fields.
var loginFields = getLoginFields()[0];
if (loginFields) {
console.log ("ITFlow: Extension clicked/activated and login fields found.")
// Get the login URL set in options & formulate the query URL
chrome.storage.sync.get(['itflowurl'], function(result) {
var query = window.location.hostname
var url = result.itflowurl+query;
console.log("ITFlow: Passing data to background service.. Query: " + query + " to " + result.itflowurl + " as " + url);
// Setup a connection to the background service
// Send the current URL
// If it returns back valid data, fill the user+pass fields
var port = chrome.runtime.connect({name: "itflow"});
port.postMessage({url: url});
port.onMessage.addListener(function(msg) {
if (msg.user){
loginFields[0].value = msg.user;
console.log("ITFlow: Filled username");
}
if (msg.pass){
loginFields[1].value = msg.pass;
console.log("ITFlow: Filled password");
}
if (msg.message){
alert(msg.message);
console.log("ITFlow: Received message from server. Message: " + msg.message);
}
});
});
}
else {
console.log("ITFlow: No login fields found. Quitting.")
}