Skip to content

Commit 963a340

Browse files
committed
feat: View on Gateway context action on IPFS pages
This adds new item to Active Tab section of menu in browser action that enables user to open existing IPFS resource at Public or Custom Gateway (depends on redirect status).
1 parent 24869d5 commit 963a340

File tree

7 files changed

+59
-9
lines changed

7 files changed

+59
-9
lines changed

add-on/_locales/en/messages.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@
9191
"message": "Copy Public Gateway URL",
9292
"description": "A menu item in Browser Action pop-up and right-click context menu (panel_copyCurrentPublicGwUrl)"
9393
},
94+
"panel_contextMenuViewOnGateway": {
95+
"message": "View on Gateway",
96+
"description": "A menu item in Browser Action pop-up and right-click context menu (panel_contextMenuViewOnGateway)"
97+
},
9498
"pageAction_titleIpfsAtPublicGateway": {
9599
"message": "IPFS resource loaded via Public Gateway",
96100
"description": "A tooltip displayed over Page Action in location bar (pageAction_titleIpfsAtPublicGateway)"
@@ -276,7 +280,7 @@
276280
"description": "An option description on the Preferences screen (option_useCustomGateway_description)"
277281
},
278282
"option_dnslinkRedirect_title": {
279-
"message": "Force load from Custom Gateway",
283+
"message": "Force page load from custom gateway",
280284
"description": "An option title on the Preferences screen (option_dnslinkRedirect_title)"
281285
},
282286
"option_dnslinkRedirect_description": {

add-on/src/lib/context-menus.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@ const contextMenuImportToIpfsSelection = 'contextMenu_importToIpfsSelection'
5858
const contextMenuCopyCanonicalAddress = 'panelCopy_currentIpfsAddress'
5959
const contextMenuCopyRawCid = 'panelCopy_copyRawCid'
6060
const contextMenuCopyAddressAtPublicGw = 'panel_copyCurrentPublicGwUrl'
61+
const contextMenuViewOnGateway = 'panel_contextMenuViewOnGateway'
6162
module.exports.contextMenuCopyCanonicalAddress = contextMenuCopyCanonicalAddress
6263
module.exports.contextMenuCopyRawCid = contextMenuCopyRawCid
6364
module.exports.contextMenuCopyAddressAtPublicGw = contextMenuCopyAddressAtPublicGw
65+
module.exports.contextMenuViewOnGateway = contextMenuViewOnGateway
6466

6567
// menu items that are enabled only when API is online
6668
const apiMenuItems = new Set()

add-on/src/lib/inspector.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict'
2+
3+
const browser = require('webextension-polyfill')
4+
const { findValueForContext } = require('./context-menus')
5+
const { pathAtHttpGateway } = require('./ipfs-path')
6+
7+
function createInspector (notify, ipfsPathValidator, getState) {
8+
return {
9+
async viewOnGateway (context, contextType) {
10+
const url = await findValueForContext(context, contextType)
11+
const ipfsPath = ipfsPathValidator.resolveToIpfsPath(url)
12+
const gateway = getState().pubGwURLString
13+
const gatewayUrl = pathAtHttpGateway(ipfsPath, gateway)
14+
await browser.tabs.create({ url: gatewayUrl })
15+
}
16+
// TODO: view in WebUI's Files
17+
// TODO: view in WebUI's IPLD Explorer
18+
}
19+
}
20+
21+
module.exports = createInspector

add-on/src/lib/ipfs-companion.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ const { createIpfsUrlProtocolHandler } = require('./ipfs-protocol')
1818
const createIpfsImportHandler = require('./ipfs-import')
1919
const createNotifier = require('./notifier')
2020
const createCopier = require('./copier')
21+
const createInspector = require('./inspector')
2122
const { createRuntimeChecks } = require('./runtime-checks')
22-
const { createContextMenus, findValueForContext, contextMenuCopyAddressAtPublicGw, contextMenuCopyRawCid, contextMenuCopyCanonicalAddress } = require('./context-menus')
23+
const { createContextMenus, findValueForContext, contextMenuCopyAddressAtPublicGw, contextMenuCopyRawCid, contextMenuCopyCanonicalAddress, contextMenuViewOnGateway } = require('./context-menus')
2324
const createIpfsProxy = require('./ipfs-proxy')
2425
const { showPendingLandingPages } = require('./on-installed')
2526

@@ -34,6 +35,7 @@ module.exports = async function init () {
3435
var modifyRequest
3536
var notify
3637
var copier
38+
var inspector
3739
var runtime
3840
var contextMenus
3941
var apiStatusUpdateInterval
@@ -69,6 +71,7 @@ module.exports = async function init () {
6971
ipfsPathValidator = createIpfsPathValidator(getState, getIpfs, dnslinkResolver)
7072
ipfsImportHandler = createIpfsImportHandler(getState, getIpfs, ipfsPathValidator, runtime)
7173
copier = createCopier(notify, ipfsPathValidator)
74+
inspector = createInspector(notify, ipfsPathValidator, getState)
7275
contextMenus = createContextMenus(getState, runtime, ipfsPathValidator, {
7376
onAddFromContext,
7477
onCopyCanonicalAddress: copier.copyCanonicalAddress,
@@ -212,6 +215,7 @@ module.exports = async function init () {
212215

213216
const BrowserActionMessageHandlers = {
214217
notification: (message) => notify(message.title, message.message),
218+
[contextMenuViewOnGateway]: inspector.viewOnGateway,
215219
[contextMenuCopyCanonicalAddress]: copier.copyCanonicalAddress,
216220
[contextMenuCopyRawCid]: copier.copyRawCid,
217221
[contextMenuCopyAddressAtPublicGw]: copier.copyAddressAtPublicGw

add-on/src/popup/browser-action/context-actions.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,23 @@ const browser = require('webextension-polyfill')
55
const html = require('choo/html')
66
const navItem = require('./nav-item')
77
const navHeader = require('./nav-header')
8-
const { contextMenuCopyAddressAtPublicGw, contextMenuCopyRawCid, contextMenuCopyCanonicalAddress } = require('../../lib/context-menus')
8+
const {
9+
contextMenuViewOnGateway,
10+
contextMenuCopyAddressAtPublicGw,
11+
contextMenuCopyRawCid,
12+
contextMenuCopyCanonicalAddress
13+
} = require('../../lib/context-menus')
914

1015
// Context Actions are displayed in Browser Action and Page Action (FF only)
1116
function contextActions ({
1217
active,
1318
redirect,
1419
isRedirectContext,
20+
pubGwURLString,
21+
gwURLString,
22+
currentTab,
1523
currentFqdn,
24+
currentDnslinkFqdn,
1625
currentTabRedirectOptOut,
1726
ipfsNodeType,
1827
isIpfsContext,
@@ -22,16 +31,21 @@ function contextActions ({
2231
isIpfsOnline,
2332
isApiAvailable,
2433
onToggleSiteRedirect,
34+
onViewOnGateway,
2535
onCopy,
2636
onPin,
2737
onUnPin
2838
}) {
2939
const activeCidResolver = active && isIpfsOnline && isApiAvailable
3040
const activePinControls = active && isIpfsOnline && isApiAvailable
31-
41+
const activeViewOnGateway = currentTab && !(currentTab.url.startsWith(pubGwURLString) || currentTab.url.startsWith(gwURLString))
3242
const renderIpfsContextItems = () => {
3343
if (!isIpfsContext) return
3444
return html`<div>
45+
${activeViewOnGateway ? navItem({
46+
text: browser.i18n.getMessage(contextMenuViewOnGateway),
47+
onClick: () => onViewOnGateway(contextMenuViewOnGateway)
48+
}) : null}
3549
${navItem({
3650
text: browser.i18n.getMessage(contextMenuCopyAddressAtPublicGw),
3751
onClick: () => onCopy(contextMenuCopyAddressAtPublicGw)
@@ -55,7 +69,7 @@ function contextActions ({
5569
</div>
5670
`
5771
}
58-
72+
// TODO: change "redirect on {fqdn}" to "disable on {fqdn}" and disable all integrations
5973
const renderSiteRedirectToggle = () => {
6074
if (!isRedirectContext) return
6175
return html`
@@ -69,11 +83,10 @@ function contextActions ({
6983
})}
7084
`
7185
}
72-
7386
return html`
7487
<div class='fade-in pv1'>
75-
${renderSiteRedirectToggle()}
7688
${renderIpfsContextItems()}
89+
${renderSiteRedirectToggle()}
7790
</div>
7891
`
7992
}

add-on/src/popup/browser-action/page.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const tools = require('./tools')
1111
// Passed current app `state` from the store and `emit`, a function to create
1212
// events, allowing views to signal back to the store that something happened.
1313
module.exports = function browserActionPage (state, emit) {
14+
const onViewOnGateway = () => emit('viewOnGateway')
1415
const onCopy = (copyAction) => emit('copy', copyAction)
1516
const onPin = () => emit('pin')
1617
const onUnPin = () => emit('unPin')
@@ -23,7 +24,7 @@ module.exports = function browserActionPage (state, emit) {
2324
const onToggleActive = () => emit('toggleActive')
2425

2526
const headerProps = Object.assign({ onToggleActive, onOpenPrefs }, state)
26-
const activeTabActionsProps = Object.assign({ onToggleSiteRedirect, onCopy, onPin, onUnPin }, state)
27+
const activeTabActionsProps = Object.assign({ onViewOnGateway, onToggleSiteRedirect, onCopy, onPin, onUnPin }, state)
2728
const opsProps = Object.assign({ onQuickUpload, onOpenWebUi, onToggleGlobalRedirect }, state)
2829

2930
return html`

add-on/src/popup/browser-action/store.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
const browser = require('webextension-polyfill')
55
const IsIpfs = require('is-ipfs')
66
const { trimHashAndSearch } = require('../../lib/ipfs-path')
7-
const { contextMenuCopyAddressAtPublicGw, contextMenuCopyRawCid, contextMenuCopyCanonicalAddress } = require('../../lib/context-menus')
7+
const { contextMenuViewOnGateway, contextMenuCopyAddressAtPublicGw, contextMenuCopyRawCid, contextMenuCopyCanonicalAddress } = require('../../lib/context-menus')
88

99
// The store contains and mutates the state for the app
1010
module.exports = (state, emitter) => {
@@ -63,6 +63,11 @@ module.exports = (state, emitter) => {
6363
}, 100)
6464
})
6565

66+
emitter.on('viewOnGateway', async () => {
67+
port.postMessage({ event: contextMenuViewOnGateway })
68+
window.close()
69+
})
70+
6671
emitter.on('copy', function (copyAction) {
6772
switch (copyAction) {
6873
case contextMenuCopyCanonicalAddress:

0 commit comments

Comments
 (0)