From 185916c8a32fcf6719bc7e6c252d73bfda35f88c Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 25 Sep 2017 20:35:54 -0400 Subject: [PATCH] api compat checks --- src/background.js | 130 ++++++++++++++++++++++++++-------------------- 1 file changed, 73 insertions(+), 57 deletions(-) diff --git a/src/background.js b/src/background.js index 8c83692730..34ed6b0cc3 100644 --- a/src/background.js +++ b/src/background.js @@ -171,64 +171,68 @@ var bg_isBackground = true, refreshBadgeAndMenu(); }); - chrome.windows.onFocusChanged.addListener(function (windowId) { - if (windowId === null || windowId < 0) { - return; - } - - refreshBadgeAndMenu(); - }); - - chrome.contextMenus.onClicked.addListener(function (info, tab) { - if (info.menuItemId === 'generate-password') { - ga('send', { - hitType: 'event', - eventAction: 'Generated Password From Context Menu' - }); - bg_passwordGenerationService.getOptions().then(function (options) { - var password = bg_passwordGenerationService.generatePassword(options); - bg_utilsService.copyToClipboard(password); - }); - } - else if (info.parentMenuItemId === 'autofill' || info.parentMenuItemId === 'copy-username' || - info.parentMenuItemId === 'copy-password') { - var id = info.menuItemId.split('_')[1]; - if (id === 'noop') { + if (chrome.windows) { + chrome.windows.onFocusChanged.addListener(function (windowId) { + if (windowId === null || windowId < 0) { return; } - bg_loginService.getAllDecrypted().then(function (logins) { - for (var i = 0; i < logins.length; i++) { - if (logins[i].id === id) { - if (info.parentMenuItemId === 'autofill') { - ga('send', { - hitType: 'event', - eventAction: 'Autofilled From Context Menu' - }); - startAutofillPage(logins[i]); - } - else if (info.parentMenuItemId === 'copy-username') { - ga('send', { - hitType: 'event', - eventAction: 'Copied Username From Context Menu' - }); - bg_utilsService.copyToClipboard(logins[i].username); - } - else if (info.parentMenuItemId === 'copy-password') { - ga('send', { - hitType: 'event', - eventAction: 'Copied Password From Context Menu' - }); - bg_utilsService.copyToClipboard(logins[i].password); - } - return; - } - } - }, function () { + refreshBadgeAndMenu(); + }); + } - }); - } - }); + if (chrome.contextMenus) { + chrome.contextMenus.onClicked.addListener(function (info, tab) { + if (info.menuItemId === 'generate-password') { + ga('send', { + hitType: 'event', + eventAction: 'Generated Password From Context Menu' + }); + bg_passwordGenerationService.getOptions().then(function (options) { + var password = bg_passwordGenerationService.generatePassword(options); + bg_utilsService.copyToClipboard(password); + }); + } + else if (info.parentMenuItemId === 'autofill' || info.parentMenuItemId === 'copy-username' || + info.parentMenuItemId === 'copy-password') { + var id = info.menuItemId.split('_')[1]; + if (id === 'noop') { + return; + } + + bg_loginService.getAllDecrypted().then(function (logins) { + for (var i = 0; i < logins.length; i++) { + if (logins[i].id === id) { + if (info.parentMenuItemId === 'autofill') { + ga('send', { + hitType: 'event', + eventAction: 'Autofilled From Context Menu' + }); + startAutofillPage(logins[i]); + } + else if (info.parentMenuItemId === 'copy-username') { + ga('send', { + hitType: 'event', + eventAction: 'Copied Username From Context Menu' + }); + bg_utilsService.copyToClipboard(logins[i].username); + } + else if (info.parentMenuItemId === 'copy-password') { + ga('send', { + hitType: 'event', + eventAction: 'Copied Password From Context Menu' + }); + bg_utilsService.copyToClipboard(logins[i].password); + } + return; + } + } + }, function () { + + }); + } + }); + } if (chrome.webRequest && chrome.webRequest.onAuthRequired) { chrome.webRequest.onAuthRequired.addListener(function (details, callback) { @@ -300,7 +304,7 @@ var bg_isBackground = true, var buildingContextMenu = false; function buildContextMenu(callback) { - if (buildingContextMenu) { + if (!chrome.contextMenus || buildingContextMenu) { return; } buildingContextMenu = true; @@ -367,6 +371,10 @@ var bg_isBackground = true, } function setIcon() { + if (!chrome.browserAction.setIcon) { + return; + } + bg_userService.isAuthenticated(function (isAuthenticated) { bg_cryptoService.getKey().then(function (key) { var suffix = ''; @@ -388,6 +396,10 @@ var bg_isBackground = true, } function refreshBadgeAndMenu() { + if (!chrome.windows || !chrome.contextMenus) { + return; + } + chrome.tabs.query({ active: true, windowId: chrome.windows.WINDOW_ID_CURRENT }, function (tabs) { var tab = null; if (tabs.length > 0) { @@ -418,7 +430,7 @@ var bg_isBackground = true, } function loadMenuAndUpdateBadge(url, tabId, contextMenuEnabled) { - if (!url) { + if (!chrome.browserAction.setBadgeBackgroundColor || !url) { return; } @@ -429,6 +441,10 @@ var bg_isBackground = true, chrome.browserAction.setBadgeBackgroundColor({ color: '#294e5f' }); + if (!chrome.browserAction.setBadgeText) { + return; + } + menuOptionsLoaded = []; bg_loginService.getAllDecryptedForDomain(tabDomain).then(function (logins) { logins.sort(bg_loginService.sortLoginsByLastUsedThenName); @@ -704,7 +720,7 @@ var bg_isBackground = true, } function loadContextMenuOptions(title, idSuffix, login) { - if (menuOptionsLoaded.indexOf(idSuffix) > -1) { + if (!chrome.contextMenus || menuOptionsLoaded.indexOf(idSuffix) > -1) { return; } menuOptionsLoaded.push(idSuffix);