Skip to content

Commit

Permalink
Fallback to opening options page manually in brave
Browse files Browse the repository at this point in the history
  • Loading branch information
olizilla committed Nov 17, 2017
1 parent 64a5e2b commit 6f0525a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 32 deletions.
70 changes: 39 additions & 31 deletions add-on/src/lib/ipfs-companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: ['<all_urls>'],
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: ['<all_urls>'],
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')
Expand Down Expand Up @@ -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)
}
}

Expand Down
8 changes: 7 additions & 1 deletion add-on/src/popup/browser-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 6f0525a

Please sign in to comment.