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.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==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 } );
}