diff options
| author | Stephen Blott | 2015-05-31 09:52:50 +0100 | 
|---|---|---|
| committer | Stephen Blott | 2015-05-31 09:52:50 +0100 | 
| commit | bd07321766056cccd083aa69254abc8146bd5266 (patch) | |
| tree | 59a9300cd4a0806f2f6072db406e00edf80674b4 /lib/utils.coffee | |
| parent | 8b0c610fb68573dbd839133fbc315521db6161f6 (diff) | |
| parent | 5930a6e3510f2bd052771601581aa410728d68e3 (diff) | |
| download | vimium-bd07321766056cccd083aa69254abc8146bd5266.tar.bz2 | |
Merge pull request #1697 from smblott-github/vomnibar-map-with-prepopulated-text
Direct keyboard access to custom-search engines via keyword flag
Diffstat (limited to 'lib/utils.coffee')
| -rw-r--r-- | lib/utils.coffee | 33 | 
1 files changed, 33 insertions, 0 deletions
| diff --git a/lib/utils.coffee b/lib/utils.coffee index 835b0359..93045f32 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -235,6 +235,38 @@ Utils =    # Like Nodejs's nextTick.    nextTick: (func) -> @setTimeout 0, func +# Utility for parsing and using the custom search-engine configuration.  We re-use the previous parse if the +# search-engine configuration is unchanged. +SearchEngines = +  previousSearchEngines: null +  searchEngines: null + +  refresh: (searchEngines) -> +    unless @previousSearchEngines? and searchEngines == @previousSearchEngines +      @previousSearchEngines = searchEngines +      @searchEngines = new AsyncDataFetcher (callback) -> +        engines = {} +        for line in searchEngines.split "\n" +          line = line.trim() +          continue if /^[#"]/.test line +          tokens = line.split /\s+/ +          continue unless 2 <= tokens.length +          keyword = tokens[0].split(":")[0] +          searchUrl = tokens[1] +          description = tokens[2..].join(" ") || "search (#{keyword})" +          continue unless Utils.hasFullUrlPrefix searchUrl +          engines[keyword] = { keyword, searchUrl, description } + +        callback engines + +  # Use the parsed search-engine configuration, possibly asynchronously. +  use: (callback) -> +    @searchEngines.use callback + +  # Both set (refresh) the search-engine configuration and use it at the same time. +  refreshAndUse: (searchEngines, callback) -> +    @refresh searchEngines +    @use callback  # This creates a new function out of an existing function, where the new function takes fewer arguments. This  # allows us to pass around functions instead of functions + a partial list of arguments. @@ -332,6 +364,7 @@ class JobRunner  root = exports ? window  root.Utils = Utils +root.SearchEngines = SearchEngines  root.SimpleCache = SimpleCache  root.AsyncDataFetcher = AsyncDataFetcher  root.JobRunner = JobRunner | 
