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
// anymore, so this ensures it does still exist.
vAPI.setIcon = function(tabId, img, badge) {
vAPI.setIcon = function(tabId, iconStatus, badge) {
tabId = parseInt(tabId, 10);
var onIconReady = function() {
if ( vAPI.lastError() ) {
@ -253,10 +253,18 @@ vAPI.setIcon = function(tabId, img, badge) {
}
chrome.browserAction.setBadgeText({ tabId: tabId, text: 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...
vAPI.app = {
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.setIcon = function(tabId, img, badge) {
var curWin = badge === undefined ? img : Services.wm.getMostRecentWindow('navigator:browser');
vAPI.tabIcons = { /*tabId: {badge: 0, img: ''}*/ };
vAPI.setIcon = function(tabId, iconStatus, badge) {
// 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);
// from 'TabSelect' event
@ -591,8 +594,8 @@ vAPI.setIcon = function(tabId, img, badge) {
}
else if (badge !== undefined) {
vAPI.tabIcons[tabId] = {
badge: badge === '>1K' ? '1k+' : badge,
img: img && img[19] && img[19].replace(/19(-off)?\.png$/, '16$1.svg')
badge: badge,
img: iconStatus === 'on'
};
}
@ -611,11 +614,9 @@ vAPI.setIcon = function(tabId, img, badge) {
}*/
var icon = vAPI.tabIcons[tabId];
button.setAttribute('badge', icon && icon.badge || '');
button.image = vAPI.getURL(
button.image && icon && icon.img || 'img/browsericons/icon16-off.svg'
);
iconStatus = !button.image || !icon || !icon.img ? '-off' : '';
button.image = vAPI.getURL('img/browsericons/icon16' + iconStatus + '.svg');
};
/******************************************************************************/
@ -717,11 +718,6 @@ vAPI.toolbarButton.register = function(doc) {
if (!this.styleURI) {
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 {',
'position: absolute;',
'margin-left: -16px;',
@ -898,25 +894,20 @@ var httpObserver = {
parentFrameId: null
},
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)) {
return;
}
var URI = httpChannel.URI, tabId, result;
// the first distinct character
topic = topic.charAt(8);
// http-on-modify-request
if (topic === 'm') {
if (topic === 'http-on-modify-request') {
// var onHeadersReceived = vAPI.net.onHeadersReceived;
return;
}
// http-on-examine-request
if (topic === 'e') {
if (topic === 'http-on-examine-request') {
try {
tabId = httpChannel.getProperty('tabId');
} catch (ex) {
@ -927,10 +918,10 @@ var httpObserver = {
return;
}
var CSPHeader = 'Content-Security-Policy';
topic = 'Content-Security-Policy';
try {
result = httpChannel.getResponseHeader(CSPHeader);
result = httpChannel.getResponseHeader(topic);
} catch (ex) {
result = null;
}
@ -939,13 +930,13 @@ var httpObserver = {
url: URI.spec,
tabId: tabId,
parentFrameId: -1,
responseHeaders: result ? [{name: CSPHeader, value: result}] : []
responseHeaders: result ? [{name: topic, value: result}] : []
});
if (result) {
httpChannel.setResponseHeader(
CSPHeader,
result.responseHeaders[0].value,
topic,
result.responseHeaders.pop().value,
true
);
}

View File

@ -428,7 +428,7 @@ safari.application.addEventListener('popover', function(e) {
/******************************************************************************/
vAPI.tabIcons = { /*tabId: {badge: 0, img: dict}*/ };
vAPI.setIcon = function(tabId, img, badge) {
vAPI.setIcon = function(tabId, iconStatus, badge) {
var curTabId = vAPI.tabs.getTabId(
safari.application.activeBrowserWindow.activeTab
);
@ -438,9 +438,13 @@ vAPI.setIcon = function(tabId, img, badge) {
tabId = curTabId;
}
else {
if (badge && typeof badge !== 'number') {
badge = 999;
}
vAPI.tabIcons[tabId] = {
badge: badge || 0/*,
img: img*/
badge: badge || 0,
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,
// 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--) {
if (items[i].browserWindow === safari.application.activeBrowserWindow) {
if (vAPI.tabIcons[tabId]) {
items[i].badge = vAPI.tabIcons[tabId].badge;
// items[i].img = vAPI.tabIcons[tabId].img;
}
else {
items[i].badge = 0;
}
var icon = vAPI.tabIcons[tabId];
items[i].badge = icon && icon.badge || 0;
// TODO: a disabled icon for Safari
// items[i].img = vAPI.getURL(icon.img);
return;
}
}

View File

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

View File

@ -686,21 +686,11 @@ PageStore.prototype.boolFromResult = function(result) {
PageStore.prototype.updateBadge = function() {
var netFiltering = this.getNetFilteringSwitch();
var iconPaths = netFiltering ?
{ '19': 'img/browsericons/icon19.png', '38': 'img/browsericons/icon38.png' } :
{ '19': 'img/browsericons/icon19-off.png', '38': 'img/browsericons/icon38-off.png' };
var iconStr = '';
var badge = '';
if ( µb.userSettings.showIconBadge && netFiltering && this.perLoadBlockedRequestCount ) {
// Safari can't show formatted strings, only integers.
if ( vAPI.safari ) {
iconStr = this.perLoadBlockedRequestCount;
badge = µb.utils.formatCount(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