From 769dc205fb56fa8f9442f68907eb9598caea52bd Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Tue, 19 Feb 2019 22:49:25 -0500 Subject: Make extension badge and onCommand optional --- ext/bg/js/backend.js | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'ext') diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 01340419..c191a150 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -28,7 +28,9 @@ class Backend { await this.translator.prepare(); await apiOptionsSet(await optionsLoad()); - chrome.commands.onCommand.addListener(this.onCommand.bind(this)); + if (chrome.commands !== null && typeof chrome.commands === 'object') { + chrome.commands.onCommand.addListener(this.onCommand.bind(this)); + } chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); if (this.options.general.showGuide) { @@ -40,13 +42,13 @@ class Backend { this.options = utilIsolate(options); if (!options.general.enable) { - chrome.browserAction.setBadgeBackgroundColor({color: '#555555'}); - chrome.browserAction.setBadgeText({text: 'off'}); + this.setExtensionBadgeBackgroundColor('#555555'); + this.setExtensionBadgeText('off'); } else if (!dictConfigured(options)) { - chrome.browserAction.setBadgeBackgroundColor({color: '#f0ad4e'}); - chrome.browserAction.setBadgeText({text: '!'}); + this.setExtensionBadgeBackgroundColor('#f0ad4e'); + this.setExtensionBadgeText('!'); } else { - chrome.browserAction.setBadgeText({text: ''}); + this.setExtensionBadgeText(''); } if (options.anki.enable) { @@ -125,6 +127,18 @@ class Backend { return true; } + + setExtensionBadgeBackgroundColor(color) { + if (typeof chrome.browserAction.setBadgeBackgroundColor === 'function') { + chrome.browserAction.setBadgeBackgroundColor({color}); + } + } + + setExtensionBadgeText(text) { + if (typeof chrome.browserAction.setBadgeText === 'function') { + chrome.browserAction.setBadgeText({text}); + } + } } window.yomichan_backend = new Backend(); -- cgit v1.2.3