1
0
mirror of https://github.com/bitwarden/desktop.git synced 2024-11-07 09:20:06 +01:00

Add new login with save from notification bar

This commit is contained in:
Kyle Spearrin 2016-12-30 14:25:57 -05:00
parent 5890449ca9
commit 2310f5ebdf
5 changed files with 142 additions and 38 deletions

View File

@ -57,10 +57,10 @@ chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
messageCurrentTab('closeOverlayPopup'); messageCurrentTab('closeOverlayPopup');
} }
else if (msg.command === 'bgOpenNotificationBar') { else if (msg.command === 'bgOpenNotificationBar') {
messageCurrentTab('openNotificationBar', msg.data); messageTab(sender.tab.id, 'openNotificationBar', msg.data);
} }
else if (msg.command === 'bgCloseNotificationBar') { else if (msg.command === 'bgCloseNotificationBar') {
messageCurrentTab('closeNotificationBar'); messageTab(sender.tab.id, 'closeNotificationBar');
} }
else if (msg.command === 'bgCollectPageDetails') { else if (msg.command === 'bgCollectPageDetails') {
collectPageDetailsForContentScript(sender.tab); collectPageDetailsForContentScript(sender.tab);
@ -68,6 +68,12 @@ chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
else if (msg.command === 'bgAddLogin') { else if (msg.command === 'bgAddLogin') {
addLogin(msg.login, sender.tab); addLogin(msg.login, sender.tab);
} }
else if (msg.command === 'bgAddClose') {
removeAddLogin(sender.tab);
}
else if (msg.command === 'bgAddSave') {
saveAddLogin(sender.tab);
}
else if (msg.command === 'collectPageDetailsResponse') { else if (msg.command === 'collectPageDetailsResponse') {
// messageCurrentTab('openNotificationBar', { type: 'add', typeData: null }); // messageCurrentTab('openNotificationBar', { type: 'add', typeData: null });
if (msg.contentScript) { if (msg.contentScript) {
@ -222,28 +228,23 @@ function loadMenuAndUpdateBadge(url, tabId, loadContextMenuOptions) {
return; return;
} }
var count = 0;
chrome.browserAction.setBadgeBackgroundColor({ color: '#294e5f' }); chrome.browserAction.setBadgeBackgroundColor({ color: '#294e5f' });
siteService.getAllDecrypted().then(function (sites) { siteService.getAllDecryptedForDomain(tabDomain).then(function (sites) {
sortSites(sites); sortSites(sites);
for (var i = 0; i < sites.length; i++) { for (var i = 0; i < sites.length; i++) {
if (sites[i].domain && tabDomain === sites[i].domain) { if (loadContextMenuOptions) {
count++; loadSiteContextMenuOptions(sites[i]);
if (loadContextMenuOptions) {
loadSiteContextMenuOptions(sites[i]);
}
} }
} }
if (count > 0 && count < 9) { if (sites.length > 0 && sites.length < 9) {
chrome.browserAction.setBadgeText({ chrome.browserAction.setBadgeText({
text: count.toString(), text: sites.length.toString(),
tabId: tabId tabId: tabId
}); });
} }
else if (count > 0) { else if (sites.length > 0) {
chrome.browserAction.setBadgeText({ chrome.browserAction.setBadgeText({
text: '9+', text: '9+',
tabId: tabId tabId: tabId
@ -356,14 +357,77 @@ function addLogin(login, tab) {
return; return;
} }
loginsToAdd.push({ siteService.getAllDecryptedForDomain(loginDomain).then(function (sites) {
username: login.username, var match = false;
password: login.password, for (var i = 0; i < sites.length; i++) {
name: loginDomain, if (sites[i] === login.username) {
uri: login.url, match = true;
tabId: tab.id break;
}
}
if (!match) {
// remove any old logins for this tab
removeAddLogin(tab);
loginsToAdd.push({
username: login.username,
password: login.password,
name: loginDomain,
uri: login.url,
tabId: tab.id,
expires: new Date((new Date()).getTime() + 30 * 60000) // 30 minutes
});
checkLoginsToAdd();
}
}); });
checkLoginsToAdd(); }
cleanupLoginsToAdd();
setInterval(cleanupLoginsToAdd, 2 * 60 * 1000); // check every 2 minutes
function cleanupLoginsToAdd() {
var now = new Date();
for (var i = loginsToAdd.length - 1; i >= 0 ; i--) {
if (loginsToAdd[i].expires < now) {
loginsToAdd.splice(i, 1);
}
}
}
function removeAddLogin(tab) {
for (var i = loginsToAdd.length - 1; i >= 0 ; i--) {
if (loginsToAdd[i].tabId === tab.id) {
loginsToAdd.splice(i, 1);
}
}
}
function saveAddLogin(tab) {
for (var i = loginsToAdd.length - 1; i >= 0 ; i--) {
if (loginsToAdd[i].tabId === tab.id) {
var loginToAdd = loginsToAdd[i];
loginsToAdd.splice(i, 1);
siteService.encrypt({
id: null,
folderId: null,
favorite: false,
name: loginToAdd.name,
uri: loginToAdd.uri,
username: loginToAdd.username,
password: loginToAdd.password,
notes: null
}).then(function (siteModel) {
var site = new Site(siteModel, true);
siteService.saveWithServer(site).then(function (site) {
ga('send', {
hitType: 'event',
eventAction: 'Added Site from Notification Bar'
});
});
});
messageTab(tab.id, 'closeNotificationBar');
}
}
} }
function checkLoginsToAdd() { function checkLoginsToAdd() {

View File

@ -1,24 +1,25 @@
!(function () { !(function () {
var pageDetails = [], var pageDetails = [],
formData = []; formData = [],
barType = null;
chrome.runtime.sendMessage({ chrome.runtime.sendMessage({
command: 'bgCollectPageDetails' command: 'bgCollectPageDetails'
}); });
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) { chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
if (msg.command === 'openNotificationBar') { if (msg.command === 'openNotificationBar') {
closeBar(); closeBar(false);
openBar(msg.data.type, msg.data.typeData); openBar(msg.data.type, msg.data.typeData);
sendResponse(); sendResponse();
return true; return true;
} }
else if (msg.command === 'closeNotificationBar') { else if (msg.command === 'closeNotificationBar') {
closeBar(); closeBar(true);
sendResponse(); sendResponse();
return true; return true;
} }
else if (msg.command === 'pageDetails') { else if (msg.command === 'pageDetails') {
console.log(msg.data);
pageDetails.push(msg.data.details); pageDetails.push(msg.data.details);
watchForms(msg.data.forms); watchForms(msg.data.forms);
sendResponse(); sendResponse();
@ -58,20 +59,21 @@
var password = null, var password = null,
username = null, username = null,
passwordId = formData[i].password ? formData[i].password.htmlID : null, passwordId = formData[i].password ? formData[i].password.htmlID : null,
usernameId = formData[i].username ? formData[i].username.htmlID : null; usernameId = formData[i].username ? formData[i].username.htmlID : null,
inputs = document.getElementsByTagName('input');
if (passwordId && passwordId !== '') { if (passwordId && passwordId !== '') {
password = document.getElementById(passwordId); password = document.getElementById(passwordId);
} }
else if (formData[i].password) { else if (formData[i].password) {
password = document.getElementsByTagName('input')[formData[i].password.elementNumber]; password = inputs[formData[i].password.elementNumber];
} }
if (usernameId && usernameId !== '') { if (usernameId && usernameId !== '') {
username = document.getElementById(usernameId); username = document.getElementById(usernameId);
} }
else if (formData[i].username) { else if (formData[i].username) {
username = document.getElementsByTagName('input')[formData[i].username.elementNumber]; username = inputs[formData[i].username.elementNumber];
} }
var login = { var login = {
@ -80,7 +82,7 @@
url: document.URL url: document.URL
}; };
if (login.password && login.password !== '') { if (login.username && login.username !== '' && login.password && login.password !== '') {
chrome.runtime.sendMessage({ chrome.runtime.sendMessage({
command: 'bgAddLogin', command: 'bgAddLogin',
login: login login: login
@ -93,6 +95,7 @@
function openBar(type, typeData) { function openBar(type, typeData) {
var barPage = 'notification/bar.html'; var barPage = 'notification/bar.html';
barType = type;
switch (type) { switch (type) {
case 'info': case 'info':
barPage = barPage + '?info=' + typeData.text; barPage = barPage + '?info=' + typeData.text;
@ -108,10 +111,15 @@
break; break;
case 'add': case 'add':
barPage = barPage + '?add=1'; barPage = barPage + '?add=1';
break;
default: default:
break; break;
} }
if (!document.body) {
return;
}
var iframe = document.createElement('iframe'); var iframe = document.createElement('iframe');
iframe.src = chrome.extension.getURL(barPage); iframe.src = chrome.extension.getURL(barPage);
iframe.style.cssText = 'height: 41px; width: 100%; border: 0;'; iframe.style.cssText = 'height: 41px; width: 100%; border: 0;';
@ -128,7 +136,7 @@
document.body.insertBefore(spacer, document.body.firstChild); document.body.insertBefore(spacer, document.body.firstChild);
} }
function closeBar() { function closeBar(explicitClose) {
var el = document.getElementById('bit-notification-bar'); var el = document.getElementById('bit-notification-bar');
if (el) { if (el) {
el.parentElement.removeChild(el); el.parentElement.removeChild(el);
@ -138,5 +146,19 @@
if (el) { if (el) {
el.parentElement.removeChild(el); el.parentElement.removeChild(el);
} }
if (!explicitClose) {
return;
}
switch (barType) {
case 'add':
chrome.runtime.sendMessage({
command: 'bgAddClose'
});
break;
default:
break;
}
} }
})(); })();

View File

@ -26,16 +26,13 @@
<tbody> <tbody>
<tr> <tr>
<td>Should bitwarden remember this password for you?</td> <td>Should bitwarden remember this password for you?</td>
<td align="right" width="200"> <td align="right" width="150">
<button class="add-save">Save Site</button> <button class="add-save">Yes, Save Now</button>
<button class="add-never">Never</button>
</td> </td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
<div id="template-alert"> <div id="template-alert"></div>
This is an alert.
</div>
</div> </div>
<script src="../lib/jquery/jquery.js"></script> <script src="../lib/jquery/jquery.js"></script>
<script src="bar.js"></script> <script src="bar.js"></script>

View File

@ -7,8 +7,14 @@
setContent(template_add); setContent(template_add);
var add = $('#template-add-clone'), var add = $('#template-add-clone'),
addButton = $('#template-add-clone.add-save'), addButton = $('#template-add-clone .add-save');
neverButton = $('#template-add-clone.add-never');
$(addButton).click(function (e) {
e.preventDefault();
chrome.runtime.sendMessage({
command: 'bgAddSave'
});
});
} }
else if (getQueryVariable('info')) { else if (getQueryVariable('info')) {
setContent(template_alert); setContent(template_alert);
@ -28,7 +34,7 @@
for (var i = 0; i < vars.length; i++) { for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('='); var pair = vars[i].split('=');
if (pair[0] == variable) { if (pair[0] === variable) {
return pair[1]; return pair[1];
} }
} }

View File

@ -130,6 +130,21 @@ function initSiteService() {
}); });
}; };
SiteService.prototype.getAllDecryptedForDomain = function (domain) {
var self = this;
return self.getAllDecrypted().then(function (sites) {
var sitesToReturn = [];
for (var i = 0; i < sites.length; i++) {
if (sites[i].domain === domain) {
sitesToReturn.push(sites[i]);
}
}
return sitesToReturn;
});
};
SiteService.prototype.saveWithServer = function (site) { SiteService.prototype.saveWithServer = function (site) {
var deferred = Q.defer(); var deferred = Q.defer();