Make vAPI.setIcon less Chromium specific

This commit is contained in:
Deathamns 2014-12-26 21:41:44 +01:00
parent b301ac031e
commit b8c943d539
5 changed files with 47 additions and 61 deletions

View File

@ -245,7 +245,7 @@ vAPI.tabs.injectScript = function(tabId, details, callback) {
// Since we may be called asynchronously, the tab id may not exist // Since we may be called asynchronously, the tab id may not exist
// anymore, so this ensures it does still exist. // anymore, so this ensures it does still exist.
vAPI.setIcon = function(tabId, img, badge) { vAPI.setIcon = function(tabId, iconStatus, badge) {
tabId = parseInt(tabId, 10); tabId = parseInt(tabId, 10);
var onIconReady = function() { var onIconReady = function() {
if ( vAPI.lastError() ) { if ( vAPI.lastError() ) {
@ -253,10 +253,18 @@ vAPI.setIcon = function(tabId, img, badge) {
} }
chrome.browserAction.setBadgeText({ tabId: tabId, text: badge }); chrome.browserAction.setBadgeText({ tabId: tabId, text: badge });
if ( badge !== '' ) { if ( badge !== '' ) {
chrome.browserAction.setBadgeBackgroundColor({ tabId: tabId, color: '#666' }); chrome.browserAction.setBadgeBackgroundColor({
tabId: tabId,
color: '#666'
});
} }
}; };
chrome.browserAction.setIcon({ tabId: tabId, path: img }, onIconReady);
var iconPaths = iconStatus === 'on' ?
{ '19': 'img/browsericons/icon19.png', '38': 'img/browsericons/icon38.png' } :
{ '19': 'img/browsericons/icon19-off.png', '38': 'img/browsericons/icon38-off.png' };
chrome.browserAction.setIcon({ tabId: tabId, path: iconPaths }, onIconReady);
}; };
/******************************************************************************/ /******************************************************************************/

View File

@ -46,7 +46,7 @@ vAPI.firefox = true;
// TODO: read these data from somewhere... // TODO: read these data from somewhere...
vAPI.app = { vAPI.app = {
name: 'µBlock', name: 'µBlock',
version: '0.8.2.0' version: '0.8.2.3'
}; };
/******************************************************************************/ /******************************************************************************/
@ -580,9 +580,12 @@ vAPI.tabs.injectScript = function(tabId, details, callback) {
/******************************************************************************/ /******************************************************************************/
vAPI.tabIcons = { /*tabId: {badge: 0, img: dict}*/ }; vAPI.tabIcons = { /*tabId: {badge: 0, img: ''}*/ };
vAPI.setIcon = function(tabId, img, badge) { vAPI.setIcon = function(tabId, iconStatus, badge) {
var curWin = badge === undefined ? img : Services.wm.getMostRecentWindow('navigator:browser'); // If badge is undefined, then setIcon was called from the TabSelect event
var curWin = badge === undefined
? iconStatus
: Services.wm.getMostRecentWindow('navigator:browser');
var curTabId = vAPI.tabs.getTabId(curWin.gBrowser.selectedTab); var curTabId = vAPI.tabs.getTabId(curWin.gBrowser.selectedTab);
// from 'TabSelect' event // from 'TabSelect' event
@ -591,8 +594,8 @@ vAPI.setIcon = function(tabId, img, badge) {
} }
else if (badge !== undefined) { else if (badge !== undefined) {
vAPI.tabIcons[tabId] = { vAPI.tabIcons[tabId] = {
badge: badge === '>1K' ? '1k+' : badge, badge: badge,
img: img && img[19] && img[19].replace(/19(-off)?\.png$/, '16$1.svg') img: iconStatus === 'on'
}; };
} }
@ -611,11 +614,9 @@ vAPI.setIcon = function(tabId, img, badge) {
}*/ }*/
var icon = vAPI.tabIcons[tabId]; var icon = vAPI.tabIcons[tabId];
button.setAttribute('badge', icon && icon.badge || ''); button.setAttribute('badge', icon && icon.badge || '');
button.image = vAPI.getURL( iconStatus = !button.image || !icon || !icon.img ? '-off' : '';
button.image && icon && icon.img || 'img/browsericons/icon16-off.svg' button.image = vAPI.getURL('img/browsericons/icon16' + iconStatus + '.svg');
);
}; };
/******************************************************************************/ /******************************************************************************/
@ -717,11 +718,6 @@ vAPI.toolbarButton.register = function(doc) {
if (!this.styleURI) { if (!this.styleURI) {
this.styleURI = 'data:text/css,' + encodeURIComponent([ this.styleURI = 'data:text/css,' + encodeURIComponent([
'#' + this.widgetId + ' {',
'list-style-image: url(',
vAPI.getURL('img/browsericons/icon16-off.svg'),
');',
'}',
'#' + this.widgetId + '[badge]:not([badge=""])::after {', '#' + this.widgetId + '[badge]:not([badge=""])::after {',
'position: absolute;', 'position: absolute;',
'margin-left: -16px;', 'margin-left: -16px;',
@ -898,25 +894,20 @@ var httpObserver = {
parentFrameId: null parentFrameId: null
}, },
observe: function(httpChannel, topic) { observe: function(httpChannel, topic) {
// if this check is performed, it doesn't need to be QueryInterfaced? // No need for QueryInterface if this check is performed?
if (!(httpChannel instanceof Ci.nsIHttpChannel)) { if (!(httpChannel instanceof Ci.nsIHttpChannel)) {
return; return;
} }
var URI = httpChannel.URI, tabId, result; var URI = httpChannel.URI, tabId, result;
// the first distinct character if (topic === 'http-on-modify-request') {
topic = topic.charAt(8);
// http-on-modify-request
if (topic === 'm') {
// var onHeadersReceived = vAPI.net.onHeadersReceived; // var onHeadersReceived = vAPI.net.onHeadersReceived;
return; return;
} }
// http-on-examine-request if (topic === 'http-on-examine-request') {
if (topic === 'e') {
try { try {
tabId = httpChannel.getProperty('tabId'); tabId = httpChannel.getProperty('tabId');
} catch (ex) { } catch (ex) {
@ -927,10 +918,10 @@ var httpObserver = {
return; return;
} }
var CSPHeader = 'Content-Security-Policy'; topic = 'Content-Security-Policy';
try { try {
result = httpChannel.getResponseHeader(CSPHeader); result = httpChannel.getResponseHeader(topic);
} catch (ex) { } catch (ex) {
result = null; result = null;
} }
@ -939,13 +930,13 @@ var httpObserver = {
url: URI.spec, url: URI.spec,
tabId: tabId, tabId: tabId,
parentFrameId: -1, parentFrameId: -1,
responseHeaders: result ? [{name: CSPHeader, value: result}] : [] responseHeaders: result ? [{name: topic, value: result}] : []
}); });
if (result) { if (result) {
httpChannel.setResponseHeader( httpChannel.setResponseHeader(
CSPHeader, topic,
result.responseHeaders[0].value, result.responseHeaders.pop().value,
true true
); );
} }

View File

@ -428,7 +428,7 @@ safari.application.addEventListener('popover', function(e) {
/******************************************************************************/ /******************************************************************************/
vAPI.tabIcons = { /*tabId: {badge: 0, img: dict}*/ }; vAPI.tabIcons = { /*tabId: {badge: 0, img: dict}*/ };
vAPI.setIcon = function(tabId, img, badge) { vAPI.setIcon = function(tabId, iconStatus, badge) {
var curTabId = vAPI.tabs.getTabId( var curTabId = vAPI.tabs.getTabId(
safari.application.activeBrowserWindow.activeTab safari.application.activeBrowserWindow.activeTab
); );
@ -438,9 +438,13 @@ vAPI.setIcon = function(tabId, img, badge) {
tabId = curTabId; tabId = curTabId;
} }
else { else {
if (badge && typeof badge !== 'number') {
badge = 999;
}
vAPI.tabIcons[tabId] = { vAPI.tabIcons[tabId] = {
badge: badge || 0/*, badge: badge || 0,
img: img*/ img: iconStatus === 'on' ? '' : '-off'
}; };
} }
@ -450,18 +454,15 @@ vAPI.setIcon = function(tabId, img, badge) {
// if the selected tab has the same ID, then update the badge too, // if the selected tab has the same ID, then update the badge too,
// or always update it when changing tabs ('activate' event) // or always update it when changing tabs ('activate' event)
var items = safari.extension.toolbarItems, i = items.length; var items = safari.extension.toolbarItems
var i = items.length;
while (i--) { while (i--) {
if (items[i].browserWindow === safari.application.activeBrowserWindow) { if (items[i].browserWindow === safari.application.activeBrowserWindow) {
if (vAPI.tabIcons[tabId]) { var icon = vAPI.tabIcons[tabId];
items[i].badge = vAPI.tabIcons[tabId].badge; items[i].badge = icon && icon.badge || 0;
// items[i].img = vAPI.tabIcons[tabId].img; // TODO: a disabled icon for Safari
} // items[i].img = vAPI.getURL(icon.img);
else {
items[i].badge = 0;
}
return; return;
} }
} }

View File

@ -183,11 +183,7 @@ return asyncJobManager;
pageStore.updateBadge(); pageStore.updateBadge();
return; return;
} }
vAPI.setIcon( vAPI.setIcon(tabId, 'off', '');
tabId,
{ '19': 'img/browsericons/icon19-off.png', '38': 'img/browsericons/icon38-off.png' },
''
);
}; };
var updateBadgeAsync = function(tabId) { var updateBadgeAsync = function(tabId) {

View File

@ -686,21 +686,11 @@ PageStore.prototype.boolFromResult = function(result) {
PageStore.prototype.updateBadge = function() { PageStore.prototype.updateBadge = function() {
var netFiltering = this.getNetFilteringSwitch(); var netFiltering = this.getNetFilteringSwitch();
var iconPaths = netFiltering ? var badge = '';
{ '19': 'img/browsericons/icon19.png', '38': 'img/browsericons/icon38.png' } :
{ '19': 'img/browsericons/icon19-off.png', '38': 'img/browsericons/icon38-off.png' };
var iconStr = '';
if ( µb.userSettings.showIconBadge && netFiltering && this.perLoadBlockedRequestCount ) { if ( µb.userSettings.showIconBadge && netFiltering && this.perLoadBlockedRequestCount ) {
// Safari can't show formatted strings, only integers. badge = µb.utils.formatCount(this.perLoadBlockedRequestCount);
if ( vAPI.safari ) {
iconStr = this.perLoadBlockedRequestCount;
}
else {
iconStr = µb.utils.formatCount(this.perLoadBlockedRequestCount);
}
} }
vAPI.setIcon(this.tabId, iconPaths, iconStr); vAPI.setIcon(this.tabId, netFiltering ? 'on' : 'off', badge);
}; };
// https://www.youtube.com/watch?v=drW8p_dTLD4 // https://www.youtube.com/watch?v=drW8p_dTLD4