From 6f0525ac41c3ec4fef0c289aaec10b95368f3cbd Mon Sep 17 00:00:00 2001 From: Oli Evans Date: Fri, 17 Nov 2017 16:02:16 +0000 Subject: [PATCH] Fallback to opening options page manually in brave --- add-on/src/lib/ipfs-companion.js | 70 +++++++++++++++++------------- add-on/src/popup/browser-action.js | 8 +++- 2 files changed, 46 insertions(+), 32 deletions(-) diff --git a/add-on/src/lib/ipfs-companion.js b/add-on/src/lib/ipfs-companion.js index e2e34dff6..d230706dc 100644 --- a/add-on/src/lib/ipfs-companion.js +++ b/add-on/src/lib/ipfs-companion.js @@ -400,28 +400,32 @@ const contextMenuCreateShortUrl = 'contextMenu_createShortUrl' const contextMenuCopyIpfsAddress = 'panelCopy_currentIpfsAddress' const contextMenuCopyPublicGwUrl = 'panel_copyCurrentPublicGwUrl' -browser.contextMenus.create({ - id: contextMenuUploadToIpfs, - title: browser.i18n.getMessage(contextMenuUploadToIpfs), - contexts: ['image', 'video', 'audio'], - documentUrlPatterns: [''], - enabled: false, - onclick: addFromURL -}) -browser.contextMenus.create({ - id: contextMenuCopyIpfsAddress, - title: browser.i18n.getMessage(contextMenuCopyIpfsAddress), - contexts: ['page', 'image', 'video', 'audio', 'link'], - documentUrlPatterns: ['*://*/ipfs/*', '*://*/ipns/*'], - onclick: copyCanonicalAddress -}) -browser.contextMenus.create({ - id: contextMenuCopyPublicGwUrl, - title: browser.i18n.getMessage(contextMenuCopyPublicGwUrl), - contexts: ['page', 'image', 'video', 'audio', 'link'], - documentUrlPatterns: ['*://*/ipfs/*', '*://*/ipns/*'], - onclick: copyAddressAtPublicGw -}) +try { + browser.contextMenus.create({ + id: contextMenuUploadToIpfs, + title: browser.i18n.getMessage(contextMenuUploadToIpfs), + contexts: ['image', 'video', 'audio'], + documentUrlPatterns: [''], + enabled: false, + onclick: addFromURL + }) + browser.contextMenus.create({ + id: contextMenuCopyIpfsAddress, + title: browser.i18n.getMessage(contextMenuCopyIpfsAddress), + contexts: ['page', 'image', 'video', 'audio', 'link'], + documentUrlPatterns: ['*://*/ipfs/*', '*://*/ipns/*'], + onclick: copyCanonicalAddress + }) + browser.contextMenus.create({ + id: contextMenuCopyPublicGwUrl, + title: browser.i18n.getMessage(contextMenuCopyPublicGwUrl), + contexts: ['page', 'image', 'video', 'audio', 'link'], + documentUrlPatterns: ['*://*/ipfs/*', '*://*/ipns/*'], + onclick: copyAddressAtPublicGw + }) +} catch (err) { + console.error('Error creating contextMenus', err) +} function inFirefox () { return !!navigator.userAgent.match('Firefox') @@ -577,16 +581,20 @@ async function copyTextToClipboard (copyText) { } async function updateContextMenus (changedTabId) { - await browser.contextMenus.update(contextMenuUploadToIpfs, {enabled: state.peerCount > 0}) - await browser.contextMenus.update(contextMenuCreateShortUrl, {enabled: state.peerCount > 0}) - if (changedTabId) { - // recalculate tab-dependant menu items - const currentTab = await browser.tabs.query({active: true, currentWindow: true}).then(tabs => tabs[0]) - if (currentTab.id === changedTabId) { - const ipfsContext = isIpfsPageActionsContext(currentTab.url) - browser.contextMenus.update(contextMenuCopyIpfsAddress, {enabled: ipfsContext}) - browser.contextMenus.update(contextMenuCopyPublicGwUrl, {enabled: ipfsContext}) + try { + await browser.contextMenus.update(contextMenuUploadToIpfs, {enabled: state.peerCount > 0}) + await browser.contextMenus.update(contextMenuCreateShortUrl, {enabled: state.peerCount > 0}) + if (changedTabId) { + // recalculate tab-dependant menu items + const currentTab = await browser.tabs.query({active: true, currentWindow: true}).then(tabs => tabs[0]) + if (currentTab.id === changedTabId) { + const ipfsContext = isIpfsPageActionsContext(currentTab.url) + browser.contextMenus.update(contextMenuCopyIpfsAddress, {enabled: ipfsContext}) + browser.contextMenus.update(contextMenuCopyPublicGwUrl, {enabled: ipfsContext}) + } } + } catch (err) { + console.error('Error updating context menus', err) } } diff --git a/add-on/src/popup/browser-action.js b/add-on/src/popup/browser-action.js index 69041aaba..7efe95786 100644 --- a/add-on/src/popup/browser-action.js +++ b/add-on/src/popup/browser-action.js @@ -208,7 +208,13 @@ openWebUI.onclick = async () => { } openPreferences.onclick = () => { - browser.runtime.openOptionsPage().then(() => window.close()) + browser.runtime.openOptionsPage() + .then(() => window.close()) + .catch((err) => { + console.error('runtime.openOptionsPage() failed, opening options page in tab instead.', err) + // brave: fallback to opening options page as a tab. + browser.tabs.create({ url: browser.extension.getURL('src/options/options.html') }) + }) } async function updateBrowserActionPopup () {