diff options
| author | Stephen Blott | 2016-12-10 10:51:57 +0000 | 
|---|---|---|
| committer | GitHub | 2016-12-10 10:51:57 +0000 | 
| commit | bd06c7ccfbfcf36e6d15fba72b7af90254b05da2 (patch) | |
| tree | 0890bf5550df77e7739624c8e9a6ae619010b8a5 /background_scripts | |
| parent | 856f0ff8b35a66ecfa8a77ae41f4b761c1b1951c (diff) | |
| parent | 24a6217f26911ba4e934f4a7bff8bcd30c227163 (diff) | |
| download | vimium-bd06c7ccfbfcf36e6d15fba72b7af90254b05da2.tar.bz2 | |
Merge pull request #2326 from smblott-github/move-search-engines-to-bg-utils
Move SearchEngines to bg-utils.coffee.
Diffstat (limited to 'background_scripts')
| -rw-r--r-- | background_scripts/bg_utils.coffee | 31 | 
1 files changed, 31 insertions, 0 deletions
| diff --git a/background_scripts/bg_utils.coffee b/background_scripts/bg_utils.coffee index 26033476..b8e618ff 100644 --- a/background_scripts/bg_utils.coffee +++ b/background_scripts/bg_utils.coffee @@ -85,4 +85,35 @@ BgUtils =        continue if line[0] in '#"'        line +# 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 BgUtils.parseLines searchEngines +          tokens = line.split /\s+/ +          if 2 <= tokens.length +            keyword = tokens[0].split(":")[0] +            searchUrl = tokens[1] +            description = tokens[2..].join(" ") || "search (#{keyword})" +            engines[keyword] = {keyword, searchUrl, description} if Utils.hasFullUrlPrefix searchUrl + +        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 + +root.SearchEngines = SearchEngines  root.BgUtils = BgUtils | 
