From ff44941dc8d7f504d07d51e58955250609df6684 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Fri, 29 May 2015 16:29:13 +0100 Subject: Move search-engine parsing to Utils. This will allow us to use the same search-engine parsing code in the background page and in content scripts. --- lib/utils.coffee | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'lib/utils.coffee') diff --git a/lib/utils.coffee b/lib/utils.coffee index 65e26b7a..cb7b4d5c 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -228,6 +228,42 @@ 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] + url = tokens[1] + description = tokens[2..].join(" ") || "search (#{keyword})" + continue unless Utils.hasFullUrlPrefix url + engines[keyword] = + keyword: keyword + searchUrl: url + description: description + searchUrlPrefix: url.split("%s")[0] + + 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. @@ -325,6 +361,7 @@ class JobRunner root = exports ? window root.Utils = Utils +root.SearchEngines = SearchEngines root.SimpleCache = SimpleCache root.AsyncDataFetcher = AsyncDataFetcher root.JobRunner = JobRunner -- cgit v1.2.3 From 97bfe5f039ae9044fc634448ad3a97b1cdc05792 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Fri, 29 May 2015 17:06:55 +0100 Subject: Verify keyword for custom search-engine activation. For ... map s Vomnibar.activate keyword=g ... we verify that "g" is indeed a custom search-engine keyword before setting it. If it is not, we output a console.log message and launch a vanilla vomnibar. (An alternative would be to bail.) --- lib/utils.coffee | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'lib/utils.coffee') diff --git a/lib/utils.coffee b/lib/utils.coffee index cb7b4d5c..77e2b68d 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -245,14 +245,10 @@ SearchEngines = tokens = line.split /\s+/ continue unless 2 <= tokens.length keyword = tokens[0].split(":")[0] - url = tokens[1] + searchUrl = tokens[1] description = tokens[2..].join(" ") || "search (#{keyword})" - continue unless Utils.hasFullUrlPrefix url - engines[keyword] = - keyword: keyword - searchUrl: url - description: description - searchUrlPrefix: url.split("%s")[0] + continue unless Utils.hasFullUrlPrefix searchUrl + engines[keyword] = { keyword, searchUrl, description } callback engines -- cgit v1.2.3