Last active
December 19, 2022 17:59
-
-
Save ademidun/29268d38592a84703812c7984f4863cd to your computer and use it in GitHub Desktop.
Helper scripts for using browser extensions.
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
// To test that your extension is working, we can trigger a request account | |
// copy the following scripts into your browser console | |
// source: https://gist.github.com/ademidun/29268d38592a84703812c7984f4863cd | |
ethereum | |
.request({ method: 'eth_requestAccounts' }) | |
.then((accounts)=>{ | |
console.log({accounts}); | |
}) | |
.catch((error) => { | |
console.error({error}) | |
}); | |
// add a Proxy to listen to when all methods on window.ethereum is called | |
// ChatGPT prompt: how can i listen to all the function calls for a given object | |
// ChatGPT: *returned a working function but only with `apply:`. Had to also include `get:` for it to work | |
// Chat GPT prompt: the proxy doesn't get called, please fix | |
window.ethereum = new Proxy(window.ethereum, { | |
// Proxy handler that intercepts function calls and property accesses | |
apply: function(target, thisArg, args) { | |
// Log the function call | |
console.log(`apply: ${target.name} called with arguments:`, args); | |
// Call the original function | |
return Reflect.apply(target, thisArg, args); | |
}, | |
get: function(target, prop) { | |
// Return a function if the property is a function | |
if (typeof target[prop] === "function") { | |
return function(...args) { | |
// Log the function call | |
console.log(`get: ${String(prop)} called with arguments:`, args); | |
// Call the original function | |
return target[prop](...args); | |
}; | |
} | |
// Return the original property value | |
return target[prop]; | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment