Submit to Tab on Ctrl + Click

Sets form's target to `_blank` when submitted via Ctrl + Left Mouse Button or Ctrl + Enter.

Versione datata 09/02/2016. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name        Submit to Tab on Ctrl + Click
// @description Sets form's target to `_blank` when submitted via Ctrl + Left Mouse Button or Ctrl + Enter.
// @namespace   http://eldar.cz/myf/
// @license     MIT
// @version     1.0.2
// @grant       none
// ==/UserScript==
;(function(){
"use strict";

var FORM_EL;
var FORM_ORIG_TGT; 
var RESTORE_PENDING;

function mousedown(e) {
	if(e.target.form && e.ctrlKey && e.buttons === 1) {
		FORM_EL = e.target.form;
	 	FORM_ORIG_TGT = FORM_EL.target;
		FORM_EL.target = '_blank';
	}
}
function keypress(e) {
	if(e.target.form && e.ctrlKey && e.keyCode === 13) {
		FORM_EL = e.target.form;
	 	FORM_ORIG_TGT = FORM_EL.target;
		FORM_EL.target = '_blank';
		setTimeout(restoreform, 200);
	}
} 
// Cleanup must be delayed because click-submit must finish like normally.
// Artificial .submit() or .click() of dirty target="_blank"ed form could trigger popup blocker.
// That's why listening to middle click is quite futile.
function restoreform() {
	if(FORM_ORIG_TGT) {
		FORM_EL.target = FORM_ORIG_TGT;
	} else {
		FORM_EL.removeAttribute('target');
	}
	FORM_EL = null;
	FORM_ORIG_TGT = null; 
	RESTORE_PENDING = false;
}
// There is no other way to do somehing "after submit" than via timeout, is there?
function mouseup(e) {
	if(FORM_EL && !RESTORE_PENDING) {
		RESTORE_PENDING = true;
		setTimeout(restoreform, 200);
	}
}

document.body.addEventListener('mousedown', mousedown, true);
document.body.addEventListener('mouseup', mouseup, true);
document.body.addEventListener('keypress', keypress, true);

})();