Fixed issues where onReplaced and onUpdated are called multiple times

This commit is contained in:
Kyle Spearrin 2016-10-25 23:03:21 -04:00
parent 0e2e17f2e7
commit 124ac06419
3 changed files with 58 additions and 50 deletions

View File

@ -72,48 +72,65 @@ if (chrome.runtime.onInstalled) {
}); });
} }
function buildContextMenu() { function buildContextMenu(callback) {
chrome.contextMenus.removeAll(); chrome.contextMenus.removeAll(function () {
chrome.contextMenus.create({ chrome.contextMenus.create({
type: 'normal', type: 'normal',
id: 'autofill', id: 'autofill',
contexts: ['all'], contexts: ['all'],
title: i18nService.autoFill title: i18nService.autoFill
}); }, function () {
chrome.contextMenus.create({
type: 'normal',
id: 'copy-username',
contexts: ['all'],
title: i18nService.copyUsername
}, function () {
chrome.contextMenus.create({
type: 'normal',
id: 'copy-password',
contexts: ['all'],
title: i18nService.copyPassword
}, function () {
chrome.contextMenus.create({
type: 'separator'
});
chrome.contextMenus.create({ chrome.contextMenus.create({
type: 'normal', type: 'normal',
id: 'copy-username', id: 'generate-password',
contexts: ['all'], contexts: ['all'],
title: i18nService.copyUsername title: i18nService.generatePasswordCopied
}); }, function () {
if (callback) {
chrome.contextMenus.create({ callback();
type: 'normal', }
id: 'copy-password', });
contexts: ['all'], });
title: i18nService.copyPassword });
}); });
chrome.contextMenus.create({
type: 'separator'
});
chrome.contextMenus.create({
type: 'normal',
id: 'generate-password',
contexts: ['all'],
title: i18nService.generatePasswordCopied
}); });
} }
chrome.tabs.onActivated.addListener(function (activeInfo) { chrome.tabs.onActivated.addListener(function (activeInfo) {
buildContextMenu();
refreshBadgeAndMenu(); refreshBadgeAndMenu();
}); });
var loadedMenu = false; var onReplacedRan = false;
chrome.tabs.onReplaced.addListener(function (addedTabId, removedTabId) { chrome.tabs.onReplaced.addListener(function (addedTabId, removedTabId) {
if (onReplacedRan) {
return;
}
onReplacedRan = true;
refreshBadgeAndMenu();
});
var onUpdatedRan = false;
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (onUpdatedRan) {
return;
}
onUpdatedRan = true;
refreshBadgeAndMenu(); refreshBadgeAndMenu();
}); });
@ -128,18 +145,14 @@ function refreshBadgeAndMenu() {
return; return;
} }
buildContextMenu(); buildContextMenu(function () {
loadMenuAndUpdateBadge(tab.url, tab.id, true); loadMenuAndUpdateBadge(tab.url, tab.id, true);
onUpdatedRan = onReplacedRan = false;
});
}); });
} }
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
buildContextMenu();
loadMenuAndUpdateBadge(tab.url, tabId, true);
});
function loadMenuAndUpdateBadge(url, tabId, loadContextMenuOptions) { function loadMenuAndUpdateBadge(url, tabId, loadContextMenuOptions) {
loadedMenu = false;
if (!url) { if (!url) {
return; return;
} }
@ -153,11 +166,6 @@ function loadMenuAndUpdateBadge(url, tabId, loadContextMenuOptions) {
chrome.browserAction.setBadgeBackgroundColor({ color: '#294e5f' }); chrome.browserAction.setBadgeBackgroundColor({ color: '#294e5f' });
siteService.getAllDecrypted().then(function (sites) { siteService.getAllDecrypted().then(function (sites) {
if (loadedMenu) {
return;
}
loadedMenu = true;
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 (sites[i].domain && tabDomain === sites[i].domain) {

View File

@ -71,12 +71,12 @@ function initFolderService() {
cryptoService.getKey(false, function (key) { cryptoService.getKey(false, function (key) {
if (!key) { if (!key) {
deferred.reject(); deferred.reject();
return deferred.promise; return;
} }
if (self.decryptedFolderCache) { if (self.decryptedFolderCache) {
deferred.resolve(self.decryptedFolderCache); deferred.resolve(self.decryptedFolderCache);
return deferred.promise; return;
} }
var promises = []; var promises = [];

View File

@ -88,12 +88,12 @@ function initSiteService() {
cryptoService.getKey(false, function (key) { cryptoService.getKey(false, function (key) {
if (!key) { if (!key) {
deferred.reject(); deferred.reject();
return deferred.promise; return;
} }
if (self.decryptedSiteCache) { if (self.decryptedSiteCache) {
deferred.resolve(self.decryptedSiteCache); deferred.resolve(self.decryptedSiteCache);
return deferred.promise; return;
} }
var promises = []; var promises = [];