notification bar fixes

This commit is contained in:
Kyle Spearrin 2017-01-26 23:12:09 -05:00
parent c2c80bbb26
commit b2aa4fed78
2 changed files with 75 additions and 36 deletions

View File

@ -185,7 +185,6 @@ function buildContextMenu(callback) {
} }
chrome.tabs.onActivated.addListener(function (activeInfo) { chrome.tabs.onActivated.addListener(function (activeInfo) {
checkLoginsToAdd();
refreshBadgeAndMenu(); refreshBadgeAndMenu();
}); });
@ -204,6 +203,7 @@ chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (onUpdatedRan) { if (onUpdatedRan) {
return; return;
} }
console.log('onUpdated');
onUpdatedRan = true; onUpdatedRan = true;
checkLoginsToAdd(); checkLoginsToAdd();
refreshBadgeAndMenu(); refreshBadgeAndMenu();
@ -350,7 +350,7 @@ function messageCurrentTab(command, data) {
}); });
} }
function messageTab(tabId, command, data) { function messageTab(tabId, command, data, callback) {
if (!tabId) { if (!tabId) {
return; return;
} }
@ -363,11 +363,16 @@ function messageTab(tabId, command, data) {
obj['data'] = data; obj['data'] = data;
} }
chrome.tabs.sendMessage(tabId, obj); chrome.tabs.sendMessage(tabId, obj, function () {
if (callback) {
callback();
}
});
} }
function collectPageDetailsForContentScript(tab) { function collectPageDetailsForContentScript(tab) {
chrome.tabs.sendMessage(tab.id, { command: 'collectPageDetails', tabId: tab.id, contentScript: true }, function () { }); chrome.tabs.sendMessage(tab.id, { command: 'collectPageDetails', tabId: tab.id, contentScript: true }, function () {
});
} }
function addLogin(login, tab) { function addLogin(login, tab) {
@ -393,6 +398,7 @@ function addLogin(login, tab) {
username: login.username, username: login.username,
password: login.password, password: login.password,
name: loginDomain, name: loginDomain,
domain: loginDomain,
uri: login.url, uri: login.url,
tabId: tab.id, tabId: tab.id,
expires: new Date((new Date()).getTime() + 30 * 60000) // 30 minutes expires: new Date((new Date()).getTime() + 30 * 60000) // 30 minutes
@ -406,7 +412,7 @@ cleanupLoginsToAdd();
setInterval(cleanupLoginsToAdd, 2 * 60 * 1000); // check every 2 minutes setInterval(cleanupLoginsToAdd, 2 * 60 * 1000); // check every 2 minutes
function cleanupLoginsToAdd() { function cleanupLoginsToAdd() {
var now = new Date(); var now = new Date();
for (var i = loginsToAdd.length - 1; i >= 0 ; i--) { for (var i = loginsToAdd.length - 1; i >= 0; i--) {
if (loginsToAdd[i].expires < now) { if (loginsToAdd[i].expires < now) {
loginsToAdd.splice(i, 1); loginsToAdd.splice(i, 1);
} }
@ -414,7 +420,7 @@ function cleanupLoginsToAdd() {
} }
function removeAddLogin(tab) { function removeAddLogin(tab) {
for (var i = loginsToAdd.length - 1; i >= 0 ; i--) { for (var i = loginsToAdd.length - 1; i >= 0; i--) {
if (loginsToAdd[i].tabId === tab.id) { if (loginsToAdd[i].tabId === tab.id) {
loginsToAdd.splice(i, 1); loginsToAdd.splice(i, 1);
} }
@ -422,35 +428,42 @@ function removeAddLogin(tab) {
} }
function saveAddLogin(tab) { function saveAddLogin(tab) {
for (var i = loginsToAdd.length - 1; i >= 0 ; i--) { for (var i = loginsToAdd.length - 1; i >= 0; i--) {
if (loginsToAdd[i].tabId === tab.id) { if (loginsToAdd[i].tabId === tab.id) {
var loginToAdd = loginsToAdd[i]; var loginToAdd = loginsToAdd[i];
loginsToAdd.splice(i, 1);
loginService.encrypt({ var tabDomain = tldjs.getDomain(tab.url);
id: null, if (tabDomain && tabDomain === loginToAdd.domain) {
folderId: null, loginsToAdd.splice(i, 1);
favorite: false, loginService.encrypt({
name: loginToAdd.name, id: null,
uri: loginToAdd.uri, folderId: null,
username: loginToAdd.username, favorite: false,
password: loginToAdd.password, name: loginToAdd.name,
notes: null uri: loginToAdd.uri,
}).then(function (loginModel) { username: loginToAdd.username,
var login = new Login(loginModel, true); password: loginToAdd.password,
loginService.saveWithServer(login).then(function (login) { notes: null
ga('send', { }).then(function (loginModel) {
hitType: 'event', var login = new Login(loginModel, true);
eventAction: 'Added Login from Notification Bar' loginService.saveWithServer(login).then(function (login) {
ga('send', {
hitType: 'event',
eventAction: 'Added Login from Notification Bar'
});
}); });
}); });
}); messageTab(tab.id, 'closeNotificationBar');
messageTab(tab.id, 'closeNotificationBar'); }
} }
} }
} }
function checkLoginsToAdd(tab) { function checkLoginsToAdd(tab, callback) {
if (!loginsToAdd.length) { if (!loginsToAdd.length) {
if (callback) {
callback();
}
return; return;
} }
@ -468,18 +481,29 @@ function checkLoginsToAdd(tab) {
function check() { function check() {
if (!tab) { if (!tab) {
if (callback) {
callback();
}
return; return;
} }
var tabDomain = tldjs.getDomain(tab.url); var tabDomain = tldjs.getDomain(tab.url);
if (!tabDomain) { if (!tabDomain) {
if (callback) {
callback();
}
return; return;
} }
for (var i = 0; i < loginsToAdd.length; i++) { for (var i = 0; i < loginsToAdd.length; i++) {
// loginsToAdd[x].name is the domain here if (loginsToAdd[i].tabId === tab.id && loginsToAdd[i].domain === tabDomain) {
if (loginsToAdd[i].tabId === tab.id && loginsToAdd[i].name === tabDomain) { messageTab(tab.id, 'openNotificationBar', {
messageTab(tab.id, 'openNotificationBar', { type: 'add' }); type: 'add'
}, function () {
if (callback) {
callback();
}
});
break; break;
} }
} }
@ -501,7 +525,8 @@ function startAutofillPage(login) {
return; return;
} }
chrome.tabs.sendMessage(tabId, { command: 'collectPageDetails', tabId: tabId }, function () { }); chrome.tabs.sendMessage(tabId, { command: 'collectPageDetails', tabId: tabId }, function () {
});
}); });
} }
@ -623,7 +648,9 @@ function logout(expired, callback) {
userService.clearUserIdAndEmail(function () { userService.clearUserIdAndEmail(function () {
loginService.clear(userId, function () { loginService.clear(userId, function () {
folderService.clear(userId, function () { folderService.clear(userId, function () {
chrome.runtime.sendMessage({ command: 'doneLoggingOut', expired: expired }); chrome.runtime.sendMessage({
command: 'doneLoggingOut', expired: expired
});
setIcon(); setIcon();
refreshBadgeAndMenu(); refreshBadgeAndMenu();
callback(); callback();
@ -676,7 +703,8 @@ function fullSync(override) {
syncService.getLastSync(function (lastSync) { syncService.getLastSync(function (lastSync) {
var now = new Date(); var now = new Date();
if (override || !lastSync || (now - lastSync) >= syncInternal) { if (override || !lastSync || (now - lastSync) >= syncInternal) {
syncService.fullSync(override || false, function () { }); syncService.fullSync(override || false, function () {
});
} }
}); });
} }

View File

@ -13,8 +13,7 @@
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(false); closeExistingAndOpenBar(msg.data.type, msg.data.typeData);
openBar(msg.data.type, msg.data.typeData);
sendResponse(); sendResponse();
return true; return true;
} }
@ -121,9 +120,8 @@
} }
} }
function openBar(type, typeData) { function closeExistingAndOpenBar(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;
@ -144,6 +142,18 @@
break; break;
} }
var frame = document.getElementById('bit-notification-bar-iframe');
if (frame && frame.src.indexOf(barPage) >= 0) {
return;
}
closeBar(false);
openBar(type, barPage);
}
function openBar(type, barPage) {
barType = type;
if (!document.body) { if (!document.body) {
return; return;
} }
@ -151,6 +161,7 @@
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: 42px; width: 100%; border: 0;'; iframe.style.cssText = 'height: 42px; width: 100%; border: 0;';
iframe.id = 'bit-notification-bar-iframe';
var frameDiv = document.createElement('div'); var frameDiv = document.createElement('div');
frameDiv.id = 'bit-notification-bar'; frameDiv.id = 'bit-notification-bar';