Created
March 2, 2024 10:29
-
-
Save TangMonk/b67a49c2c9bc80295f3ea2388730b191 to your computer and use it in GitHub Desktop.
Dexscreener Avatar Improve
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Dexscreener Avatar Improve | |
// @namespace http://tampermonkey.net/ | |
// @version 1.3 | |
// @description Fetch notes for Ethereum addresses and display them as real popovers on hover | |
// @author You | |
// @match https://dexscreener.com/ethereum/* | |
// @grant GM_xmlhttpRequest | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
function fetchLpInformation(lpAddress, element) { | |
GM_xmlhttpRequest({ | |
method: 'GET', | |
url: `https://api.dexscreener.com/latest/dex/pairs/ethereum/${lpAddress}`, | |
onload: function(response) { | |
const data = JSON.parse(response.responseText); | |
if (data && data.pairs.length > 0) { | |
const baseTokenAddress = data.pairs[0].baseToken.address; | |
const avatarImageUrl = `https://dd.dexscreener.com/ds-data/tokens/ethereum/${baseTokenAddress}.png?size=lg`; | |
appendAvatarImageToElement(avatarImageUrl, element); | |
} | |
} | |
}); | |
} | |
function appendAvatarImageToElement(imageUrl, element) { | |
const img = document.createElement('img'); | |
img.src = imageUrl; | |
img.style.cssText = 'height: 30px; width: 30px; margin-left: 5px;align-self: center;margin: 0 5px;'; | |
img.onerror = function() { | |
// 当图片加载失败时的处理 | |
this.style.display = 'none'; // 如果不想显示图片,可以选择隐藏 | |
// 或者显示一个黑色的框 | |
const fallbackDiv = document.createElement('div'); | |
fallbackDiv.style.cssText = 'width: 30px; height: 30px; background-color: black; margin-right: 5px;'; | |
element.parentNode.insertBefore(fallbackDiv, element); | |
}; | |
img.onload = function() { | |
// 图片加载成功,插入到指定元素的前面 | |
element.parentNode.insertBefore(img, element); | |
}; | |
} | |
const processElements = () => { | |
document.querySelectorAll('a[href*="/ethereum/"]').forEach(element => { | |
if (!element.hasAttribute('data-avatar-not-proccessed')) { | |
element.setAttribute('data-avatar-not-proccessed', 'true'); | |
const href = element.getAttribute('href'); | |
const addressLpMatch = href.match(/\/ethereum\/(0x[a-fA-F0-9]+)/); | |
if(addressLpMatch && addressLpMatch[1]) { | |
fetchLpInformation(addressLpMatch[1], element); | |
} | |
} | |
}); | |
}; | |
window.addEventListener('load', processElements); | |
setInterval(processElements, 1000); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment