From 78ec69a9c8a1e70a5cd0c60d5df3c0f822d482a3 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Sat, 17 Sep 2016 00:00:17 -0400 Subject: [PATCH] move autofill script building method to service --- src/background.js | 1 + src/manifest.json | 1 + src/popup/app/current/currentController.js | 118 +------------------- src/popup/app/services/backgroundService.js | 3 + src/services/autofillService.js | 117 +++++++++++++++++++ 5 files changed, 125 insertions(+), 115 deletions(-) create mode 100644 src/services/autofillService.js diff --git a/src/background.js b/src/background.js index c0a6c3b008..46a6d8a545 100644 --- a/src/background.js +++ b/src/background.js @@ -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(); diff --git a/src/manifest.json b/src/manifest.json index f7668088db..49c89f0a34 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -38,6 +38,7 @@ "services/folderService.js", "services/siteService.js", "services/syncService.js", + "services/autofillService.js", "background.js" ] }, diff --git a/src/popup/app/current/currentController.js b/src/popup/app/current/currentController.js index 3194257bf3..7845e36055 100644 --- a/src/popup/app/current/currentController.js +++ b/src/popup/app/current/currentController.js @@ -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; - } }); diff --git a/src/popup/app/services/backgroundService.js b/src/popup/app/services/backgroundService.js index 068f6b56c7..ce1b96ca1b 100644 --- a/src/popup/app/services/backgroundService.js +++ b/src/popup/app/services/backgroundService.js @@ -24,4 +24,7 @@ }) .factory('tldjs', function () { return chrome.extension.getBackgroundPage().tldjs; + }) + .factory('autofillService', function () { + return chrome.extension.getBackgroundPage().autofillService; }); diff --git a/src/services/autofillService.js b/src/services/autofillService.js new file mode 100644 index 0000000000..0f6c5db7ea --- /dev/null +++ b/src/services/autofillService.js @@ -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; + }; +};