Anyterm Forum

Anyterm: A Terminal Anywhere

You are not logged in.

#1 2009-03-23 20:15:34

moman
Member

Parameterizing rows, column, and general param in url

Howdy, couldn't find an anyterm mailing list so I hope this is a good place to share some changes I made.

We're looking at using / integrating anyterm into our project, to do so we need to be able to parameterize the rows / columns/ and general purpose param passed to create_term, which are currently hard coded in anyterm.html. Obviously defaults are nice, but it is also useful to be able to specify any of these parameters via the current query url, eg http://server:port/anyterm.html?param=foo&rows=30&columns=100

Attached is a patch that does just that, and I'm hoping it can be accepted upstream as I can see it as useful functionality others might want. Thanks.


Index: browser/anyterm.html
===================================================================
--- browser/anyterm.html    (revision 5300)
+++ browser/anyterm.html    (working copy)
@@ -28,6 +28,18 @@
</script>

<script type="text/javascript">
+  var rows = get_url_param("rows");
+  var columns = get_url_param("columns");
+  var general_param = get_url_param("param");
+
+  if(rows == ""){
+    rows = 25;
+  }

+  if(columns == ""){
+    columns = 80;
+  }
+
   // To create the terminal, just call create_term.  The paramters are:
   //  - The id of a <div> element that will become the terminal.
   //  - The title.  %h and %v expand to the hostname and Anyterm version.
@@ -35,7 +47,7 @@
   //  - An optional parameter which is substituted for %p in the command string.
   //  - An optional character set.
   //  - An option number of lines of scrollback (default 0).
-  window.onload=function() {create_term("term","%h (Anyterm%v)",25,80,"","",50);};
+  window.onload=function() {create_term("term","%h (Anyterm%v)",rows,columns,general_param,"",50);};

   // When the user closes the terminal, by default they'll see a blank page.
   // Generally you'll want to be more friendly than that.  If you set the
Index: browser/anyterm.js
===================================================================
--- browser/anyterm.js  (revision 5300)
+++ browser/anyterm.js  (working copy)
@@ -787,3 +787,14 @@
   sel.appendChild(opt);
}

+function get_url_param( name )
+{
+  var regexS = "[\\?&]"+name+"=([^&#]*)";
+  var regex = new RegExp( regexS );
+  var results = regex.exec( window.location.href );
+  if( results == null )
+    return "";
+  else
+    return results[1];
+}

Last edited by moman (2009-04-02 20:01:02)

Offline

#2 2009-03-24 10:29:48

phil
Administrator

Re: Parameterizing rows, column, and general param in url

Yes, that looks sensible.

Offline

#3 2009-03-25 08:41:04

cantara
Member

Re: Parameterizing rows, column, and general param in url

I'd like to second this request...  I implemented pretty much the exact same patch, it'd be nice if it came out of the box.

Thanks,

Raul

Offline

#4 2009-03-26 18:29:29

phil
Administrator

Re: Parameterizing rows, column, and general param in url

I'm trying to see what's going on here.  I've spent a few minutes staring at this:

Code:

name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");

I think that's going to replace [ with \[ and ] with \].  Right?  OK, so why?  Surely name should be an identifier like "rows" or something.  If for some reason this is needed, can't it be as simple as name.replace("[","\\[").replace("]","\\]") ?

Offline

#5 2009-04-02 20:02:16

moman
Member

Re: Parameterizing rows, column, and general param in url

Hey sorry for the delay, got pulled  onto something else last week, and have been busy since.

I remove that regex line from the original post, was a relic from something else I was working on a while back where we needed it.

Last edited by moman (2009-04-02 21:10:54)

Offline

#6 2009-04-02 23:50:20

phil
Administrator

Re: Parameterizing rows, column, and general param in url

OK, I'll try look at this properly soon.  Pester me if I seem to have forgotten...

Offline

#7 2009-04-06 20:29:29

phil
Administrator

Re: Parameterizing rows, column, and general param in url

Hi,

Here's something based on your proposal.  I've added a default parameter to get_url_param() and fiddled with it to make it look like I wrote it (!), and in anyterm.html I've put some examples of how to use it but commented-out.  This feels safer for users who upgrade.  I could perhaps enable it by default in the future.  This is not tested.  Can you see any problems?

Code:

Index: anyterm.html
===================================================================
--- anyterm.html    (revision 4890)
+++ anyterm.html    (working copy)
@@ -35,8 +35,33 @@
   //  - An optional parameter which is substituted for %p in the command string.
   //  - An optional character set.
   //  - An option number of lines of scrollback (default 0).
+
+  // So the following creates an 80x25 terminal with 50 lines of scrollback:
   window.onload=function() {create_term("term","%h (Anyterm%v)",25,80,"","",50);};
 
+  // You might want to be able to control these settings, and you can write any
+  // JavaScript code you want here to do so.  A common requirement is to be able
+  // to control the terminal dimensions from the page's URL.  So you might have
+  // another HTML page with links like this:
+  // <a href="anyterm.html?rows=25&cols=80">Small terminal</a>
+  // <a href="anyterm.html?rows=60&cols=180">Large terminal</a>
+  // To make that work you need to extract those parameters from the page's URL
+  // and pass them to your call to create_term().  There's a function in
+  // anyterm.js to make this easier, as follows:
+  // var rows = get_url_param("rows",25);
+  // var cols = get_url_param("cols",80);
+  // window.onload=function() {create_term("term","Terminal",rows,cols,"","",0);};
+  // The second parameter to get_url_param is a default.
+  //
+  // Here's another example, passing the name of the host to connect to from a
+  // URL paramter to the %p parameter:
+  // <a href="anyterm.html?host=foo">Connect to foo</a>
+  // var host = get_url_param("host","default_host");
+  // window.onload=function() {create_term("term",host,25,80,host,"",0);};
+  // Use this with a command like:
+  //    anytermd -c 'ssh %p'
+  // You probably only want to do that with trusted users and HTTP AUTH, of course.
+
   // When the user closes the terminal, by default they'll see a blank page.
   // Generally you'll want to be more friendly than that.  If you set the
   // variable on_close_goto_url to a URL, they'll be sent to that page after
Index: anyterm.js
===================================================================
--- anyterm.js    (revision 4890)
+++ anyterm.js    (working copy)
@@ -787,3 +787,17 @@
   sel.appendChild(opt);
 }
 
+
+// This isn't used from within this file, but it can be called from the user-supplied
+// script in anyterm.html if necessary:
+
+function get_url_param(name, def) {
+  var regex = new RegExp("[\\?&]"+name+"=([^&#]*)");
+  var matches = regex.exec(window.location.href);
+  if (matches==null) {
+    return def;
+  } else {
+    return matches[1];
+  }
+}
+

Offline

#8 2009-04-07 21:58:10

moman
Member

Re: Parameterizing rows, column, and general param in url

Those comments in the html would be returned to the browser correct? Don't think we'd want to include it there if this is more optimally configured by the server admin.

In any case isn't this functionality already able to be leveraged, albeit with a bit more difficulty, by default? Anyone could checkout the anyterm html / js / css and serve it locally, configuring it to connect to the anyterm server, accepting their own params to create_term. This patch would make this process a lot simpler, and wouldn't break existing configurations / decrease security anyhow.

Perhaps if we wanted to leave this optional, we could incorporate a config flag or command line param to try and gather the parameters from the url after they have been initialized from default values. If disabled, as we could have it by default, but easily toggled, the admin can control whether anyterm permits url parameters or not.

Offline

#9 2009-04-07 22:10:06

phil
Administrator

Re: Parameterizing rows, column, and general param in url

I suppose the issue here is that these files are compiled in to the executable and you want to ship a single executable while still allowing customisation.  In this case I think that the best strategy is to have Apache serve these files, rather than compiling them in.

I'm cautious about enabling this by default mainly because I don't really have time to test it properly: it is almost trivial for me to "test" some comments...

Offline

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson