[Greasemonkey] Patterns

Edward Lee edilee at gmail.com
Thu Apr 14 19:17:36 EDT 2005


On 4/14/05, Prakash Kailasa <pk-moz at kailasa.net> wrote:
> On Thu, Apr 14, 2005 at 11:43:44AM -0700, Julien Couvreur wrote:
> > Couldn't agree more. The javascript shell bookmarklet is awesome. My
> > only gripe is that is only allows one line of input, which makes it
> > difficult to copy and paste chunks of codes (objects, functions,...).
> 
> I haven't had any trouble with pasting multiple lines of code.
> 
> The problem is it displays only the last line of code upon paste. But,
> once you hit ENTER, you should see all of the pasted code.

I made some modifications to the javascript shell to allow multiline
with a textarea instead of an input box. (I'm sending this to Jesse as
well.)

http://ed.agadak.net/jsshell.php

Basically, I removed the form and input and replaced it with a
textarea. Perhaps the size of the textarea should change because right
now I just have it as textarea with no specified height.
<textarea id=\"input\" class=\"input\"></textarea>

Also, when init is called, it adds an keydown event listener to the
textarea. This allows you to go back in history when you hit ctrl-up
or press up when the cursor is at the top of the input; go forward in
history when you hit ctrl-down or press down when the cursor is at the
end; execute the input on enter (multiline expressions are handled
like normal javascript multilines meaning it's as if a semicolon was
there); add a newline/replacing selected text on ctrl-enter.

The code that I added for the link above doesn't have the comments
that are below, and other things can be cleaned up as well such as the
inputkey function that checks to do tab completion or history stuff.

_in.addEventListener(\"keydown\", function(e) {
  if (e.ctrlKey && e.keyCode == 13) {
    // add a new line with ctrl-enter
    with(e.target) {
      var tmp = [selectionStart, selectionEnd];
      value = value.substring(0, tmp[0]) + \"\\n\" +
value.substring(tmp[1], textLength);
      setSelectionRange(tmp[0] + 1, tmp[0] + 1); // set cursor after the newline
    }
  } else if (e.keyCode == 13) {
    // execute the input on enter
    try { go(); } catch(er) { alert(er); };
    setTimeout(function() { e.target.value = \"\"; }, 0); // can't
preventDefault on input, so clear it later
  } else if (e.keyCode == 38 && (e.ctrlKey || e.target.selectionStart == 0)) {
    // go up in history
    hist(e.keyCode);
  } else if (e.keyCode == 40 && (e.ctrlKey || e.target.selectionEnd ==
e.target.textLength)) {
    // go down in history
    hist(e.keyCode);
  } else {
    // handle tab completion.. inputkey function probably could be
absorbed into this
    inputkey(e.keyCode);
  }
 }, false);


-- 
Ed


More information about the Greasemonkey mailing list