From 21d4307a5adff7a00f6f21518a6b0d59bd6eb3b5 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 30 Apr 2015 20:15:01 -0600 Subject: [PATCH] Safari: make toolbar icon more robust & performant Gets rid of background page exceptions --- platform/safari/vapi-background.js | 32 +++++++++++++++++++----------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/platform/safari/vapi-background.js b/platform/safari/vapi-background.js index c63ebe8d6..e113ab685 100644 --- a/platform/safari/vapi-background.js +++ b/platform/safari/vapi-background.js @@ -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); }; /******************************************************************************/