add proper handling of potentially unsupported webext APIs

This commit is contained in:
gorhill 2017-07-24 11:35:22 -04:00
parent 3de48a694d
commit d866e4d472
No known key found for this signature in database
GPG key ID: 25E1490B761470C2
2 changed files with 71 additions and 33 deletions

View file

@ -608,32 +608,59 @@ 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, iconStatus, badge) { // https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/browserAction#Browser_compatibility
tabId = toChromiumTabId(tabId); // Firefox for Android does no support browser.browserAction.setIcon().
if ( tabId === 0 ) {
return;
}
var onIconReady = function() { vAPI.setIcon = (function() {
if ( vAPI.lastError() ) { var titleTemplate = chrome.runtime.getManifest().name + ' ({badge})';
return; var iconPaths = [
{
'19': 'img/browsericons/icon19-off.png',
'38': 'img/browsericons/icon38-off.png'
},
{
'19': 'img/browsericons/icon19.png',
'38': 'img/browsericons/icon38.png'
} }
chrome.browserAction.setBadgeText({ tabId: tabId, text: badge }); ];
if ( badge !== '' ) {
chrome.browserAction.setBadgeBackgroundColor({ return function(tabId, iconStatus, badge) {
tabId = toChromiumTabId(tabId);
if ( tabId === 0 ) { return; }
if ( chrome.browserAction.setIcon instanceof Object ) {
chrome.browserAction.setIcon(
{
tabId: tabId,
path: iconPaths[iconStatus === 'on' ? 1 : 0]
},
function onIconReady() {
if ( vAPI.lastError() ) { return; }
chrome.browserAction.setBadgeText({
tabId: tabId,
text: badge
});
if ( badge !== '' ) {
chrome.browserAction.setBadgeBackgroundColor({
tabId: tabId,
color: '#666'
});
}
}
);
} else if ( chrome.browserAction.setTitle instanceof Object ) {
chrome.browserAction.setTitle({
tabId: tabId, tabId: tabId,
color: '#666' title: titleTemplate.replace(
'{badge}',
iconStatus === 'on' ? (badge !== '' ? badge : '0') : 'off'
)
}); });
} }
vAPI.contextMenu.onMustUpdate(tabId);
}; };
})();
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);
vAPI.contextMenu.onMustUpdate(tabId);
};
/******************************************************************************/ /******************************************************************************/
/******************************************************************************/ /******************************************************************************/
@ -1178,7 +1205,10 @@ vAPI.net.registerListeners = function() {
/******************************************************************************/ /******************************************************************************/
/******************************************************************************/ /******************************************************************************/
vAPI.contextMenu = { // https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/contextMenus#Browser_compatibility
// Firefox for Android does no support browser.contextMenus.
vAPI.contextMenu = chrome.contextMenus && {
_callback: null, _callback: null,
_entries: [], _entries: [],
_createEntry: function(entry) { _createEntry: function(entry) {
@ -1304,7 +1334,7 @@ vAPI.punycodeURL = function(url) {
// https://github.com/gorhill/uBlock/issues/900 // https://github.com/gorhill/uBlock/issues/900
// Also, UC Browser: http://www.upsieutoc.com/image/WXuH // Also, UC Browser: http://www.upsieutoc.com/image/WXuH
vAPI.adminStorage = { vAPI.adminStorage = chrome.storage.managed && {
getItem: function(key, callback) { getItem: function(key, callback) {
var onRead = function(store) { var onRead = function(store) {
var data; var data;

View file

@ -27,6 +27,14 @@
/******************************************************************************/ /******************************************************************************/
var api = {
update: function() {}
};
if ( vAPI.contextMenu === undefined ) {
return api;
}
var µb = µBlock; var µb = µBlock;
/******************************************************************************/ /******************************************************************************/
@ -134,20 +142,20 @@ vAPI.contextMenu.onMustUpdate = update;
/******************************************************************************/ /******************************************************************************/
return { api.update = function(tabId) {
update: function(tabId) { if ( µb.userSettings.contextMenuEnabled && tabId === undefined ) {
if ( µb.userSettings.contextMenuEnabled && tabId === undefined ) { vAPI.tabs.get(null, function(tab) {
vAPI.tabs.get(null, function(tab) { if ( tab ) {
if ( tab ) { update(tab.id);
update(tab.id); }
} });
}); return;
return;
}
update(tabId);
} }
update(tabId);
}; };
return api;
/******************************************************************************/ /******************************************************************************/
})(); })();