Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
Shows extra data in NFT Post pages
Browse files Browse the repository at this point in the history
  • Loading branch information
iPaulPro committed Nov 2, 2021
1 parent e9280bb commit 0648637
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
15 changes: 15 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,21 @@ const isFollowingPublicKey = (publicKey, isFollowingPublicKey) => {
.then(res => res.json())
}

const getSinglePost = (postHashHex, fetchParents = false, commentLimit = 0) => {
if (!postHashHex) return Promise.reject('Missing required parameter')

const request = buildRequest('omit')
request.body = JSON.stringify({
PostHashHex: postHashHex,
ReaderPublicKeyBase58Check: getLoggedInPublicKey(),
FetchParents: fetchParents,
CommentLimit: commentLimit
})

return fetch(`${getBaseUrl()}/api/v0/get-single-post`, request)
.then(res => res.json())
}

const getBidsForNftPost = (publicKey, postHashHex) => {
if (!publicKey || !isFollowingPublicKey) return Promise.reject('Missing required parameter')

Expand Down
54 changes: 48 additions & 6 deletions lib/nft.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ const isTransferNftUrl = () => {
return segments[1] === 'nft' && segments[segments.length - 1] === 'transfer'
}

const getNftPostPageFooter = (nftPostPage) => {
const nftPost = nftPostPage.querySelector('nft-post')

const feedPostElement = nftPost.querySelector('feed-post')
if (!feedPostElement) return

return feedPostElement.firstElementChild.lastElementChild
}

const enrichNftPostPage = (nftPostPage) => {
if (!nftPostPage || isRequestingNftEntries) return

Expand All @@ -37,16 +46,13 @@ const enrichNftPostPage = (nftPostPage) => {
const postHashHex = getPostHashHexFromUrl()
if (!publicKey || !postHashHex) return

const nftPost = nftPostPage.querySelector('nft-post')

const feedPostElement = nftPost.querySelector('feed-post')
if (!feedPostElement) return

const footerElement = feedPostElement.firstElementChild.lastElementChild
const footerElement = getNftPostPageFooter(nftPostPage)
if (!footerElement) return

isRequestingNftEntries = true

addNftExtraDataToNftPage(nftPostPage)

getNftEntriesForPostHashHex(publicKey, postHashHex)
.then(nftEntries => {
const ownedEntries = nftEntries.filter(entry => entry['OwnerPublicKeyBase58Check'] === publicKey)
Expand Down Expand Up @@ -815,3 +821,39 @@ const addNftTransfersMenuItem = () => {

checkForNftTransfers()
}

const addNftExtraDataToNftPage = (nftPostPage) => {
const extraDataLinkId = 'plus_nft-extra-data-link'
if (document.getElementById(extraDataLinkId)) return

const postHashHex = getPostHashHexFromUrl()
getSinglePost(postHashHex)
.then(data => data['PostFound']['PostExtraData'])
.then(postExtraData => {
if (!postExtraData || _.isEmpty(postExtraData) || document.getElementById(extraDataLinkId)) return

const json = document.createElement('pre')
json.innerText = JSON.stringify(postExtraData, null, 2)
json.className = 'plus-text-primary d-none p-3 bg-dark rounded mt-2'
json.style.whiteSpace = 'pre'

const link = document.createElement('div')
link.id = extraDataLinkId
link.className = 'cursor-pointer'
link.innerText = "Show Extra Data"
link.onclick = () => {
if (json.classList.contains('d-none')) {
json.classList.remove('d-none')
link.innerText = "Hide Extra Data"
} else {
json.classList.add('d-none')
link.innerText = "Show Extra Data"
}
}

const footer = getNftPostPageFooter(nftPostPage)
footer?.appendChild(link)
footer?.appendChild(json)
})
.catch()
}

0 comments on commit 0648637

Please sign in to comment.