General :  K-Meleon Web Browser Forum
General discussion about K-Meleon 
Selecting the first form field with a hotkey
Posted by: Peabody
Date: November 21, 2006 12:29AM

Greetings,

In Firefox I have used an extension that allows me to use the keyboard shortcut of "Ctrl-;" to automatically place the text cursor into the first text field box in any web page containing form fields or text areas.

Assigning a hotkey is straightforward in the accel.cfg file, but I don't know how to cause that shortcut to perform the task. Perhaps through a K-M macro? Is this possible in K-M?

Thank you.

Options: ReplyQuote
Re: Selecting the first form field with a hotkey
Posted by: rmn
Date: November 21, 2006 12:35PM

Do you know how to define and use a macro? The following code should work.

open("javascript: (function() { var es = document.getElementsByTagName('input'); var len = es.length; for (var i = 0; i < len; i++) { var e = es; if (e.type == 'text') { e.focus(); break } } })()");

If you're curious, here's the relevant JavaScript code:

var es = document.getElementsByTagName('input')
var len = es.length
for (var i = 0; i < len; i++) {
	var e = es
	if (e.type == 'text') {
		e.focus()
		break
	}
}


Options: ReplyQuote
Re: Selecting the first form field with a hotkey
Posted by: Peabody
Date: November 22, 2006 02:58AM

Quote

Do you know how to define and use a macro? The following code should work.

No, not yet, but I'm getting there!

Okay, in accel.cfg I added:

CTRL ; = macros(First_Text_Field)

This was the key sequence used with the Firefox extension. I thought I'd keep the same hotkey.

In macros.cfg I added the following:

First_Text_Field {
open("javascript: (function() { var es = document.getElementsByTagName('input'); var len = es.length; for (var i = 0; i < len; i++) { var e = es; if (e.type == 'text') { e.focus(); break } } })()");
}


I restarted K-M. Pressing Ctrl-; did nothing. I then enabled JavaScript and reloaded the page. The macro then worked. Thank you, but Bummer.

I do NOT want JavaScript enabled full time. I suspect the Firefox extension worked without JavaScript externally enabled because the extension code was intercepted at the lower code level. Probably another XUL thingie where JavaScript can be executed internally but not externally.

For this handy K-M macro to function, I need to enable JavaScript externally and I don't like that. For safety reasons against malicious people and script kiddies, I have always surfed with JavaScript disabled.

Is there a way to assign a hotkey without externally enabling JavaScript?

Options: ReplyQuote
Re: Selecting the first form field with a hotkey
Posted by: rmn
Date: November 23, 2006 05:04PM

Okay, try this.

$JSdoit = "var es = getElementsByTagName('input'); var len = es.length; for (var i = 0; i < len; i++) { var e = es; if (e.type == 'text') { e.focus(); break } }";
&hndlDoc;


Options: ReplyQuote
Re: Selecting the first form field with a hotkey
Posted by: Peabody
Date: November 26, 2006 01:16AM

Being unfamiliar with JavaScript I lack a fluent understanding about what the code snippet does, but that last snippet works great! Thank you, rmn!

Devs:

Any chance this snippet can be included in the next K-M release as a regular macro? Please consider this a formal request. Thank you.

Options: ReplyQuote
Re: Selecting the first form field with a hotkey
Posted by: rmn
Date: November 26, 2006 09:37AM

Oops, the previous code has a bug (it modifies several global variables). Please use this one instead.

$JSdoit = "(function () { var es = getElementsByTagName('input'); var len = es.length; for (var i = 0; i < len; i++) { var e = es; if (e.type == 'text') { e.focus(); break } } })()"
&hndlDoc;



Edited 1 time(s). Last edit at 11/26/2006 09:42AM by rmn.

Options: ReplyQuote
Re: Selecting the first form field with a hotkey
Posted by: Peabody
Date: November 29, 2006 08:06PM

I am not versed in JavaScript therefore I do not know the functional difference between the latter two snippets of code. However,the last snippet does not work even with external JavaScript enabled. I restored the second to last snippet and the hotkey then properly selects the first text field box without externally enabling JavaScript.

Options: ReplyQuote
Re: Selecting the first form field with a hotkey
Posted by: rmn
Date: November 30, 2006 07:50PM

Strange, it works for me. The second to last code has the potential to break websites' JS code in somewhat unpredictable ways so I suggest you try the last one again.

Options: ReplyQuote
Re: Selecting the first form field with a hotkey
Posted by: Peabody
Date: November 30, 2006 10:06PM

Quote

The second to last code has the potential to break websites' JS code in somewhat unpredictable ways so I suggest you try the last one again.
I don't know what to add. Apparently I have K-M configured in a manner that renders the snippet impotent. The code does not work for me. I don't where our two systems differ.

Options: ReplyQuote
Re: Selecting the first form field with a hotkey
Posted by: rmn
Date: December 01, 2006 08:19AM

Oh well, just use the one that works. You'll only hit the bug if you turn on JS, go to a page that defines variables with the same name, and then run this macro.

Options: ReplyQuote
Re: Selecting the first form field with a hotkey
Posted by: Peabody
Date: December 02, 2006 10:08PM

Quote

Oh well, just use the one that works.
No biggie. smiling smiley

Any chance you could modify this code such that when the cursor pops into the first form field that the entire field is automatically selected? This is useful for search forms when the field already contains text but the user wants to enter new criteria into the field.

If not, then thanks for all your help regardless!

Options: ReplyQuote
Re: Selecting the first form field with a hotkey
Posted by: rmn
Date: December 05, 2006 09:24AM

$JSdoit = "(function () { var es = getElementsByTagName('input'); var len = es.length; for (var i = 0; i < len; i++) { var e = es; if (e.type == 'text') { e.focus(); e.select(); break } } })()";
&hndlDoc;


Options: ReplyQuote
Re: Selecting the first form field with a hotkey
Posted by: Peabody
Date: December 05, 2006 07:45PM

That did the trick. Thank you!

Options: ReplyQuote


K-Meleon forum is powered by Phorum.