Make chrome.contextMenus and some browserAction APIs optional.

This commit is contained in:
FelisCatus 2017-08-03 21:45:50 -04:00
parent 2442d1af5f
commit 227e189a73
5 changed files with 35 additions and 32 deletions

View File

@ -3,6 +3,8 @@ localStorage['log'] = ''
localStorage['logLastError'] = '' localStorage['logLastError'] = ''
window.OmegaContextMenuQuickSwitchHandler = -> null window.OmegaContextMenuQuickSwitchHandler = -> null
if chrome.contextMenus?
# We don't need this API. However its presence indicates that Chrome >= 35 # We don't need this API. However its presence indicates that Chrome >= 35
# which provides info.checked we need in contextMenu callback. # which provides info.checked we need in contextMenu callback.
# https://developer.chrome.com/extensions/contextMenus # https://developer.chrome.com/extensions/contextMenus

View File

@ -22,7 +22,7 @@ module.exports = class ExternalApi
return unless @disabled return unless @disabled
@options.setProxyNotControllable(null) @options.setProxyNotControllable(null)
chrome.browserAction.setPopup({popup: 'popup/index.html'}) chrome.browserAction.setPopup?({popup: 'popup/index.html'})
@options.reloadQuickSwitch() @options.reloadQuickSwitch()
@disabled = false @disabled = false
@options.clearBadge() @options.clearBadge()
@ -49,7 +49,7 @@ module.exports = class ExternalApi
if @knownExts[port.sender.id] >= 32 if @knownExts[port.sender.id] >= 32
reason = 'upgrade' reason = 'upgrade'
@options.setProxyNotControllable reason, {text: 'X', color: '#5ab432'} @options.setProxyNotControllable reason, {text: 'X', color: '#5ab432'}
chrome.browserAction.setPopup({popup: 'popup/index.html'}) chrome.browserAction.setPopup?({popup: 'popup/index.html'})
port.postMessage({action: 'state', state: 'disabled'}) port.postMessage({action: 'state', state: 'disabled'})
when 'enable' when 'enable'
@reenable() @reenable()

View File

@ -8,6 +8,7 @@ module.exports = class Inspect
@onInspect = onInspect @onInspect = onInspect
enable: -> enable: ->
return unless chrome.contextMenus?
# We don't need this API. However its presence indicates that Chrome >= 35, # We don't need this API. However its presence indicates that Chrome >= 35,
# which provides the menuItemId we need in contextMenu callback. # which provides the menuItemId we need in contextMenu callback.
# https://developer.chrome.com/extensions/contextMenus # https://developer.chrome.com/extensions/contextMenus

View File

@ -78,7 +78,7 @@ class ChromeOptions extends OmegaTarget.Options
if @_proxyNotControllable if @_proxyNotControllable
@setBadge() @setBadge()
else else
chrome.browserAction.setBadgeText(text: '') chrome.browserAction.setBadgeText?(text: '')
return return
_formatBypassItem: (condition) -> _formatBypassItem: (condition) ->
@ -261,7 +261,7 @@ class ChromeOptions extends OmegaTarget.Options
if not @_quickSwitchContextMenuCreated if not @_quickSwitchContextMenuCreated
@_quickSwitchContextMenuCreated = true @_quickSwitchContextMenuCreated = true
if quickSwitch if quickSwitch
chrome.contextMenus.update('enableQuickSwitch', {checked: true}) chrome.contextMenus?.update('enableQuickSwitch', {checked: true})
window.OmegaContextMenuQuickSwitchHandler = (info) => window.OmegaContextMenuQuickSwitchHandler = (info) =>
changes = {} changes = {}
changes['-enableQuickSwitch'] = info.checked changes['-enableQuickSwitch'] = info.checked
@ -273,7 +273,7 @@ class ChromeOptions extends OmegaTarget.Options
) )
if quickSwitch if quickSwitch
chrome.browserAction.setPopup({popup: ''}) chrome.browserAction.setPopup?({popup: ''})
if not @_quickSwitchInit if not @_quickSwitchInit
@_quickSwitchInit = true @_quickSwitchInit = true
chrome.browserAction.onClicked.addListener (tab) => chrome.browserAction.onClicked.addListener (tab) =>
@ -290,7 +290,7 @@ class ChromeOptions extends OmegaTarget.Options
return if url.substr(0, 4) == 'moz-' return if url.substr(0, 4) == 'moz-'
chrome.tabs.reload(tab.id) chrome.tabs.reload(tab.id)
else else
chrome.browserAction.setPopup({popup: 'popup/index.html'}) chrome.browserAction.setPopup?({popup: 'popup/index.html'})
Promise.resolve() Promise.resolve()
setInspect: (settings) -> setInspect: (settings) ->

View File

@ -39,7 +39,7 @@ class ChromeTabs
processTab: (tab, changeInfo) -> processTab: (tab, changeInfo) ->
if @_badgeTab if @_badgeTab
for own id of @_badgeTab for own id of @_badgeTab
try chrome.browserAction.setBadgeText(text: '', tabId: id) try chrome.browserAction.setBadgeText?(text: '', tabId: id)
@_badgeTab = null @_badgeTab = null
if not tab.url? or tab.url.indexOf("chrome") == 0 if not tab.url? or tab.url.indexOf("chrome") == 0
if @_defaultAction if @_defaultAction
@ -56,8 +56,8 @@ class ChromeTabs
setTabBadge: (tab, badge) -> setTabBadge: (tab, badge) ->
@_badgeTab ?= {} @_badgeTab ?= {}
@_badgeTab[tab.id] = true @_badgeTab[tab.id] = true
chrome.browserAction.setBadgeText(text: badge.text, tabId: tab.id) chrome.browserAction.setBadgeText?(text: badge.text, tabId: tab.id)
chrome.browserAction.setBadgeBackgroundColor( chrome.browserAction.setBadgeBackgroundColor?(
color: badge.color color: badge.color
tabId: tab.id tabId: tab.id
) )
@ -77,11 +77,11 @@ class ChromeTabs
_chromeSetIcon: (params) -> _chromeSetIcon: (params) ->
try try
chrome.browserAction.setIcon(params, @ignoreError) chrome.browserAction.setIcon?(params, @ignoreError)
catch catch
# Some legacy Chrome versions will panic if there are other icon sizes. # Some legacy Chrome versions will panic if there are other icon sizes.
params.imageData = {19: params.imageData[19], 38: params.imageData[38]} params.imageData = {19: params.imageData[19], 38: params.imageData[38]}
chrome.browserAction.setIcon(params, @ignoreError) chrome.browserAction.setIcon?(params, @ignoreError)
clearIcon: (tabId) -> clearIcon: (tabId) ->
return unless @_defaultAction?.icon? return unless @_defaultAction?.icon?