Home > Enterprise >  EventListener and onClick - why do I have to click twice to trigger?
EventListener and onClick - why do I have to click twice to trigger?

Time:01-14

I am trying to extract base64 HTML image data after clicking a button which loads the base64 data from the server.

When testing locally, I have to click twice initially to get the expected results. And every subsequent click works as expected.

How can I shape this code so that it triggers on only one click?

<html>
<button id="t-load" type="button" data-board="g" data-tid="0" style="font-size: 11px; padding: 0px; width: 90px; box-sizing: border-box; margin: 0px 6px 0px 0px; vertical-align: middle; height: 18px;">Get Captcha</button>

<div id="t-fg" style="width: 100%; height: 100%; position: absolute; background-repeat: no-repeat; background-position: left top; background-image: url(&quot;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPcAAABQAgMAAAC4 wuWAAAACVBMVEUL Avu7u4AAACitRlfAAAAAXRSTlMAQObYZgAABsFJREFUWIWFmMGO2zYQhrlGt0B92i6iBZpTLwsUegq3aA71ySuIRKxTA9RFwqdYFHUPObXFsmh9Wi8iQ5qn7D8zpETZ3qyCJDQ1nzj8hxxxZKxewaaroS411/bc5dqh1Rlt CO81 Z5/JBay4ST/neHvzX1yRlPE0z7rNtluHZSHI79dv3gCFqDpwDjhEKG0wRnxx3lOBXp1 ZoEsuiAS7P8m30lqhoaEBwj/Ynk9erpsJHfBM1Bkn0G1EYrSY/8svTNT2Y3KWNI72Ksa8c4zi5Kpjta5N39Y6eBM8GxHy253Axm KtRY/w4kvC6UQ1nWbbTnB3oA1LosPH Hk6UHsyfxhdd466yeiHPs2qyPB2SeXx4I7aKtgcx/rZ5XgTQ0ptrasiTHD leNNsA1H/w3wyg6rB1PUJ2FANVzLTdazyfFeV3X5UZVz8V96LHRRJgGgjxc8HOFylQheMpPRzQXmsx1xbLi171bFMnjB12nuULSH/SHfaDCBjd Pux8C43HGYHlOlefBMHqbi QfjSmsy1Y cI/laqDwQ4avbd0Br0uWaHWR8HtjLq19N9pBYE8rY0petONIvDslb8DaLswU12tV6FzfEXrnVNRmTAeB8aqQdg3GReWMSZ7YocW9sxZmuUxOVzfcgG8X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f5fbkMtThEzqYjXlEqjnmtD3De8YHQ2AGv0rsCRWwVuESuOGuwqA3SbixfgRcujq4HzJSoKdZKsKt6JycZGZ16x4ZNiH1cF6nln4XienTV tb6J0wAplwQFuoun4OdVjI4VR341WHTVet5s3FawDflU68VlNPTkh9PzCPehAnuIXsp0DWiHguw6gzuW9rRWLak0RtWmuP3RJ/6VHQ0UiuFEa6lVtuc4BxzhB5iP1F7l42WT5PnAzVe74cKj8sPeUmJY5wFficWv472zcRzlmNvxfm0ihRnq5pPIrccdyo4SDdncX73j58XvJTAjdXCs0vysng8wPjlQ0aT2tiTHcpRxje8LZa7VHW7Av/3MN1nEqXPMiXj2skeytwdBEJvwVWtC67/lWw85Y54mkUFwy53yNgP7DBXiTi0Y8V1NuJIFWHEP8QmTb5FAA8JR6AFXkV8/IKRfas5xiVqvSR3mKR7gqcf acHorusko9lYK CpCI3BMaXUaWhdMZTtyzw M0rOn9QS9l29db2PeOa6lK34oeuzD4EpGNhwmUBIXA8eseebnOnGO/tBP8ftdeFnJm6qUIAAAAASUVORK5CYII=&quot;);"></div>
</html>


<script>
// turn string to image
function b64toImageData(datas) {
    return new Promise(async function (resolve, reject) {

        // We create an image to receive the Data URI
        var img = document.createElement('img');

        // When the event "onload" is triggered we can resize the image.
        img.onload = function () {
        // We create a canvas and get its context.
        var canvas = document.createElement('canvas');
        var ctx = canvas.getContext('2d');

        // We set the dimensions at the wanted size.
        canvas.width = img.width;
        canvas.height = img.height;

        // We resize the image with the canvas method drawImage();
        ctx.drawImage(this, 0, 0, img.width, img.height);
        // get image data
        var imageDatas = ctx.getImageData(0, 0, img.width, img.height);

        // This is the return of the Promise
        resolve(imageDatas);
        };
        // add data to img
        img.src = datas;
    })
    }

// event listener;
document.addEventListener("click", function () {
  document.getElementById("t-load").onclick = async function () {
        // get img
        var fg = document.getElementById("t-fg").style.backgroundImage.slice(5, -2);
        console.log(fg)
        // get img data
        var base_img = await b64toImageData(fg);
        // log results
        console.log(base_img.data);
  }

});
</script>

CodePudding user response:

Instead of the event handlers that you are using, you can delegate the click handler to the body and use still async.

function b64toImageData(datas) {
  return new Promise(async function(resolve, reject) {

    // We create an image to receive the Data URI
    var img = document.createElement('img');

    // When the event "onload" is triggered we can resize the image.
    img.onload = function() {
      // We create a canvas and get its context.
      var canvas = document.createElement('canvas');
      var ctx = canvas.getContext('2d');

      // We set the dimensions at the wanted size.
      canvas.width = img.width;
      canvas.height = img.height;

      // We resize the image with the canvas method drawImage();
      ctx.drawImage(this, 0, 0, img.width, img.height);
      // get image data
      var imageDatas = ctx.getImageData(0, 0, img.width, img.height);

      // This is the return of the Promise
      resolve(imageDatas);
    };
    // add data to img
    img.src = datas;
  })
}

document.body.addEventListener("click", async(e) => {
  let el = e.target;
  if (el.id == "t-load") {
    var fg = document.getElementById("t-fg").style.backgroundImage.slice(5, -2);
    console.log(fg)
    console.log("WAITING!! don't click again!")
    var base_img = await b64toImageData(fg);
    console.log(base_img.data);
  }
});
<button id="t-load" type="button" data-board="g" data-tid="0" style="font-size: 11px; padding: 0px; width: 90px; box-sizing: border-box; margin: 0px 6px 0px 0px; vertical-align: middle; height: 18px;">Get Captcha</button>

<div id="t-fg" style="width: 100%; height: 100%; position: absolute; background-repeat: no-repeat; background-position: left top; background-image: url(&quot;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPcAAABQAgMAAAC4 wuWAAAACVBMVEUL Avu7u4AAACitRlfAAAAAXRSTlMAQObYZgAABsFJREFUWIWFmMGO2zYQhrlGt0B92i6iBZpTLwsUegq3aA71ySuIRKxTA9RFwqdYFHUPObXFsmh9Wi8iQ5qn7D8zpETZ3qyCJDQ1nzj8hxxxZKxewaaroS411/bc5dqh1Rlt CO81 Z5/JBay4ST/neHvzX1yRlPE0z7rNtluHZSHI79dv3gCFqDpwDjhEKG0wRnxx3lOBXp1 ZoEsuiAS7P8m30lqhoaEBwj/Ynk9erpsJHfBM1Bkn0G1EYrSY/8svTNT2Y3KWNI72Ksa8c4zi5Kpjta5N39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