Utility Functions for Webjumps in Conkeror

Long-time readers probably know that Conkeror is my web-browser of choice. You can customize and create functionality by writing files of JavaScript which Conkeror loads and executes. One example is Conkeror’s concept of ‘webjumps’, short-cuts to websites. Webjumps are different from plain bookmarks because they can be dynamic. For example, if I browse to jdk string buffer then I end up on this page.

Today I want to share a simply set of utility functions I use for creating such webjumps.

Whitespace—The Heart of the Matter

URLs cannot have whitespace, and sites deal with this in different ways. Some replace whitespace with an escaped space character, some replace it with dashes, or underscores, and so on. To create webjumps for these sites, particularly like the example above where I’m searching for something with whitespace in it (i.e. “string buffer”), we want the webjump to convert any whitespace we type into the format expected by the site we’re visiting.

We could perform this conversion in every webjump definition by using a regular expression. But instead, to simplify the matter, we’re going to create a function which creates another function that will perform the conversion. It looks like this:

/**
 * Takes a character and returns a function which will accept a string
 * and replace all of the whitespace in that string with that
 * character.  This is useful for building utility functions for
 * creating webjumps that need slight modifications to the search
 * queries.
 */
function create_whitespace_converter(replacement) {
    return function (input) {
        input.replace(/\s+/g, replacement);
        return input;
    };
}

Now we can create a series of functions which convert whitespace to different characters.

let convert_whitespace_to_dashes = create_whitespace_converter("-");
let convert_whitespace_to_spaces = create_whitespace_converter(" ");
let convert_whitespace_to_pluses = create_whitespace_converter("+");
let convert_whitespace_to_underscores = create_whitespace_converter("_");

And now we can use those functions within the definition of webjumps. Here’s a few examples from my own Conkeror configuration:

define_webjump("jdk",
               function (term) {
                 return "https://search.oracle.com/search/search?start=1&search_p_main_operator=all&q="
                      + convert_whitespace_to_underscores(term);
               });

define_webjump("uesp",
              function (term) {
                  return "http://uesp.net/w/index.php?title=Special%3ASearch&search="
                      + convert_whitespace_to_pluses(term)
                      + "&button=";
              });

define_webjump("lastfm",
               function (term) {
                   return "http://www.last.fm/music/" + convert_whitespace_to_dashes(term);
               });

Self-explanatory I hope, but if you have any questions let me know in the comments below.

Advertisements

Add Your Thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s