Safari: make toolbar icon more robust & performant

Gets rid of background page exceptions
This commit is contained in:
Chris 2015-04-30 20:15:01 -06:00
parent 25222c1439
commit 21d4307a5a
1 changed files with 20 additions and 12 deletions

View File

@ -507,17 +507,22 @@
/******************************************************************************/
vAPI.toolbarItem = false;
safari.application.addEventListener("validate", function(event) {
if(vAPI.toolbarItem === event.target) {
return;
var getIconForWindow = function(target) {
var items = safari.extension.toolbarItems;
for(var i = 0, n = items.length; i < n; i++) {
if(items[i].browserWindow === target) {
return items[i];
}
}
vAPI.toolbarItem = event.target;
}, true);
};
safari.application.addEventListener("activate", function(event) {
if(!(event.target instanceof SafariBrowserTab)) {
return;
var target = event.target;
if(target instanceof SafariBrowserTab) {
vAPI.updateIcon(vAPI.tabs.getTabId(target));
}
else if(target instanceof SafariBrowserWindow) {
vAPI.toolbarItem = getIconForWindow(target);
}
vAPI.updateIcon(vAPI.toolbarItem);
}, true);
/******************************************************************************/
@ -538,9 +543,12 @@
TabIconState.prototype.img = "";
vAPI.tabIconState = { /*tabId: {badge: 0, img: suffix}*/ };
vAPI.updateIcon = function(icon) {
var tabId = vAPI.tabs.getTabId(icon.browserWindow.activeTab),
state = vAPI.tabIconState[tabId];
vAPI.updateIcon = function(tabId) {
var icon = vAPI.toolbarItem;
if(icon === false) {
icon = getIconForWindow(vAPI.tabs.stack[tabId].browserWindow);
}
var state = vAPI.tabIconState[tabId];
if(typeof state === "undefined") {
state = vAPI.tabIconState[tabId] = new TabIconState();
}
@ -554,7 +562,7 @@
}
state.badge = badge || 0;
state.img = (iconStatus === "on" ? "" : "-off");
vAPI.updateIcon(vAPI.toolbarItem);
vAPI.updateIcon(tabId);
};
/******************************************************************************/