From 69ea703f584f65c962fb04fcae7bb6500a11b76a Mon Sep 17 00:00:00 2001 From: Niklas Baumstark Date: Sat, 21 Jan 2012 04:00:12 +0100 Subject: allow custom functions as commands, add example in form of forced web search --- fuzzyMode.js | 3 ++- lib/completion.js | 11 +++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/fuzzyMode.js b/fuzzyMode.js index 8c43cad0..a470bea9 100644 --- a/fuzzyMode.js +++ b/fuzzyMode.js @@ -7,7 +7,8 @@ var fuzzyMode = (function() { new completion.SmartCompleter({ 'wiki ': [ 'Wikipedia (en)', 'http://en.wikipedia.org/wiki/%s' ], 'cc ' : [ 'dict.cc', 'http://www.dict.cc/?s=%s' ], - ';' : [ 'goto', '%s' ] + ';' : [ 'goto', '%s' ], + '?' : [ 'search', function(query) { return utils.createSearchUrl(query) } ], }), new completion.FuzzyHistoryCompleter(1000), new completion.FuzzyBookmarkCompleter(), diff --git a/lib/completion.js b/lib/completion.js index 2b29258f..6e8ea803 100644 --- a/lib/completion.js +++ b/lib/completion.js @@ -246,10 +246,17 @@ var completion = (function() { var term = query.slice(key.length, query.length); var command = commands[key]; var desc = command[0]; - var urlPattern = command[1]; + var pattern = command[1]; + var url; + + if (typeof pattern === 'function') + url = pattern(term); + else + url = pattern.replace(/%s/g, term); + suggestions.push({ render: createConstantFunction('' + desc + ' ' + term), - action: createActionOpenUrl(utils.createFullUrl(urlPattern.replace(/%s/g, term))), + action: createActionOpenUrl(utils.createFullUrl(url)), }); } } -- cgit v1.2.3