From 302e86c35f23ed9385ba4532b4769f6c283a7349 Mon Sep 17 00:00:00 2001 From: David Kettler Date: Mon, 12 Sep 2011 23:11:10 +0930 Subject: [PATCH] webjump: simple POST webjumps A string webjump can now provide post_data, with replacement of '%s' elements with the webjump arguments. This allows simple definition of webjumps that perform an http POST in the following form: define_webjump("example", "http://example.com/", $post_data=[['search', '%s']]); Note that only a literal '%s' is replaced, rather than that sequence anywhere in a string; it's not clear that the latter is useful in post_data. --- modules/webjump.js | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/modules/webjump.js b/modules/webjump.js index 7174f5f..67883c8 100644 --- a/modules/webjump.js +++ b/modules/webjump.js @@ -11,11 +11,13 @@ in_module(null); var webjumps = {}; -define_keywords("$completer", "$description", "$argument", "$alternative"); +define_keywords("$completer", "$description", "$argument", "$alternative", + "$post_data"); function define_webjump (key, handler) { keywords(arguments); var argument = arguments.$argument; let alternative = arguments.$alternative; + var post_data = arguments.$post_data; // handler may be a function or a string. An alternative url may // be passed using the $alternative keyword; it is used in place @@ -26,6 +28,11 @@ function define_webjump (key, handler) { // A webjump can thus function both as a way to invoke a search // and as a bookmark. // + // When the handler is a string and post_data is given the webjump + // will result in a POST. A '%s' element in post_data will be + // replaced by the webjump argument and the alternative is the + // same as the handler (but as a GET). + // // The argument property may be false (no arguments will be // accepted for the webjump), true (arguments are required for the // webjump) or 'optional' (arguments are accepted but not @@ -39,7 +46,9 @@ function define_webjump (key, handler) { if (argument == null && alternative == null) argument = true; } else if (typeof(handler) == "string") { - if (handler.indexOf('%s') == -1) + if (post_data && alternative == null) + alternative = handler; + else if (handler.indexOf('%s') == -1) argument = false; else if (alternative == null) alternative = url_path_trim(handler); @@ -47,7 +56,7 @@ function define_webjump (key, handler) { if (alternative && argument == null) argument = 'optional'; - function make_handler (template) { + function make_string_handler (template) { var b = template.indexOf('%s'); return function (arg) { var a = b + 2; @@ -58,8 +67,26 @@ function define_webjump (key, handler) { }; } - if (typeof(handler) == "string") - handler = make_handler(handler); + function make_post_handler (uri, post_data) { + return function (arg) { + return load_spec({ + uri: uri, + post_data: make_post_data(post_data.map(function (pair) { + if (pair[1] == '%s') + return [pair[0], arg]; + else + return pair; + })) + }); + }; + } + + if (typeof(handler) == "string") { + if (post_data) + handler = make_post_handler(handler, post_data); + else + handler = make_string_handler(handler); + } webjumps[key] = { key: key, handler: handler, -- 2.11.4.GIT