From f5c807f2ea0556414e8e166a9ce8f3744a96bf49 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 18 Feb 2015 21:37:54 -0700 Subject: [PATCH 1/4] Safari: better and more reliable setIcon structuring --- platform/safari/vapi-background.js | 66 ++++++++++++++---------------- 1 file changed, 30 insertions(+), 36 deletions(-) diff --git a/platform/safari/vapi-background.js b/platform/safari/vapi-background.js index 756c26f55..0f610452c 100644 --- a/platform/safari/vapi-background.js +++ b/platform/safari/vapi-background.js @@ -27,7 +27,7 @@ (function() { - 'use strict'; + "use strict"; var vAPI = self.vAPI = self.vAPI || {}; @@ -419,22 +419,22 @@ /******************************************************************************/ - // update badge when tab is activated - safari.application.addEventListener('activate', function(e) { - // ignore windows - if(!(e.target instanceof SafariBrowserTab)) { + vAPI.toolbarItem = false; + safari.application.addEventListener("validate", function(event) { + if(vAPI.toolbarItem === event.target) { return; } - - // update the badge - vAPI.setIcon(vAPI.tabs.getTabId(e.target)); + vAPI.toolbarItem = event.target; + }, true); + safari.application.addEventListener("activate", function() { + vAPI.updateIcon(vAPI.toolbarItem); }, true); /******************************************************************************/ // reload the popup when it's opened - safari.application.addEventListener('popover', function(e) { - var w = e.target.contentWindow, body = w.document.body, child; + safari.application.addEventListener("popover", function(event) { + var w = event.target.contentWindow, body = w.document.body, child; while(child = body.firstChild) { body.removeChild(child); } @@ -442,35 +442,29 @@ }, true); /******************************************************************************/ - function TabIcon() {} - TabIcon.prototype.badge = 0; - TabIcon.prototype.img = ""; - vAPI.tabIcons = { /*tabId: {badge: 0, img: suffix}*/ }; + function TabIconState() {} + TabIconState.prototype.badge = 0; + 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]; + if(typeof state === "undefined") { + state = vAPI.tabIconState[tabId] = new TabIconState(); + } + icon.badge = state.badge; + icon.image = vAPI.getURL("img/browsericons/icon16" + state.img + ".png"); + }; vAPI.setIcon = function(tabId, iconStatus, badge) { - var icon = vAPI.tabIcons[tabId]; - if(typeof icon === "undefined") { - icon = vAPI.tabIcons[tabId] = new TabIcon(); - } - - // If we've been passed a badge/iconStatus: - if(typeof badge !== "undefined") { - icon.badge = badge; - icon.img = (iconStatus === "on" ? "" : "-off"); - } - - var items = safari.extension.toolbarItems, - i = items.length; - - var curWindow = safari.application.activeBrowserWindow; - while(i --) { - if(items[i].browserWindow === curWindow) { - items[i].badge = icon.badge; - items[i].image = vAPI.getURL("img/browsericons/icon16" + - icon.img + ".png"); - return; - } + var state = vAPI.tabIconState[tabId]; + if(typeof state === "undefined") { + state = vAPI.tabIconState[tabId] = new TabIconState(); } + state.badge = badge || 0; + state.img = (iconStatus === "on" ? "" : "-off"); + vAPI.updateIcon(vAPI.toolbarItem); }; /******************************************************************************/ From 0a604f8cc78a92578edbdf8958becf31df413a7f Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 18 Feb 2015 21:40:34 -0700 Subject: [PATCH 2/4] Safari code review --- platform/safari/vapi-background.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform/safari/vapi-background.js b/platform/safari/vapi-background.js index 0f610452c..2d2c90066 100644 --- a/platform/safari/vapi-background.js +++ b/platform/safari/vapi-background.js @@ -412,7 +412,7 @@ vAPI.tabs.onClosed(tabId); } - delete vAPI.tabIcons[tabId]; + delete vAPI.tabIconState[tabId]; delete vAPI.tabs.stack[tabId]; } }, true); From d4e85d1d70892322185c7e99bc9a3ee8980a9110 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 19 Feb 2015 11:44:51 -0700 Subject: [PATCH 3/4] Safari: ignore non-tab activation targets --- platform/safari/vapi-background.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/platform/safari/vapi-background.js b/platform/safari/vapi-background.js index 2d2c90066..b63cfeca1 100644 --- a/platform/safari/vapi-background.js +++ b/platform/safari/vapi-background.js @@ -426,7 +426,10 @@ } vAPI.toolbarItem = event.target; }, true); - safari.application.addEventListener("activate", function() { + safari.application.addEventListener("activate", function(event) { + if(!(event.target instanceof SafariBrowserTab)) { + return; + } vAPI.updateIcon(vAPI.toolbarItem); }, true); From 7e77a0c17e46c896224d0b73b700b7aba7628ecc Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 19 Feb 2015 16:36:08 -0700 Subject: [PATCH 4/4] README: fix Safari uBlock URL to save one redirect --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0dd2ac23b..5c3204abc 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ Install from [Firefox Add-ons homepage](https://addons.mozilla.org/en-US/firefox ##### 8.0 or newer only -You can get and install the latest µBlock for Safari [right here](https://chrismatic.io/ublock). +You can get and install the latest µBlock for Safari **[right here](https://chrismatic.io/ublock/)**. µBlock is also available on the [Safari Extension Gallery](https://extensions.apple.com/details/?id=net.gorhill.uBlock-96G4BAKDQ9), although that's not guaranteed to be the latest version.