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("foo", "http://foo.com", $post_data=[['search', '%s']]);
In particular all the examples at http://conkeror.org/Webjumps that
use make_post_data are simplified with this form.
Note that currently 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. Implementing this would be a little different to
the usual string case, because the post_data will be URL encoded.
---
Previously: http://thread.gmane.org/gmane.comp.mozilla.conkeror/2493
---
modules/webjump.js | 36 +++++++++++++++++++++++++++++++-----
1 files changed, 31 insertions(+), 5 deletions(-)
diff --git a/modules/webjump.js b/modules/webjump.js
index 7174f5f..0c6638d 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;
+ let 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,25 @@ 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,
--
1.7.5.4
_______________________________________________
Conkeror mailing list
[email protected]
https://www.mozdev.org/mailman/listinfo/conkeror