Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: copy current IPFS path for IPNS address #532

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions add-on/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
"message": "Copy Public Gateway URL",
"description": "A menu item in Browser Action pop-up and right-click context menu (panel_copyCurrentPublicGwUrl)"
},
"panel_copyResolveIpns": {
"message": "Copy IPNS Resolved to IPFS",
"description": "A menu item in Browser Action pop-up (panel_copyResolveIpns)"
},
"pageAction_titleIpfsAtPublicGateway": {
"message": "IPFS resource loaded via Public Gateway",
"description": "A tooltip displayed over Page Action in location bar (pageAction_titleIpfsAtPublicGateway)"
Expand Down Expand Up @@ -107,6 +111,10 @@
"message": "Copied Canonical Address",
"description": "A title of system notification (notify_copiedCanonicalAddressTitle)"
},
"notify_copiedResolvedIpns": {
"message": "Copied Resolved IPFS Address",
"description": "A title of system notification (notify_copiedResolvedIpns)"
},
"notify_pinnedIpfsResourceTitle": {
"message": "IPFS Resource is now pinned",
"description": "A title of system notification (notify_pinnedIpfsResourceTitle)"
Expand Down
17 changes: 13 additions & 4 deletions add-on/src/lib/copier.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,30 @@ async function copyTextToClipboard (copyText) {
}

function createCopier (getState, notify) {
const copy = (text, notificationTitle) => {
copyTextToClipboard(text)
if (notify && notificationTitle) {
notify(notificationTitle, text)
}
}
return {
async copyCanonicalAddress (context) {
const url = await findUrlForContext(context)
const rawIpfsAddress = safeIpfsPath(url)
copyTextToClipboard(rawIpfsAddress)
notify('notify_copiedCanonicalAddressTitle', rawIpfsAddress)
copy(rawIpfsAddress, 'notify_copiedCanonicalAddressTitle')
},

async copyAddressAtPublicGw (context) {
const url = await findUrlForContext(context)
const state = getState()
const urlAtPubGw = url.replace(state.gwURLString, state.pubGwURLString)
copyTextToClipboard(urlAtPubGw)
notify('notify_copiedPublicURLTitle', urlAtPubGw)
copy(urlAtPubGw, 'notify_copiedPublicURLTitle')
},

async copy (text, notificationTitle) {
copy(text, notificationTitle)
}

}
}

Expand Down
3 changes: 2 additions & 1 deletion add-on/src/lib/ipfs-companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ module.exports = async function init () {
const BrowserActionMessageHandlers = {
notification: (message) => notify(message.title, message.message),
copyCanonicalAddress: () => copier.copyCanonicalAddress(),
copyAddressAtPublicGw: () => copier.copyAddressAtPublicGw()
copyAddressAtPublicGw: () => copier.copyAddressAtPublicGw(),
copyResolvedIpnsAddress: (message) => copier.copy(message.text, 'notify_copiedResolvedIpns')
}

function handleMessageFromBrowserAction (message) {
Expand Down
12 changes: 12 additions & 0 deletions add-on/src/lib/ipfs-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ function safeIpfsPath (urlOrPath) {

exports.safeIpfsPath = safeIpfsPath

async function resolveIpfsPath (ipfs, urlOrPath) {
const path = safeIpfsPath(urlOrPath) // https://github.com/ipfs/ipfs-companion/issues/303
if (/^\/ipns/.test(path)) {
const response = await ipfs.name.resolve(path, {recursive: true, nocache: false})
// old versions of API used object with Path field
return response.Path ? response.Path : response
}
return path
}

exports.resolveIpfsPath = resolveIpfsPath

function urlAtPublicGw (path, pubGwUrl) {
return new URL(`${pubGwUrl}${path}`).toString().replace(/([^:]\/)\/+/g, '$1')
}
Expand Down
12 changes: 11 additions & 1 deletion add-on/src/popup/browser-action/context-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = function contextActions ({
active,
ipfsNodeType,
isIpfsContext,
isIpnsContext,
isPinning,
isUnPinning,
isPinned,
Expand All @@ -17,10 +18,12 @@ module.exports = function contextActions ({
onCopyIpfsAddr,
onCopyPublicGwAddr,
onPin,
onUnPin
onUnPin,
onCopyResolvedIpnsAddr
}) {
if (!isIpfsContext) return null
const activePinControls = active && isIpfsOnline && isApiAvailable && !(isPinning || isUnPinning)
const activeIpnsResolver = active && isIpfsOnline && isApiAvailable
return html`
<div class='fade-in pv1'>
${navItem({
Expand All @@ -31,6 +34,13 @@ module.exports = function contextActions ({
text: browser.i18n.getMessage('panel_copyCurrentPublicGwUrl'),
onClick: onCopyPublicGwAddr
})}
${isIpnsContext ? (
navItem({
text: browser.i18n.getMessage('panel_copyResolveIpns'),
disabled: !activeIpnsResolver,
onClick: onCopyResolvedIpnsAddr
})
) : null}
${!isPinned ? (
navItem({
text: browser.i18n.getMessage('panel_pinCurrentIpfsAddress'),
Expand Down
3 changes: 2 additions & 1 deletion add-on/src/popup/browser-action/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = function browserActionPage (state, emit) {
const onCopyPublicGwAddr = () => emit('copyPublicGwAddr')
const onPin = () => emit('pin')
const onUnPin = () => emit('unPin')
const onCopyResolvedIpnsAddr = () => emit('copyResolvedIpnsAddr')

const onQuickUpload = () => emit('quickUpload')
const onOpenWebUi = () => emit('openWebUi')
Expand All @@ -23,7 +24,7 @@ module.exports = function browserActionPage (state, emit) {
const onToggleActive = () => emit('toggleActive')

const headerProps = Object.assign({ onToggleNodeType, onToggleActive, onOpenPrefs }, state)
const contextActionsProps = Object.assign({ onCopyIpfsAddr, onCopyPublicGwAddr, onPin, onUnPin }, state)
const contextActionsProps = Object.assign({ onCopyIpfsAddr, onCopyPublicGwAddr, onPin, onUnPin, onCopyResolvedIpnsAddr }, state)
const opsProps = Object.assign({ onQuickUpload, onOpenWebUi, onToggleRedirect }, state)

return html`
Expand Down
31 changes: 18 additions & 13 deletions add-on/src/popup/browser-action/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint-env browser, webextensions */

const browser = require('webextension-polyfill')
const { safeIpfsPath } = require('../../lib/ipfs-path')
const { resolveIpfsPath } = require('../../lib/ipfs-path')

// The store contains and mutates the state for the app
module.exports = (state, emitter) => {
Expand All @@ -11,6 +11,7 @@ module.exports = (state, emitter) => {
active: true,
// UI state
isIpfsContext: false,
isIpnsContext: false,
isPinning: false,
isUnPinning: false,
isPinned: false,
Expand Down Expand Up @@ -65,13 +66,25 @@ module.exports = (state, emitter) => {
window.close()
})

emitter.on('copyResolvedIpnsAddr', async function copyResolvedIpnsAddress () {
try {
const ipfs = await getIpfsApi()
const currentPath = await resolveIpfsPath(ipfs, new URL(state.currentTab.url).pathname)
port.postMessage({ event: 'copyResolvedIpnsAddress', text: currentPath })
} catch (error) {
console.error('Unable to resolve and copy IPNS address due to', error)
notify('notify_addonIssueTitle', 'notify_addonIssueMsg')
}
window.close()
})

emitter.on('pin', async function pinCurrentResource () {
state.isPinning = true
emitter.emit('render')

try {
const ipfs = await getIpfsApi()
const currentPath = await resolveToIPFS(ipfs, new URL(state.currentTab.url).pathname)
const currentPath = await resolveIpfsPath(ipfs, new URL(state.currentTab.url).pathname)
const pinResult = await ipfs.pin.add(currentPath, { recursive: true })
console.log('ipfs.pin.add result', pinResult)
state.isPinned = true
Expand All @@ -90,7 +103,7 @@ module.exports = (state, emitter) => {

try {
const ipfs = await getIpfsApi()
const currentPath = await resolveToIPFS(ipfs, new URL(state.currentTab.url).pathname)
const currentPath = await resolveIpfsPath(ipfs, new URL(state.currentTab.url).pathname)
const result = await ipfs.pin.rm(currentPath, {recursive: true})
state.isPinned = false
console.log('ipfs.pin.rm result', result)
Expand Down Expand Up @@ -192,6 +205,7 @@ module.exports = (state, emitter) => {
// Check if current page is an IPFS one
state.isIpfsContext = status.ipfsPageActionsContext || false
state.currentTab = status.currentTab || null
state.isIpnsContext = state.isIpfsContext && state.currentTab && new URL(status.currentTab.url).pathname.startsWith('/ipns/')

// browser.pageAction-specific items that can be rendered earlier (snappy UI)
requestAnimationFrame(async () => {
Expand Down Expand Up @@ -244,7 +258,7 @@ module.exports = (state, emitter) => {
// skip update if there is an ongoing pin or unpin
if (state.isPinning || state.isUnPinning) return
try {
const currentPath = await resolveToIPFS(ipfs, new URL(status.currentTab.url).pathname)
const currentPath = await resolveIpfsPath(ipfs, new URL(status.currentTab.url).pathname)
const response = await ipfs.pin.ls(currentPath, {quiet: true})
console.log(`positive ipfs.pin.ls for ${currentPath}: ${JSON.stringify(response)}`)
state.isPinned = true
Expand Down Expand Up @@ -272,12 +286,3 @@ async function getIpfsApi () {
const bg = await getBackgroundPage()
return (bg && bg.ipfsCompanion) ? bg.ipfsCompanion.ipfs : null
}

async function resolveToIPFS (ipfs, path) {
path = safeIpfsPath(path) // https://github.com/ipfs/ipfs-companion/issues/303
if (/^\/ipns/.test(path)) {
const response = await ipfs.name.resolve(path, {recursive: true, nocache: false})
return response.Path
}
return path
}
3 changes: 2 additions & 1 deletion add-on/src/popup/page-action/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ const contextActions = require('../browser-action/context-actions')
module.exports = function pageActionPage (state, emit) {
const onCopyIpfsAddr = () => emit('copyIpfsAddr')
const onCopyPublicGwAddr = () => emit('copyPublicGwAddr')
const onCopyResolvedIpnsAddr = () => emit('copyResolvedIpnsAddr')
const onPin = () => emit('pin')
const onUnPin = () => emit('unPin')
const contextActionsProps = Object.assign({ onCopyIpfsAddr, onCopyPublicGwAddr, onPin, onUnPin }, state)
const contextActionsProps = Object.assign({ onCopyIpfsAddr, onCopyPublicGwAddr, onCopyResolvedIpnsAddr, onPin, onUnPin }, state)

// Instant init: page-action is shown only in ipfsContext
contextActionsProps.isIpfsContext = true
Expand Down