Skip to content

Instantly share code, notes, and snippets.

@ademidun
Last active December 19, 2022 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ademidun/29268d38592a84703812c7984f4863cd to your computer and use it in GitHub Desktop.
Save ademidun/29268d38592a84703812c7984f4863cd to your computer and use it in GitHub Desktop.
Helper scripts for using browser extensions.
// 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