3

我设法编写了以下代码。这个想法是能够弹出一个询问我想要发送多少 ETH 的窗口。

但我无法获得弹出窗口。我花了几个小时来解决这个问题,但无法得出结论。

我有时能够得到弹出窗口,但只有当我刷新页面时。

<!doctype html>
<html>
  <head>
    <title>MetaMask</title>

<style>
input[type=number], select {
    width: 50%;
    padding: 12px 20px;
    margin: 8px 0;
    display: inline-block;
    border: 1px solid #ccc;
    border-radius: 4px;
    box-sizing: border-box;
}

.tip-button {
  width: 304px;
  height: 79px;
  background-size: 100%;
  background-image: url('https://github.com/MetaMask/TipButton/raw/master/images/1_pay_mm_over.png');
  cursor: pointer;
}
.tip-button:hover {
  background-image: url('https://github.com/MetaMask/TipButton/raw/master/images/1_pay_mm_over.png');
}
.tip-button:active {
  background-image: url('https://github.com/MetaMask/TipButton/raw/master/images/1_pay_mm_off.png');
}
</style>


  </head>

  <body>
      <h3>To Proceed please click the METAMASK LOGO below.</h3>
    <h3>A popup will appear to ask you the ETH amount</h3>
    <div class="tip-button"></div>
    <div class="message"></div>
  </body>
<script>
var MY_ADDRESS = '0x7266D19108705D60066f5d0c29f60893A7B1fE14'
var tipButton = document.querySelector('.tip-button')
tipButton.addEventListener('click', function() {
  if (typeof web3 === 'undefined') {
    return renderMessage('<div>Looks like you dont have <a href=https://metamask.io>MetaMask</a> to use this feature.  <a href=https://metamask.io>https://metamask.io</a></div>')
  }
  var numSubmit = prompt("Please enter the ETH amount.");

    if(isNaN(numSubmit)) {
         numSubmit = prompt("A number is required.");
         numSubmit = +numSubmit;
    }

    else {
        numSubmit = +numSubmit;
    }
  var user_address = web3.eth.accounts[0]
  web3.eth.sendTransaction({
    to: MY_ADDRESS,
    from: user_address,
    value: web3.toWei(numSubmit, 'ether'),
    gas: 232575,
    gasPrice: 4000000000,
  }, function (err, transactionHash) {
    if (err) return renderMessage('There was a problem! Please reload the Page again.')
    // If you get a transactionHash, you can assume it was sent,
    // or if you want to guarantee it was received, you can poll
    // for that transaction to be mined first.
    renderMessage('Thank You')
  })
})
function renderMessage (message) {
  var messageEl = document.querySelector('.message')
  messageEl.innerHTML = message
}
</script>

</html>

谢谢!

4

0 回答 0