我正在寻找一种使用Greasemonkey在下拉列表(which is auto-sized)中点击两次来提交值的方法。
目标HTML:
<form method="post">
<select size="3" name="z_pos_id">
<option value="2463">Option A</option>
<option value="2609">Option B</option>
<option value="3013">Option C</option>
</select>
<input type="submit" value="Button">
</form>
目前我点击选项将其选中,然后点击按钮提交值(按Enter键也可以)。
但是使用Greasemonkey脚本我想单击选项以选择它,然后再次单击所选选项以提交它。 这可能吗?
答案 0 :(得分:1)
处理双重 左 - 点击而不干扰中心和右键点击(太多)的一种方法是:
<option>
事件触发时是否选择mousedown
。为了大大简化代码,这次使用jQuery。 See the code in action at 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);
}