Skip to content

Instantly share code, notes, and snippets.

@austintgriffith
Created January 3, 2019 15:30
Show Gist options
  • Save austintgriffith/75e732c5d5ae5a5712719e34708d6370 to your computer and use it in GitHub Desktop.
Save austintgriffith/75e732c5d5ae5a5712719e34708d6370 to your computer and use it in GitHub Desktop.
let messageBuffer = {}
module.exports = (web3)=>{
let burnerWeb3 = web3
var iframe = document.createElement('iframe')
iframe.style.opacity = 0.9
iframe.style.position = "absolute"
iframe.style.width = "300px"
iframe.style.height = "100%"
iframe.style.right = 0
iframe.style.top = 0
iframe.src = "http://xdai.io"
document.body.appendChild(iframe)
bindEvent(window, 'message', function (e) {
console.log("))))))))))))))))) MESSAGE RETURN",e)
try{
let message = e.data
let callback = messageBuffer[message.id]
if(callback&&typeof callback == "function"){
callback(...message.result)
}
}catch(e){console.log(e)}
});
iframe.contentWindow.postMessage("hello world", '*');
burnerWeb3.eth.oldGetAccounts = burnerWeb3.eth.getAccounts
/*burnerWeb3.eth.getAccounts = ()=>{
return new Promise(function(resolve, reject) {
console.log("############ GETTING ACCOUNTS")
let randomId = burnerWeb3.utils.randomHex(16)
messageBuffer[randomId] = resolve
iframe.contentWindow.postMessage(JSON.stringify({id:randomId,function:"eth.getAccounts",args:[]}), '*');
});
}*/
burnerWeb3.eth.getAccounts = (callback)=>{
console.log("############ GETTING OLD ACCOUNTS")
let randomId = burnerWeb3.utils.randomHex(16)
messageBuffer[randomId] = callback
iframe.contentWindow.postMessage({id:randomId,function:"eth.getAccounts",args:[]}, '*');
}
burnerWeb3.version = {}
console.log("burnerWeb3 version",burnerWeb3.version)
burnerWeb3.version.getNetwork = (callback)=>{
console.log("############ GETTING OLD NETWORK")
let randomId = burnerWeb3.utils.randomHex(16)
messageBuffer[randomId] = callback
iframe.contentWindow.postMessage({id:randomId,function:"version.getNetwork",args:[]}, '*');
}
burnerWeb3.eth.sendTransaction = (transactionObject,callback)=>{
console.log("############ eth.sendTransaction")
let randomId = burnerWeb3.utils.randomHex(16)
messageBuffer[randomId] = callback
iframe.contentWindow.postMessage({id:randomId,function:"eth.sendTransaction",args:[transactionObject]}, '*');
}
//
//burnerWeb3.eth.oldContract = web3.eth.Contract
//burnerWeb3.eth.Contract = (jsonInterface,address)=>{
// let newContract = new burnerWeb3.eth.oldContract(jsonInterface,address)
// console.log("new contract ",newContract)
// return newContract;
//}
//burnerWeb3.eth.Contract.prototype._executeMethod = function _executeMethod(){
//}
window.web3 = burnerWeb3
return burnerWeb3
}
function bindEvent(element, eventName, eventHandler) {
if (element.addEventListener) {
element.addEventListener(eventName, eventHandler, false);
} else if (element.attachEvent) {
element.attachEvent('on' + eventName, eventHandler);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment