Commit 4918646d authored by Stefy Spora's avatar Stefy Spora

Fix MetaMask detection in popup context

- Remove FetLife page requirement for Web3 donations
- Remove call to non-existent checkMetaMaskDirect() function
- Simplify Web3 donation event listener to work directly in popup context
- Fix conflicting event listeners that were causing MetaMask detection failure
- Web3 donations now work on any website, not just FetLife pages
parent 17f26d23
...@@ -309,80 +309,12 @@ document.addEventListener('DOMContentLoaded', function() { ...@@ -309,80 +309,12 @@ document.addEventListener('DOMContentLoaded', function() {
} }
} }
// Web3 donation event listener
web3DonateBtn.addEventListener('click', async function() { web3DonateBtn.addEventListener('click', async function() {
try { try {
web3DonateBtn.disabled = true; web3DonateBtn.disabled = true;
web3DonateBtn.textContent = '🔄 Connecting...'; web3DonateBtn.textContent = '🔄 Connecting...';
// Get the current active tab
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
// Check if we're on a FetLife page
if (!tab.url || !tab.url.includes('fetlife.com')) {
showWeb3Status('Please navigate to a FetLife page first.', true);
return;
}
// First, check if MetaMask is available via direct injection
const isMetaMaskAvailable = await checkMetaMaskDirect(tab.id);
if (!isMetaMaskAvailable) {
showWeb3Status('MetaMask not detected. Please ensure MetaMask is installed and unlocked.', true);
return;
}
// Then check if content script is loaded by sending a test message
try {
await chrome.tabs.sendMessage(tab.id, { action: 'ping' });
} catch (pingError) {
console.log('Ping failed, trying to inject content script:', pingError);
// If ping fails, try to inject the content script
try {
await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ['content.js']
});
// Wait a moment for the script to load
await new Promise(resolve => setTimeout(resolve, 500));
} catch (injectError) {
console.error('Failed to inject content script:', injectError);
showWeb3Status('Failed to load extension on this page. Try refreshing.', true);
return;
}
}
// Send message to content script to handle Web3 donation
const response = await chrome.tabs.sendMessage(tab.id, {
action: 'executeWeb3Donation'
});
showWeb3Status(response.message, !response.success);
} catch (error) {
console.error('Web3 donation error:', error);
// Handle different types of errors
if (error.message && error.message.includes('Could not establish connection')) {
showWeb3Status('Extension not loaded properly. Try refreshing the page.', true);
} else if (error.message && error.message.includes('No tab')) {
showWeb3Status('No active tab found.', true);
} else {
showWeb3Status('Failed to initiate donation: ' + (error.message || 'Unknown error'), true);
}
} finally {
web3DonateBtn.disabled = false;
web3DonateBtn.textContent = '🦊 Donate with MetaMask';
}
});
// Remove the old web3 event listener and add the new one
const oldBtn = document.getElementById('web3-donate-btn');
if (oldBtn) {
oldBtn.replaceWith(oldBtn.cloneNode(true));
const newBtn = document.getElementById('web3-donate-btn');
newBtn.addEventListener('click', async function() {
try {
newBtn.disabled = true;
newBtn.textContent = '🔄 Connecting...';
// Execute Web3 donation directly in popup context // Execute Web3 donation directly in popup context
const result = await executeWeb3Donation(); const result = await executeWeb3Donation();
showWeb3Status(result.message, !result.success); showWeb3Status(result.message, !result.success);
...@@ -391,11 +323,10 @@ document.addEventListener('DOMContentLoaded', function() { ...@@ -391,11 +323,10 @@ document.addEventListener('DOMContentLoaded', function() {
console.error('Web3 donation error:', error); console.error('Web3 donation error:', error);
showWeb3Status('Failed to initiate donation: ' + (error.message || 'Unknown error'), true); showWeb3Status('Failed to initiate donation: ' + (error.message || 'Unknown error'), true);
} finally { } finally {
newBtn.disabled = false; web3DonateBtn.disabled = false;
newBtn.textContent = '🦊 Donate with MetaMask'; web3DonateBtn.textContent = '🦊 Donate with MetaMask';
} }
}); });
}
}); });
// Function to find and click the next link // Function to find and click the next link
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment