In my Firefox's & Google Chrome's extensions I can prevent default behavior when middle click on a link like this:
function onClick(e) {
var url = getLink(e);
if (e.button == 1) { // Middle Click
// prevent opening a link
e.preventDefault();
e.stopPropagation();
// do something with a link
// url ...
}
}
if (browser == "chrome")
document.addEventListener("auxclick", onClick); // for Google Chrome (old "click" event doesn't prevent opening link with middle click but they added "auxclick" for this)
else if (browser == "firefox")
document.addEventListener("click", onClick); // for Firefox (still works)
also https://developers.google.com/web/updates/2016/10/auxclick, https://developer.mozilla.org/en-US/docs/Web/Events/auxclick
I'm also trying to do this for my Microsoft Edge's extension, but seems middle click event for this browser doesn't work at all:
function onClick(e) {
var url = getLink(e);
if (e.button == 1) { // Middle Click
alert("hello"); // isn't working for Microsoft Edge
}
}
document.addEventListener("click", onClick);
So instead of this for Microsoft Edge I use:
document.addEventListener("mousedown", function(e) {
var target = e.target || e.srcElement;
while (target) {
if (target instanceof HTMLAnchorElement)
break;
target = target.parentNode;
}
if (e.button == 1 && target.href != null) {
alert("hello"); // works when middle click on a link
// but these preventing doesn't works here:
e.preventDefault();
e.stopPropagation();
// link will still be opened in a new tab
}
});
But this method doesn't prevent link from opening in a new tab when middle click
How can I make Microsoft Edge behave like Google Chrome or Firefox?