Permalink
Browse files

form filling

  • Loading branch information...
1 parent f2f012a commit c40f1895f0a40b26ea567afa8ecda2b3abd20bb3 @psawaya psawaya committed Nov 20, 2012
Showing with 83 additions and 88 deletions.
  1. +3 −0 data/browser_action.js
  2. +54 −87 data/observer.js
  3. +10 −1 main.js
  4. +16 −0 pageaction_interface.js
View
@@ -54,6 +54,9 @@ $(document).ready(function() {
if (backgroundPage.validatePIN(msg.pin)) {
$('#logins').show();
$('#pin-entry-frame').hide();
+ // The user has successfully authenticatd with their PIN,
+ // so fill in the forms on the current page.
+ backgroundPage.formFillCurrentTab();
}
else {
pinEntryIframe.contentWindow.postMessage({
View
@@ -7,11 +7,12 @@
var inputsList = Array.prototype.slice.call(inputs);
var pwFields = getPwFields(inputsList);
-
+
+ // TODO: Remove this code (and anything else relying on addAutofillToForm)
// If it's a login form, add the autofill listener.
- if (pwFields.length == 1) {
- addAutofillToForm('login',forms[formIdx],pwFields);
- }
+ // if (pwFields.length == 1) {
+ // addAutofillToForm('login',forms[formIdx],pwFields);
+ // }
// Keep in formsByType a map from nummber of password inputs to form elements.
if (formsByType[pwFields.length] === undefined) {
@@ -159,112 +160,79 @@
}
}
- function addAutofillToForm(type,formEl,pwInputs) {
- // Big random number to represent this form uniquely
- var form_id = parseInt(Math.random()*100000);
- var onMsgFunc = function(msg) {
- console.log('in onMsgFunc, saw message of type ', msg.type);
- console.log(msg);
- if (msg.type == 'fill_form' && msg.form_id == form_id) {
- for (var inputIdx in pwInputs) {
- pwInputs[inputIdx].value = msg.password;
- }
- chrome.extension.onMessage.removeListener(onMsgFunc);
- }
- // else if (msg.type == 'do_autologin') {
- // var usernameElem;
- // var passwordElem;
- // passwordElem = findByFieldDescriptor(msg.login.passwordField);
- // if (!passwordElem) {
- // // FIXME: For now, assume there's only one password input in the form,
- // // so if we can't find it via descriptor, no problem!
- // passwordElem = formEl.querySelector('[type="password"]');
- // }
- // usernameElem = findByFieldDescriptor(msg.login.usernameField);
- // if (!usernameElem) {
- // // Couldn't find via descriptor, try heuristically.
- // var formInputs = formEl.getElementsByTagName('input');
- // usernameElem = getUsernameFieldForPasswordField(formInputs,formInputs.indexOf(passwordElem));
- // if (!usernameElem) {
- // // Still can't find a username field for autologin; bail.
- // autologinError("Can't find username field!");
- // return;
- // }
- // }
- // // TODO: "type like a human" with focus, keydown, and blur events.
- // usernameElem.value = msg.login.username;
- // passwordElem.value = msg.login.password;
- // formEl.submit();
- // }
- };
- chrome.extension.onMessage.addListener(onMsgFunc);
- // switch (type) {
- // case "login":
- //
- //
- // break;
- //
- // case "signup":
- //
- // break;
- //
- // case "change_password":
- //
- // break;
- // }
- }
+ // function addAutofillToForm(type,formEl,pwInputs) {
+ // // Big random number to represent this form uniquely
+ // var form_id = parseInt(Math.random()*100000);
+ // var onMsgFunc = function(msg) {
+ // console.log('in onMsgFunc, saw message of type ', msg.type);
+ // console.log(msg);
+ // if (msg.type == 'fill_form' && msg.form_id == form_id) {
+ // for (var inputIdx in pwInputs) {
+ // pwInputs[inputIdx].value = msg.password;
+ // }
+ // chrome.extension.onMessage.removeListener(onMsgFunc);
+ // }
+ // };
+ // chrome.extension.onMessage.addListener(onMsgFunc);
+ // }
function autologinError(errorDescription) {
// TODO: send a message back to chrome saying we screwed up.
}
function masterOnMessageListener(msg) {
+ console.log("got msg", msg);
if (msg.type == 'confirm_form_exists') {
console.log('confirm_form_exists');
if (findByFieldDescriptor(msg.login.formEl)) {
chrome.extension.sendMessage({
type: 'ask_for_autologin',
message: {
- login: msg.login
+ login: msg.login
}
});
}
}
else if (msg.type == 'do_autologin') {
- var usernameElem;
- var passwordElem;
var formEl = formsByType[1][0];
- // TODO: Clean up, and use determineFormType
- console.log('formEl:',formEl);
- // passwordElem = findByFieldDescriptor(msg.login.passwordField);
- passwordElem = formEl.querySelector('[type="password"]');
- if (!passwordElem) {
- // FIXME: For now, assume there's only one password input in the form,
- // so if we can't find it via descriptor, no problem!
- passwordElem = formEl.querySelector('[type="password"]');
- }
- // usernameElem = findByFieldDescriptor(msg.login.usernameField);
-
- if (!usernameElem || true) {
- // Couldn't find via descriptor, try heuristically.
- var formInputs = Array.prototype.slice.call(formEl.getElementsByTagName('input'));
- usernameElem = getUsernameFieldForPasswordField(formInputs,formInputs.indexOf(passwordElem));
- if (!usernameElem) {
- // Still can't find a username field for autologin; bail.
- autologinError("Can't find username field!");
- return;
- }
- }
+ var fields = getLoginFieldsForForm(formEl);
// TODO: "type like a human" with focus, keydown, and blur events.
- usernameElem.value = msg.login.username;
- passwordElem.value = msg.login.password;
-
+ fields.username.value = msg.login.username;
+ fields.password.value = msg.login.password;
formEl.submit();
}
+ else if (msg.type == 'fill_form') {
+ console.log("in fill_form: ", msg);
+ var forms = document.body.getElementsByTagName('form');
+ for (var formIdx = 0; formIdx < forms.length; formIdx++) {
+ if (forms[formIdx].querySelectorAll('input[type="password"]').length > 0) {
+ var fields = getLoginFieldsForForm(forms[formIdx]);
+ fields.username.value = msg.login.username;
+ fields.password.value = msg.login.password;
+ }
+ }
+ }
}
chrome.extension.onMessage.addListener(masterOnMessageListener);
+ function getLoginFieldsForForm(formEl) {
+ var usernameElem;
+ var passwordElem;
+ var formInputs = Array.prototype.slice.call(formEl.getElementsByTagName('input'));
+ passwordElem = formEl.querySelector('[type="password"]');
+ usernameElem = getUsernameFieldForPasswordField(formInputs,formInputs.indexOf(passwordElem));
+ if (!usernameElem) {
+ // Still can't find a username field for autologin; bail.
+ autologinError("Can't find username field!");
+ return;
+ }
+ return {
+ username: usernameElem,
+ password: passwordElem
+ };
+ }
+
function findByFieldDescriptor(descriptor,parent) {
if (descriptor.id) {
return document.getElementById(descriptor.id);
@@ -284,8 +252,7 @@
}
function storeLogin(loginObj) {
- //username,password,site,usernameElement,passwordElement
- console.log(loginObj);
+ // loginObj fields: username,password,site,usernameElement,passwordElement
chrome.extension.sendMessage({
type: 'add_login',
message: loginObj
View
11 main.js
@@ -7,7 +7,7 @@
*/
-// Maps a notificationID to the data extracted from it by an observer.
+// Maps a notificationID to the data extracted from it by an g.
var loginData = {};
// Incrementing counter of last notification ID
@@ -81,7 +81,16 @@ var messageHandlers = {
// Search for logins for this particular site
getLoginsForSite(message.hostname, function(logins) {
if (logins.length == 0) return;
+
if (logins.length == 1) {
+ // Check if the login is not PIN locked. If it's not, form fill the page now.
+ if (!logins[0].pin_locked) {
+ chrome.tabs.sendMessage(tabID,{
+ type: 'fill_form',
+ login: logins[0]
+ });
+ }
+
// If there's only a single login form on the page, we're fine. Otherwise,
// see if we were able to record an id or a name for the form when we observed
// the login.
View
@@ -11,6 +11,22 @@ function getActiveTab(callback) {
});
}
+// This is called when the user has PIN authenticated in the browser action
+// popup. Fills in the forms on the current page.
+function formFillCurrentTab() {
+ getActiveTab(function(tab) {
+ var tabID = tab.id;
+ getPageDataForPopup(function (logins) {
+ // TODO: Find out how to present a user with a choice if they have
+ // more than one login saved for the current domain.
+ chrome.tabs.sendMessage(tabID,{
+ type: 'fill_form',
+ login: logins[0]
+ });
+ });
+ });
+}
+
function getPageDataForPopup(callback) {
getActiveTab(function(tab) {
var newURL = new Uri(tab.url);

0 comments on commit c40f189

Please sign in to comment.