move autofill script building method to service

This commit is contained in:
Kyle Spearrin 2016-09-17 00:00:17 -04:00
parent 69ea468716
commit 78ec69a9c8
5 changed files with 125 additions and 115 deletions

View File

@ -5,6 +5,7 @@ var userService = new UserService(tokenService, apiService);
var siteService = new SiteService(cryptoService, userService, apiService);
var folderService = new FolderService(cryptoService, userService, apiService);
var syncService = new SyncService(siteService, folderService, userService, apiService);
var autofillService = new AutofillService();
function buildContextMenu() {
chrome.contextMenus.removeAll();

View File

@ -38,6 +38,7 @@
"services/folderService.js",
"services/siteService.js",
"services/syncService.js",
"services/autofillService.js",
"background.js"
]
},

View File

@ -1,7 +1,7 @@
angular
.module('bit.current')
.controller('currentController', function ($scope, siteService, tldjs, toastr, $q, $window, $state) {
.controller('currentController', function ($scope, siteService, tldjs, toastr, $q, $window, $state, autofillService) {
var pageDetails = null,
tabId = null,
url = null,
@ -32,7 +32,7 @@ angular
var sitePromise = $q.when(siteService.getAllDecrypted());
sitePromise.then(function (sites) {
for (var i = 0; i < sites.length; i++) {
if (sites[i].domain && sites[i].domain == domain) {
if (sites[i].domain && sites[i].domain === domain) {
filteredSites.push(sites[i]);
}
}
@ -61,7 +61,7 @@ angular
$scope.fillSite = function (site) {
var fillScript = null;
if (site && canAutofill && pageDetails) {
fillScript = makeFillScript(site.username, site.password);
fillScript = autofillService.generateFillScript(pageDetails, site.username, site.password);
}
if (tabId && fillScript) {
@ -76,116 +76,4 @@ angular
toastr.error('Unable to auto-fill the selected site. Copy/paste your username and/or password instead.');
}
};
function makeFillScript(fillUsername, fillPassword) {
if (!pageDetails) {
return null;
}
var fillScript = {
documentUUID: pageDetails.documentUUID,
script: [],
autosubmit: null,
properties: {},
options: {},
metadata: {}
};
var passwordFields = [];
for (var i = 0; i < pageDetails.fields.length; i++) {
if (pageDetails.fields[i].type === 'password') {
passwordFields.push(pageDetails.fields[i]);
}
}
var passwordForms = [];
for (var formKey in pageDetails.forms) {
for (var j = 0; j < passwordFields.length; j++) {
if (formKey === passwordFields[j].form) {
passwordForms.push(pageDetails.forms[formKey]);
break;
}
}
}
if (!passwordForms.length) {
return null;
}
var loginForm = null;
if (passwordForms.length > 1) {
// More than one form with a password field is on the page.
// This usually occurs when a website has a login and signup form on the same page.
// Let's try to guess which one is the login form.
// First let's try to guess the correct login form by examining the form attribute strings
// for common login form attribute.
for (i = 0; i < passwordForms.length; i++) {
var formDescriptor = (passwordForms[i].htmlName + '~' + passwordForms[i].htmlId +
'~' + passwordForms[i].htmlAction).toLowerCase();
if (formDescriptor.indexOf('login') !== -1 || formDescriptor.indexOf('log-in') !== -1 ||
formDescriptor.indexOf('signin') !== -1 || formDescriptor.indexOf('sign-in') !== -1 ||
formDescriptor.indexOf('logon') !== -1 || formDescriptor.indexOf('log-on') !== -1) {
loginForm = passwordForms[i];
break;
}
}
if (!loginForm) {
// Next we can try to find the login form that only has one password field. Typically
// a registration form may have two password fields for password confirmation.
for (i = 0; i < passwordForms.length; i++) {
var passwordFieldCount = 0;
for (j = 0; j < passwordFields.length; j++) {
if (passwordForms[i].opid === passwordFields[j].form) {
passwordFieldCount++;
}
}
if (passwordFieldCount === 1) {
loginForm = passwordForms[i];
break;
}
}
}
}
if (!loginForm) {
loginForm = passwordForms[0];
}
var password = null;
for (i = 0; i < pageDetails.fields.length; i++) {
var f = pageDetails.fields[i];
if (f.form === loginForm.opid && f.type === 'password') {
password = f;
break;
}
}
var username = null;
for (i = 0; i < pageDetails.fields.length; i++) {
f = pageDetails.fields[i];
if (f.form === loginForm.opid && (f.type === 'text' || f.type === 'email')
&& f.elementNumber < password.elementNumber) {
username = f;
}
}
if (username) {
fillScript.script.push(['click_on_opid', username.opid]);
fillScript.script.push(['fill_by_opid', username.opid, fillUsername]);
}
fillScript.script.push(['click_on_opid', password.opid]);
fillScript.script.push(['fill_by_opid', password.opid, fillPassword]);
if (loginForm.htmlAction) {
fillScript.autosubmit = { focusOpid: password.opid };
}
return fillScript;
}
});

View File

@ -24,4 +24,7 @@
})
.factory('tldjs', function () {
return chrome.extension.getBackgroundPage().tldjs;
})
.factory('autofillService', function () {
return chrome.extension.getBackgroundPage().autofillService;
});

View File

@ -0,0 +1,117 @@
function AutofillService() {
initAutofill();
};
function initAutofill() {
AutofillService.prototype.generateFillScript = function (pageDetails, fillUsername, fillPassword) {
if (!pageDetails) {
return null;
}
var fillScript = {
documentUUID: pageDetails.documentUUID,
script: [],
autosubmit: null,
properties: {},
options: {},
metadata: {}
};
var passwordFields = [];
for (var i = 0; i < pageDetails.fields.length; i++) {
if (pageDetails.fields[i].type === 'password') {
passwordFields.push(pageDetails.fields[i]);
}
}
var passwordForms = [];
for (var formKey in pageDetails.forms) {
for (var j = 0; j < passwordFields.length; j++) {
if (formKey === passwordFields[j].form) {
passwordForms.push(pageDetails.forms[formKey]);
break;
}
}
}
if (!passwordForms.length) {
return null;
}
var loginForm = null;
if (passwordForms.length > 1) {
// More than one form with a password field is on the page.
// This usually occurs when a website has a login and signup form on the same page.
// Let's try to guess which one is the login form.
// First let's try to guess the correct login form by examining the form attribute strings
// for common login form attribute.
for (i = 0; i < passwordForms.length; i++) {
var formDescriptor = (passwordForms[i].htmlName + '~' + passwordForms[i].htmlId +
'~' + passwordForms[i].htmlAction).toLowerCase();
if (formDescriptor.indexOf('login') !== -1 || formDescriptor.indexOf('log-in') !== -1 ||
formDescriptor.indexOf('signin') !== -1 || formDescriptor.indexOf('sign-in') !== -1 ||
formDescriptor.indexOf('logon') !== -1 || formDescriptor.indexOf('log-on') !== -1) {
loginForm = passwordForms[i];
break;
}
}
if (!loginForm) {
// Next we can try to find the login form that only has one password field. Typically
// a registration form may have two password fields for password confirmation.
for (i = 0; i < passwordForms.length; i++) {
var passwordFieldCount = 0;
for (j = 0; j < passwordFields.length; j++) {
if (passwordForms[i].opid === passwordFields[j].form) {
passwordFieldCount++;
}
}
if (passwordFieldCount === 1) {
loginForm = passwordForms[i];
break;
}
}
}
}
if (!loginForm) {
loginForm = passwordForms[0];
}
var password = null;
for (i = 0; i < pageDetails.fields.length; i++) {
var f = pageDetails.fields[i];
if (f.form === loginForm.opid && f.type === 'password') {
password = f;
break;
}
}
var username = null;
for (i = 0; i < pageDetails.fields.length; i++) {
f = pageDetails.fields[i];
if (f.form === loginForm.opid && (f.type === 'text' || f.type === 'email')
&& f.elementNumber < password.elementNumber) {
username = f;
}
}
if (username) {
fillScript.script.push(['click_on_opid', username.opid]);
fillScript.script.push(['fill_by_opid', username.opid, fillUsername]);
}
fillScript.script.push(['click_on_opid', password.opid]);
fillScript.script.push(['fill_by_opid', password.opid, fillPassword]);
if (loginForm.htmlAction) {
fillScript.autosubmit = { focusOpid: password.opid };
}
return fillScript;
};
};