Update badge for tabs onReplaced since onUpdated doesnt always fire

This commit is contained in:
Kyle Spearrin 2016-09-22 22:04:50 -04:00
parent c3d1d4101e
commit 6b06772f22
1 changed files with 30 additions and 20 deletions

View File

@ -31,18 +31,6 @@ function buildContextMenu() {
contexts: ['all'], contexts: ['all'],
title: 'Copy Password' title: 'Copy Password'
}); });
//chrome.contextMenus.create({
// type: 'separator',
// contexts: ['all']
//});
//chrome.contextMenus.create({
// type: 'normal',
// id: 'generate-password',
// contexts: ['all'],
// title: 'Generate Password'
//});
} }
chrome.tabs.onActivated.addListener(function (activeInfo) { chrome.tabs.onActivated.addListener(function (activeInfo) {
@ -61,16 +49,35 @@ chrome.tabs.onActivated.addListener(function (activeInfo) {
}); });
}); });
var loadedMenuOnUpdate = false; var loadedMenu = false;
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { chrome.tabs.onReplaced.addListener(function (addedTabId, removedTabId) {
loadedMenuOnUpdate = false; chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
buildContextMenu(); var tab = null;
if (tabs.length > 0) {
tab = tabs[0];
}
if (!tab.url) { if (!tab) {
return; return;
} }
var tabDomain = tldjs.getDomain(tab.url); loadMenuAndUpdateBadge(tab.url, tab.id, false);
});
});
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
buildContextMenu();
loadMenuAndUpdateBadge(tab.url, tabId, true);
});
function loadMenuAndUpdateBadge(url, tabId, loadContextMenuOptions) {
loadedMenu = false;
if (!url) {
return;
}
var tabDomain = tldjs.getDomain(url);
if (!tabDomain) { if (!tabDomain) {
return; return;
} }
@ -79,18 +86,21 @@ chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
chrome.browserAction.setBadgeBackgroundColor({ color: '#294e5f' }); chrome.browserAction.setBadgeBackgroundColor({ color: '#294e5f' });
siteService.getAllDecrypted().then(function (sites) { siteService.getAllDecrypted().then(function (sites) {
if (loadedMenuOnUpdate) { if (loadedMenu) {
return; return;
} }
loadedMenuOnUpdate = true; 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) {
count++; count++;
if (loadContextMenuOptions) {
loadSiteContextMenuOptions(sites[i]); loadSiteContextMenuOptions(sites[i]);
} }
} }
}
if (count > 0 && count < 9) { if (count > 0 && count < 9) {
chrome.browserAction.setBadgeText({ chrome.browserAction.setBadgeText({
@ -112,7 +122,7 @@ chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
}); });
} }
}); });
}); }
chrome.contextMenus.onClicked.addListener(function (info, tab) { chrome.contextMenus.onClicked.addListener(function (info, tab) {
if (info.parentMenuItemId === 'autofill' || info.parentMenuItemId === 'copy-username' || if (info.parentMenuItemId === 'autofill' || info.parentMenuItemId === 'copy-username' ||