1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-10-04 16:47:15 +02:00

Safari: better and more reliable setIcon structuring

This commit is contained in:
Chris 2015-02-18 21:37:54 -07:00
parent ac85a7c979
commit f5c807f2ea

View File

@ -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);
};
/******************************************************************************/