处理双击左键而不干扰中心和右键单击(太多)的一种方法是:
- 跟踪事件触发
<option>
时是否选择了。mousedown
- 确保稍后触发鼠标单击事件时它仍处于选中状态。
- 如果这两个条件都为真,则提交包含表单。
为了大大简化代码,这次使用 jQuery。请参阅 jsFiddle 的实际代码。
一个完整的脚本看起来像这样:
// ==UserScript==
// @name _Activate double-click on select, submits form
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @grant GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change introduced
in GM 1.0. It restores the sandbox.
*/
$("select[name=z_pos_id] option").bind ("mousedown click", handleOptionClicks);
function handleOptionClicks (zEvent) {
_self = handleOptionClicks;
_self.optionWasSelected = _self.optionWasSelected || false;
if (zEvent.type == "mousedown") {
_self.optionWasSelected = zEvent.target.selected;
}
else {//zEvent.type == "click"
if (zEvent.target.selected && _self.optionWasSelected) {
//-- Double-click, so submit form:
$(this).parents ("form").submit ();
}
}
}
更新:这是一个也可以在 Chrome 上运行的版本(需要使用脚本注入)。
这假设目标页面还没有 jQuery:
// ==UserScript==
// @name _Activate double-click on select, submits form
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @grant none
// ==/UserScript==
function GM_main ($) {
$("select[name=z_pos_id] option").bind ("mousedown click", handleOptionClicks);
function handleOptionClicks (zEvent) {
_self = handleOptionClicks;
_self.optionWasSelected = _self.optionWasSelected || false;
if (zEvent.type == "mousedown") {
_self.optionWasSelected = zEvent.target.selected;
}
else {//zEvent.type == "click"
if (zEvent.target.selected && _self.optionWasSelected) {
//-- Double-click, so submit form:
$(this).parents ("form").submit ();
}
}
}
}
add_jQuery (GM_main, "1.7.2");
function add_jQuery (callbackFn, jqVersion) {
jqVersion = jqVersion || "1.7.2";
var D = document;
var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
var scriptNode = D.createElement ('script');
scriptNode.src = 'http://ajax.googleapis.com/ajax/libs/jquery/'
+ jqVersion
+ '/jquery.min.js'
;
scriptNode.addEventListener ("load", function () {
var scriptNode = D.createElement ("script");
scriptNode.textContent =
'var gm_jQuery = jQuery.noConflict (true);\n'
+ '(' + callbackFn.toString () + ')(gm_jQuery);'
;
targ.appendChild (scriptNode);
}, false);
targ.appendChild (scriptNode);
}