Skip to content

Commit

Permalink
feat: Adapt to new Etherscan and basescan versions, remove copy butto…
Browse files Browse the repository at this point in the history
…n for transaction hash
  • Loading branch information
0xbe37e committed Jun 13, 2024
1 parent 2caf921 commit 9d26963
Show file tree
Hide file tree
Showing 11 changed files with 443 additions and 192 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
### v5.0.7

- [fix] Remove the copy button for the new version of Etherscan transaction hash, and readapt some of the copy block buttons
- [feat] Adapted to the new version of basescan

### v5.0.6

- [fix] Replace copy-to-clipboard with clipboard-copy package
- [chore] Update node version requirement in CONTRIBUTING.md

### v5.0.5

- [fix] Fix the bug in the Fund Flow feature on the Solscan website that causes the current Solana address to be retrieved incorrectly
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "metasuites",
"version": "5.0.5",
"version": "5.0.7",
"repository": {
"type": "git",
"url": "https://github.com/blocksecteam/metasuites.git"
Expand Down
4 changes: 2 additions & 2 deletions src/common/config/allowlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ export default {
ETHERSCAN_V1_MATCHES: [
'*://*.arbiscan.io/*',
'*://cronoscan.com/*',
'*://*.basescan.org/*',
'*://gnosisscan.io/*',
'*://opbnb-testnet.bscscan.com/*',
'*://goerli-optimism.etherscan.io/*'
Expand Down Expand Up @@ -30,7 +29,8 @@ export default {
'*://zkevm.polygonscan.com/*',
'*://*.lineascan.build/*',
'*://*.wemixscan.com/*',
'*://testnet.ftmscan.com/*'
'*://testnet.ftmscan.com/*',
'*://*.basescan.org/*'
],
BTC_EXPLORER_MATCHES: ['*://*.btc.com/*'],
BLOCKSEC_MATCHES: ['*://*.blocksec.com/*'],
Expand Down
5 changes: 0 additions & 5 deletions src/common/scripts/README.md

This file was deleted.

157 changes: 0 additions & 157 deletions src/common/scripts/copy-address.tsx

This file was deleted.

7 changes: 7 additions & 0 deletions src/common/utils/dom.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import $ from 'jquery'

export const getNodeValue = (content: HTMLElement | null): string | null => {
if (!content) return null
const arr = []
Expand All @@ -20,3 +22,8 @@ export const setDeepestChildText = (
setDeepestChildText(element.lastChild, text)
}
}

export const hasSiblingWithClass = (el: HTMLElement, className: string) => {
if (!el) return false
return $(el).siblings(`.${className}`).length > 0
}
64 changes: 63 additions & 1 deletion src/content/btc/feat-scripts/copy-address.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,68 @@
import $ from 'jquery'
import React, { type ReactNode } from 'react'
import isMobile from 'is-mobile'
import { createRoot } from 'react-dom/client'

import { handleAddressNodeListCopy } from '@common/scripts/copy-address'
import { getHrefQueryVariable, pickAddress } from '@common/utils'
import { CopyButton } from '@common/components'

const appendIconToElement = (el: HTMLElement, reactNode: ReactNode) => {
if (!isMobile()) {
el.onmouseover = () => {
const btnEls = el.querySelectorAll<HTMLElement>(
'.__metadock-copy-address-btn__'
)
if (btnEls.length) {
btnEls.forEach(btnEl => {
btnEl.style.display = 'inline-block'
})
}
}
el.onmouseout = () => {
const btnEls = el.querySelectorAll<HTMLElement>(
'.__metadock-copy-address-btn__'
)
if (btnEls.length) {
btnEls.forEach(btnEl => {
btnEl.style.display = 'none'
})
}
}
}

el.setAttribute('style', 'padding-right:18px;position:relative')
const rootEl = document.createElement('span')
rootEl.classList.add('__metadock-copy-address-btn__')
rootEl.setAttribute(
'style',
`position:absolute;right:0;display:${isMobile() ? 'inline-block' : 'none'}`
)
el?.appendChild(rootEl)
createRoot(rootEl).render(reactNode)
}

const handleAddressNodeListCopy = (
addressTags: NodeListOf<HTMLElement> | HTMLElement[]
) => {
for (let i = 0; i < addressTags.length; i++) {
const el = addressTags[i]
let address: string | undefined
const href = el.getAttribute('href')
const dataOriginalTitle = el.getAttribute('data-original-title')
const title = el.getAttribute('title')
if (href) {
const tokenAddress = getHrefQueryVariable(href, 'a')
address = tokenAddress ?? pickAddress(href)
} else if (dataOriginalTitle) {
address = pickAddress(dataOriginalTitle)
} else if (title) {
address = pickAddress(title)
} else {
address = pickAddress(el.innerText)
}
if (address) appendIconToElement(el, <CopyButton text={address} />)
}
}

const genCopyAddressBtn = async () => {
const addressTags = $("a[href*='/btc/address' i]").toArray()
Expand Down
73 changes: 72 additions & 1 deletion src/content/etherscan/feat-scripts/copy-icon.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,57 @@
import React, { type ReactNode } from 'react'
import isMobile from 'is-mobile'
import { createRoot } from 'react-dom/client'

import { ETHERSCAN_PAGES } from '@common/constants'
import { handleBlockNodeListCopy } from '@common/scripts/copy-address'
import { CopyButton } from '@common/components'
import { validOrigin } from '@common/utils'

const appendIconToElement = (el: HTMLElement, reactNode: ReactNode) => {
if (!isMobile()) {
el.onmouseover = () => {
const btnEls = el.querySelectorAll<HTMLElement>(
'.__metadock-copy-address-btn__'
)
if (btnEls.length) {
btnEls.forEach(btnEl => {
btnEl.style.display = 'inline-block'
})
}
}
el.onmouseout = () => {
const btnEls = el.querySelectorAll<HTMLElement>(
'.__metadock-copy-address-btn__'
)
if (btnEls.length) {
btnEls.forEach(btnEl => {
btnEl.style.display = 'none'
})
}
}
}

el.setAttribute('style', 'padding-right:18px;position:relative')

const rootEl = document.createElement('span')
rootEl.classList.add('__metadock-copy-address-btn__')
rootEl.setAttribute(
'style',
`position:absolute;right:0;display:${isMobile() ? 'inline-block' : 'none'}`
)
el?.appendChild(rootEl)
createRoot(rootEl).render(reactNode)
}

const handleBlockNodeListCopy = (blockTags: NodeListOf<HTMLElement>) => {
for (let i = 0; i < blockTags.length; i++) {
const el = blockTags[i]
const href = el.getAttribute('href')
if (!href) continue
el.classList.remove('hash-tag')
const block = el.innerText.trim()
appendIconToElement(el, <CopyButton text={block} />)
}
}

/** show copy icon */
const genCopyIconBtn = async (pageName: string) => {
Expand All @@ -24,6 +76,25 @@ const genCopyIconBtn = async (pageName: string) => {
".card tbody a[href^='/block/']"
)
handleBlockNodeListCopy(blockTags)
const iframes = document.querySelectorAll('iframe')
for (let i = 0; i < iframes.length; ++i) {
const iframe = iframes[i]
if (validOrigin(iframe.src)) {
iframe.addEventListener(
'load',
function () {
const _document = iframe?.contentWindow?.document
if (_document) {
const iframeBlockTags = _document.querySelectorAll<HTMLElement>(
".table-responsive table tbody a[href^='/block/']"
)
handleBlockNodeListCopy(iframeBlockTags)
}
},
true
)
}
}
break
}
case ETHERSCAN_PAGES.BLOCKS_FORKED.name:
Expand Down
Loading

0 comments on commit 9d26963

Please sign in to comment.