commit ed26d9335a5bff1de0706464bdb0fd4b6f334930 Author: Sewmina Date: Mon Aug 19 22:21:44 2024 +0530 init diff --git a/background.js b/background.js new file mode 100644 index 0000000..08500ba --- /dev/null +++ b/background.js @@ -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 + } +}); diff --git a/content.js b/content.js new file mode 100644 index 0000000..9a1a813 --- /dev/null +++ b/content.js @@ -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}
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); diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..e9de66e --- /dev/null +++ b/manifest.json @@ -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/*" + ] +} \ No newline at end of file