mirror of
https://github.com/bitwarden/desktop.git
synced 2024-10-01 04:27:40 +02:00
Added autofill support for iframed login forms. #20
This commit is contained in:
parent
3f7f8b781c
commit
2b2794dd7e
@ -26,7 +26,11 @@ chrome.commands.onCommand.addListener(function (command) {
|
||||
}
|
||||
});
|
||||
|
||||
var loadMenuRan = false;
|
||||
var loadMenuRan = false,
|
||||
siteToAutoFill = null,
|
||||
pageDetailsToAutoFill = [],
|
||||
autofillTimeout = null;
|
||||
|
||||
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
|
||||
if (msg.command === 'loggedOut' || msg.command === 'loggedIn' || msg.command === 'unlocked' || msg.command === 'locked') {
|
||||
if (loadMenuRan) {
|
||||
@ -51,6 +55,11 @@ chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
|
||||
else if (msg.command === 'bgCloseOverlayPopup') {
|
||||
messageCurrentTab('closeOverlayPopup');
|
||||
}
|
||||
else if (msg.command === 'collectPageDetailsResponse') {
|
||||
clearTimeout(autofillTimeout);
|
||||
pageDetailsToAutoFill.push({ frameId: sender.frameId, tabId: msg.tabId, details: msg.details });
|
||||
autofillTimeout = setTimeout(autofillPage, 300);
|
||||
}
|
||||
});
|
||||
|
||||
setIcon();
|
||||
@ -259,7 +268,7 @@ chrome.contextMenus.onClicked.addListener(function (info, tab) {
|
||||
hitType: 'event',
|
||||
eventAction: 'Autofilled From Context Menu'
|
||||
});
|
||||
autofillPage(sites[i]);
|
||||
startAutofillPage(sites[i]);
|
||||
}
|
||||
else if (info.parentMenuItemId === 'copy-username') {
|
||||
ga('send', {
|
||||
@ -310,7 +319,8 @@ function messageCurrentTab(command, data) {
|
||||
});
|
||||
}
|
||||
|
||||
function autofillPage(site) {
|
||||
function startAutofillPage(site) {
|
||||
siteToAutoFill = site;
|
||||
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
|
||||
var tabId = null;
|
||||
if (tabs.length > 0) {
|
||||
@ -324,21 +334,49 @@ function autofillPage(site) {
|
||||
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 && fillScript.script) {
|
||||
chrome.tabs.sendMessage(tabId, {
|
||||
command: 'fillForm',
|
||||
fillScript: fillScript
|
||||
});
|
||||
}
|
||||
});
|
||||
chrome.tabs.sendMessage(tabId, { command: 'collectPageDetails', tabId: tabId }, function () { });
|
||||
});
|
||||
}
|
||||
|
||||
function autofillPage() {
|
||||
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;
|
||||
}
|
||||
|
||||
var fillScript = null;
|
||||
if (siteToAutoFill && pageDetailsToAutoFill && pageDetailsToAutoFill.length) {
|
||||
for (var i = 0; i < pageDetailsToAutoFill.length; i++) {
|
||||
// make sure we're still on correct tab
|
||||
if (pageDetailsToAutoFill[i].tabId != tabId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var fillScript = autofillService.generateFillScript(pageDetailsToAutoFill[i].details, siteToAutoFill.username,
|
||||
siteToAutoFill.password);
|
||||
if (tabId && fillScript && fillScript.script && fillScript.script.length) {
|
||||
chrome.tabs.sendMessage(tabId, {
|
||||
command: 'fillForm',
|
||||
fillScript: fillScript
|
||||
}, {
|
||||
frameId: pageDetailsToAutoFill[i].frameId
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// reset
|
||||
siteToAutoFill = null;
|
||||
pageDetailsToAutoFill = [];
|
||||
});
|
||||
}
|
||||
|
||||
function sortSites(sites) {
|
||||
|
@ -81,7 +81,15 @@
|
||||
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
|
||||
if (msg.command === 'collectPageDetails') {
|
||||
var pageDetails = collect(document);
|
||||
sendResponse(JSON.parse(pageDetails));
|
||||
var pageDetailsObj = JSON.parse(pageDetails);
|
||||
console.log(pageDetailsObj);
|
||||
//sendResponse(pageDetailsObj);
|
||||
chrome.runtime.sendMessage({
|
||||
command: 'collectPageDetailsResponse',
|
||||
tabId: msg.tabId,
|
||||
details: pageDetailsObj
|
||||
});
|
||||
sendResponse();
|
||||
return true;
|
||||
}
|
||||
else if (msg.command === 'fillForm') {
|
||||
|
@ -15,6 +15,7 @@
|
||||
},
|
||||
"content_scripts": [
|
||||
{
|
||||
"all_frames": true,
|
||||
"js": [ "content/autoFill.js" ],
|
||||
"matches": [ "http://*/*", "https://*/*", "file:///*" ],
|
||||
"run_at": "document_start"
|
||||
|
@ -5,7 +5,7 @@ angular
|
||||
$analytics, i18nService) {
|
||||
$scope.i18n = i18nService;
|
||||
|
||||
var pageDetails = null,
|
||||
var pageDetails = [],
|
||||
tabId = null,
|
||||
url = null,
|
||||
domain = null,
|
||||
@ -32,8 +32,7 @@ angular
|
||||
return;
|
||||
}
|
||||
|
||||
chrome.tabs.sendMessage(tabId, { command: 'collectPageDetails' }, function (details) {
|
||||
pageDetails = details;
|
||||
chrome.tabs.sendMessage(tabId, { command: 'collectPageDetails', tabId: tabId }, function () {
|
||||
canAutofill = true;
|
||||
});
|
||||
|
||||
@ -71,21 +70,31 @@ angular
|
||||
};
|
||||
|
||||
$scope.fillSite = function (site) {
|
||||
var fillScript = null;
|
||||
if (site && canAutofill && pageDetails) {
|
||||
fillScript = autofillService.generateFillScript(pageDetails, site.username, site.password);
|
||||
var didAutofill = false;
|
||||
|
||||
if (site && canAutofill && pageDetails && pageDetails.length) {
|
||||
for (var i = 0; i < pageDetails.length; i++) {
|
||||
if (pageDetails[i].tabId != tabId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var fillScript = autofillService.generateFillScript(pageDetails[i].details, site.username, site.password);
|
||||
if (tabId && fillScript && fillScript.script && fillScript.script.length) {
|
||||
didAutofill = true;
|
||||
$analytics.eventTrack('Autofilled');
|
||||
chrome.tabs.sendMessage(tabId, {
|
||||
command: 'fillForm',
|
||||
fillScript: fillScript
|
||||
}, {
|
||||
frameId: pageDetails[i].frameId
|
||||
}, function () {
|
||||
$window.close();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (tabId && fillScript && fillScript.script && fillScript.script.length) {
|
||||
$analytics.eventTrack('Autofilled');
|
||||
chrome.tabs.sendMessage(tabId, {
|
||||
command: 'fillForm',
|
||||
fillScript: fillScript
|
||||
}, function () {
|
||||
$window.close();
|
||||
});
|
||||
}
|
||||
else {
|
||||
if (!didAutofill) {
|
||||
$analytics.eventTrack('Autofilled Error');
|
||||
toastr.error(i18nService.autofillError);
|
||||
}
|
||||
@ -106,4 +115,8 @@ angular
|
||||
setTimeout(loadVault, 500);
|
||||
}
|
||||
});
|
||||
|
||||
$scope.$on('collectPageDetailsResponse', function (event, details) {
|
||||
pageDetails.push(details);
|
||||
});
|
||||
});
|
||||
|
@ -29,5 +29,12 @@ angular
|
||||
$state.go('home');
|
||||
});
|
||||
}
|
||||
else if (msg.command === 'collectPageDetailsResponse') {
|
||||
$scope.$broadcast('collectPageDetailsResponse', {
|
||||
frameId: sender.frameId,
|
||||
tabId: msg.tabId,
|
||||
details: msg.details
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user