Submit to Tab with Ctrl + Click / Ctrl + Enter / Middle click

Sets form's target to `_blank` when submitted while Control key is being pressed. Allow submit with middle mouse button.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        Submit to Tab with Ctrl + Click / Ctrl + Enter / Middle click
// @description Sets form's target to `_blank` when submitted while Control key is being pressed. Allow submit with middle mouse button.
// @namespace   http://eldar.cz/myf/
// @license     CC0
// @version     2.2.0
// @grant       none
// @include     *
// ==/UserScript==
// https://greasyfork.org/en/scripts/9954/versions/new
const controlKey = "Control";
const middleMouseButton = 4;
let isControlPressed = false;
let formElement = null;
let pressedButton = null;
let originalTarget = '';
window.addEventListener( 'submit', onSubmit, true );
window.addEventListener( 'keydown', onKeydown, true );
window.addEventListener( 'keyup', onKeyup, true );
window.addEventListener( 'mousedown', onMousedown, true );
window.addEventListener( 'mouseup', onMouseup, true );
window.addEventListener( 'blur', onBlur, false );
function onSubmit ( event ) {
 if ( isControlPressed === true && formElement === null ) {
   formElement = event.target;
   originalTarget = formElement.target;
   formElement.target = '_blank';
 }
}
function onKeydown ( event ) {
 if ( event.key === controlKey ) {
   isControlPressed = true;
 }
}
function onKeyup ( event ) {
  if ( event.key === controlKey ) {
    isControlPressed = false;
    if ( formElement !== null ) {
      formElement.target = originalTarget;
      formElement = null;
      originalTarget = '';
    }
  }
}
function onMousedown ( event ) {
  if ( event.buttons === middleMouseButton ) {
    if ( event.target?.type === 'submit' ) {
      pressedButton = event.target;
    }
  }
}
function onMouseup ( event ) {
   if ( pressedButton === event.target ) {
     let _origTarget = pressedButton.form.target;
     pressedButton.form.target = '_blank';
     pressedButton.form.dispatchEvent(
       new SubmitEvent("submit", { submitter: pressedButton })
     );
     pressedButton.form.target = _origTarget;
   }
   pressedButton = null;
}
function onBlur ( event ) {
  onKeyup( { key: controlKey } );
}