Skip to content

Commit

Permalink
Merge pull request #315 from ipfs/feat/brave
Browse files Browse the repository at this point in the history
Fallback to opening options page manually in brave
  • Loading branch information
lidel committed Nov 27, 2017
2 parents f148251 + 84ed233 commit 7814ab1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 31 deletions.
68 changes: 38 additions & 30 deletions add-on/src/lib/ipfs-companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,28 +182,32 @@ const contextMenuUploadToIpfs = 'contextMenu_UploadToIpfs'
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.log('[ipfs-companion] Error creating contextMenus', err)
}

function inFirefox () {
return !!navigator.userAgent.match('Firefox')
Expand Down Expand Up @@ -362,15 +366,19 @@ async function copyTextToClipboard (copyText) {
}

async function updateContextMenus (changedTabId) {
await browser.contextMenus.update(contextMenuUploadToIpfs, {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 && 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})
if (changedTabId) {
// recalculate tab-dependant menu items
const currentTab = await browser.tabs.query({active: true, currentWindow: true}).then(tabs => tabs[0])
if (currentTab && currentTab.id === changedTabId) {
const ipfsContext = isIpfsPageActionsContext(currentTab.url)
browser.contextMenus.update(contextMenuCopyIpfsAddress, {enabled: ipfsContext})
browser.contextMenus.update(contextMenuCopyPublicGwUrl, {enabled: ipfsContext})
}
}
} catch (err) {
console.log('[ipfs-companion] Error updating context menus', err)
}
}

Expand Down
8 changes: 7 additions & 1 deletion add-on/src/popup/browser-action/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,13 @@ module.exports = (state, emitter) => {
})

emitter.on('openPrefs', () => {
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('dist/options/options.html') })
})
})

emitter.on('toggleRedirect', async () => {
Expand Down

0 comments on commit 7814ab1

Please sign in to comment.