From f6849ed04c822975e5326790d4f8743ff976f339 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Sat, 17 Sep 2016 00:13:12 -0400 Subject: [PATCH] implement autofill functionality into context menu --- src/background.js | 39 ++++++++++++++++++++-- src/popup/app/current/currentController.js | 2 +- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/background.js b/src/background.js index 46a6d8a545..05164b545f 100644 --- a/src/background.js +++ b/src/background.js @@ -106,12 +106,16 @@ chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { }); chrome.contextMenus.onClicked.addListener(function (info, tab) { - if (info.parentMenuItemId === 'copy-username' || info.parentMenuItemId === 'copy-password') { + if (info.parentMenuItemId === 'autofill' || info.parentMenuItemId === 'copy-username' || + info.parentMenuItemId === 'copy-password') { var id = info.menuItemId.split('_')[1]; siteService.getAllDecrypted().then(function (sites) { for (var i = 0; i < sites.length; i++) { if (sites[i].id == id) { - if (info.parentMenuItemId === 'copy-username') { + if (info.parentMenuItemId === 'autofill') { + autofillPage(sites[i]); + } + else if (info.parentMenuItemId === 'copy-username') { copyToClipboard(sites[i].username); } else if (info.parentMenuItemId === 'copy-password') { @@ -124,6 +128,37 @@ chrome.contextMenus.onClicked.addListener(function (info, tab) { } }); +function autofillPage(site) { + chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) { + var tabId = null; + if (tabs.length > 0) { + tabId = tabs[0].id; + } + else { + return; + } + + if (!tabId) { + return; + } + + chrome.tabs.sendMessage(tabId, { command: 'collectPageDetails' }, function (pageDetails) { + var fillScript = null; + if (site && pageDetails) { + fillScript = autofillService.generateFillScript(pageDetails, site.username, site.password); + } + + if (tabId && fillScript) { + chrome.tabs.sendMessage(tabId, { + command: 'fillForm', + fillScript: fillScript + }); + } + }); + }); + +} + function sortSites(sites) { sites.sort(function (a, b) { var nameA = (a.name + '_' + a.username).toUpperCase(); diff --git a/src/popup/app/current/currentController.js b/src/popup/app/current/currentController.js index 7845e36055..5ba5d39d1d 100644 --- a/src/popup/app/current/currentController.js +++ b/src/popup/app/current/currentController.js @@ -69,7 +69,7 @@ angular command: 'fillForm', fillScript: fillScript }, function () { - $window.close() + $window.close(); }); } else {