Mouse middle click

by gsguma

HTML

<div id="banner-message">
  <p>Hello World</p>
  <a id="btn-view-account-page-url" href="https://hp.com.com">View Customer Account</a>
</div>

CSS

body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#banner-message {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  font-size: 25px;
  text-align: center;
  transition: all 0.2s;
  margin: 0 auto;
  width: 300px;
}

button {
  background: #0084ff;
  border: none;
  border-radius: 5px;
  padding: 8px 14px;
  font-size: 15px;
  color: #fff;
}

#banner-message.alt {
  background: #0084ff;
  color: #fff;
  margin-top: 40px;
  width: 200px;
}

#banner-message.alt button {
  background: #fff;
  color: #000;
}

JavaScript

$(function() {
  "use strict";
  var chargeEventId;
  var currentTab;

  function main() {
    currentTab = null;
    onPageReady();
  }

  function onPageReady() {
    registerEventListeners();
  }

  function registerEventListeners() {
    registerClickListeners();
    registerMiddleClickListeners(); /* NOVO CODIGO */
  }

  function registerClickListeners() {
    var selectorFunction = {
      "#btn-view-account-page-url": accountPageClick
    };

    for (var key in selectorFunction) {
      if (selectorFunction.hasOwnProperty(key)) {
        $(key).click(selectorFunction[key]);
      }
    }
  }

	/* NOVO CODIGO */
  function registerMiddleClickListeners() {
    var selectorFunction = {
      "#btn-view-account-page-url": accountPageClick
    };

    for (var key in selectorFunction) {
      if (selectorFunction.hasOwnProperty(key)) {
        $(key).on('mousedown', function(event) {
          selectorFunction[key](event);
        });
      }
    }
  }

  function accountPageClick(event) {
    var element = $(this);
    var openWindow = false;

    $.ajax('https://jsonplaceholder.typicode.com/todos/1', {
      type: 'get',
      async: false
    }).done(onDone);

    function onDone(data) {
      element.attr('href', 'https://www.google.com/search?q=' + data.userId);
      openWindow = true;
    }

		/* NOVO CODIGO - Talvez não seja necessário o window.open */
    if (event.which == 2) {
      window.open(element.attr('href'));
      event.preventDefault();
    }
    /* NOVO CODIGO */

    return openWindow;
  }

  main();
});