This commit is contained in:
Sewmina 2024-08-19 22:21:44 +05:30
commit ed26d9335a
3 changed files with 110 additions and 0 deletions

31
background.js Normal file
View File

@ -0,0 +1,31 @@
chrome.runtime.onInstalled.addListener(() => {
chrome.tabs.query({ url: "*://*.x.com/*" }, (tabs) => {
for (let tab of tabs) {
chrome.scripting.executeScript({
target: {tabId: tab.id, allFrames: true},
files: ['content.js']
});
}
});
});
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && /^https:\/\/(www\.)?(x|twitter)\.com/.test(tab.url)) {
chrome.scripting.executeScript({
target: {tabId: tabId, allFrames: true},
files: ['content.js']
});
}
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "fetchData") {
fetch(request.url)
.then(response => response.json())
.then(data => {
sendResponse({ data: data });
})
.catch(error => console.error("Error fetching token data:", error));
return true; // Will respond asynchronously
}
});

56
content.js Normal file
View File

@ -0,0 +1,56 @@
const DEXSCREENER_API_URL = "https://api.dexscreener.io/latest/dex/tokens/";
function isSmartContract(address) {
return address.includes('0x');
}
function fetchTokenData(contractAddress,x,y) {
const url = `${DEXSCREENER_API_URL}${contractAddress}`;
chrome.runtime.sendMessage({ action: "fetchData", url: url }, (response) => {
const data = response.data.pairs[0]; // Assuming the first pair is what you want
if (data) {
//alert(data.baseToken.symbol);
const tooltip = createTooltip(data.baseToken.symbol, data.priceUsd || data.priceNative, data.url,x,y);
document.body.appendChild(tooltip);
}
});
}
function extractTicker(text){
const index = text.indexOf('0x');
if (index !== -1 && text.length >= index + 42) { // 2 characters for '0x' + 40 characters for the address
return text.substring(index, index + 42); // Extract '0x' + 40 characters
}
return null;
}
function createTooltip(ticker, price, chartUrl,x,y) {
const tooltip = document.createElement('div');
tooltip.style.position = 'absolute';
tooltip.style.background = '#333';
tooltip.style.color = '#fff';
tooltip.style.padding = '5px';
tooltip.style.borderRadius = '5px';
tooltip.style.zIndex = '9999';
tooltip.style.left = `${x}px`;
tooltip.style.top = `${y}px`;
tooltip.innerHTML = `Ticker: ${ticker}<br>Price: ${price}`;
tooltip.addEventListener('click', () => window.open(chartUrl, '_blank'));
return tooltip;
}
function handleMouseOver(event) {
const target = event.target;
if (target.nodeType === Node.TEXT_NODE || target.nodeType === Node.ELEMENT_NODE) {
const text = target.textContent.trim();
if (isSmartContract(text)) {
const extractedTicker = extractTicker(text);
const rect = target.getBoundingClientRect(); // Get the target's position relative to the viewport
const x = rect.left + window.scrollX; // X-coordinate
const y = rect.top + window.scrollY;
fetchTokenData(extractedTicker,x,y);
}
}
}
document.addEventListener('mouseover', handleMouseOver);

23
manifest.json Normal file
View File

@ -0,0 +1,23 @@
{
"manifest_version": 3,
"name": "Dexscreener Smart Contract Detector",
"version": "1.0",
"description": "Detect smart contracts in tweets, show ticker and price, and redirect to Dexscreener chart.",
"permissions": [
"activeTab",
"scripting",
"storage"
],
"background": {
"service_worker": "background.js"
},
"content_scripts": [
{
"matches": ["*://x.com/*", "*://twitter.com/*"],
"js": ["content.js"]
}
],
"host_permissions": [
"https://api.dexscreener.io/*"
]
}