Submit to Tab on Ctrl + Click

Sets form's target to `_blank` when submitted via ctrl + left mouse button.

目前為 2015-05-20 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Submit to Tab on Ctrl + Click
// @description Sets form's target to `_blank` when submitted via ctrl + left mouse button.
// @namespace   http://eldar.cz/myf/
// @license     MIT
// @version     1.0.1
// @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';
	}
} 
// 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);

})();