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,28 +3,30 @@ localStorage['log'] = ''
localStorage['logLastError'] = ''
window.OmegaContextMenuQuickSwitchHandler = -> null
# We don't need this API. However its presence indicates that Chrome >= 35
# which provides info.checked we need in contextMenu callback.
# https://developer.chrome.com/extensions/contextMenus
if chrome.i18n.getUILanguage?
# We must create the menu item here before others to make it first in menu.
if chrome.contextMenus?
# We don't need this API. However its presence indicates that Chrome >= 35
# which provides info.checked we need in contextMenu callback.
# https://developer.chrome.com/extensions/contextMenus
if chrome.i18n.getUILanguage?
# We must create the menu item here before others to make it first in menu.
chrome.contextMenus.create({
id: 'enableQuickSwitch'
title: chrome.i18n.getMessage('contextMenu_enableQuickSwitch')
type: 'checkbox'
checked: false
contexts: ["browser_action"]
onclick: (info) -> window.OmegaContextMenuQuickSwitchHandler(info)
})
chrome.contextMenus.create({
id: 'enableQuickSwitch'
title: chrome.i18n.getMessage('contextMenu_enableQuickSwitch')
type: 'checkbox'
checked: false
title: chrome.i18n.getMessage('popup_reportIssues')
contexts: ["browser_action"]
onclick: (info) -> window.OmegaContextMenuQuickSwitchHandler(info)
onclick: OmegaDebug.reportIssue
})
chrome.contextMenus.create({
title: chrome.i18n.getMessage('popup_reportIssues')
contexts: ["browser_action"]
onclick: OmegaDebug.reportIssue
})
chrome.contextMenus.create({
title: chrome.i18n.getMessage('popup_errorLog')
contexts: ["browser_action"]
onclick: OmegaDebug.downloadLog
})
chrome.contextMenus.create({
title: chrome.i18n.getMessage('popup_errorLog')
contexts: ["browser_action"]
onclick: OmegaDebug.downloadLog
})

View File

@ -22,7 +22,7 @@ module.exports = class ExternalApi
return unless @disabled
@options.setProxyNotControllable(null)
chrome.browserAction.setPopup({popup: 'popup/index.html'})
chrome.browserAction.setPopup?({popup: 'popup/index.html'})
@options.reloadQuickSwitch()
@disabled = false
@options.clearBadge()
@ -49,7 +49,7 @@ module.exports = class ExternalApi
if @knownExts[port.sender.id] >= 32
reason = 'upgrade'
@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'})
when 'enable'
@reenable()

View File

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

View File

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

View File

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