diff options
| author | Phil Crosby | 2014-06-14 05:05:16 -0700 |
|---|---|---|
| committer | Phil Crosby | 2014-06-14 05:05:16 -0700 |
| commit | d674b10c38e1d9d6cf230391a23107ef53b82282 (patch) | |
| tree | 47c50bf7f2467e5a6b3b0564e5eec7a62129c108 /background_scripts | |
| parent | f42690b1578de0b874048579354a17c251f567dd (diff) | |
| parent | a18ad484f2fdc0019c15d94405c915f8bfe6d76f (diff) | |
| download | vimium-d674b10c38e1d9d6cf230391a23107ef53b82282.tar.bz2 | |
Merge pull request #1058 from mijoharas/custom_search_engines
Custom search engines
Diffstat (limited to 'background_scripts')
| -rw-r--r-- | background_scripts/completion.coffee | 22 | ||||
| -rw-r--r-- | background_scripts/main.coffee | 2 | ||||
| -rw-r--r-- | background_scripts/settings.coffee | 21 |
3 files changed, 45 insertions, 0 deletions
diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee index 07f97322..b0ab4b88 100644 --- a/background_scripts/completion.coffee +++ b/background_scripts/completion.coffee @@ -267,6 +267,27 @@ class TabCompleter computeRelevancy: (suggestion) -> RankingUtils.wordRelevancy(suggestion.queryTerms, suggestion.url, suggestion.title) +# A completer which will return your search engines +class SearchEngineCompleter + searchEngines: {} + + filter: (queryTerms, onComplete) -> + searchEngineMatch = this.getSearchEngineMatches(queryTerms[0]) + suggestions = [] + if searchEngineMatch + searchEngineMatch = searchEngineMatch.replace(/%s/g, queryTerms[1..].join(" ")) + suggestion = new Suggestion(queryTerms, "search", searchEngineMatch, queryTerms[0] + ": " + queryTerms[1..].join(" "), @computeRelevancy) + suggestions.push(suggestion) + onComplete(suggestions) + + computeRelevancy: -> 1 + + refresh: -> + this.searchEngines = root.Settings.getSearchEngines() + + getSearchEngineMatches: (queryTerm) -> + this.searchEngines[queryTerm] + # A completer which calls filter() on many completers, aggregates the results, ranks them, and returns the top # 10. Queries from the vomnibar frontend script come through a multi completer. class MultiCompleter @@ -513,6 +534,7 @@ root.MultiCompleter = MultiCompleter root.HistoryCompleter = HistoryCompleter root.DomainCompleter = DomainCompleter root.TabCompleter = TabCompleter +root.SearchEngineCompleter = SearchEngineCompleter root.HistoryCache = HistoryCache root.RankingUtils = RankingUtils root.RegexpCache = RegexpCache diff --git a/background_scripts/main.coffee b/background_scripts/main.coffee index 0511303a..3b7670d4 100644 --- a/background_scripts/main.coffee +++ b/background_scripts/main.coffee @@ -24,9 +24,11 @@ completionSources = history: new HistoryCompleter() domains: new DomainCompleter() tabs: new TabCompleter() + seachEngines: new SearchEngineCompleter() completers = omni: new MultiCompleter([ + completionSources.seachEngines, completionSources.bookmarks, completionSources.history, completionSources.domains]) diff --git a/background_scripts/settings.coffee b/background_scripts/settings.coffee index c26da5a4..175f3262 100644 --- a/background_scripts/settings.coffee +++ b/background_scripts/settings.coffee @@ -32,10 +32,28 @@ root.Settings = Settings = root.Commands.parseCustomKeyMappings value root.refreshCompletionKeysAfterMappingSave() + searchEngines: (value) -> + root.Settings.parseSearchEngines value + # postUpdateHooks convenience wrapper performPostUpdateHook: (key, value) -> @postUpdateHooks[key] value if @postUpdateHooks[key] + # Here we have our functions that parse the search engines + # this is a map that we use to store our search engines for use. + searchEnginesMap: {} + + # this parses the search engines settings and clears the old searchEngines and sets the new one + parseSearchEngines: (searchEnginesText) -> + @searchEnginesMap = {} + # find the split pairs by first splitting by line then splitting on the first `: ` + split_pairs = ( pair.split( /: (.+)/, 2) for pair in searchEnginesText.split( /\n/ ) when pair[0] != "#" ) + @searchEnginesMap[a[0]] = a[1] for a in split_pairs + @searchEnginesMap + getSearchEngines: -> + this.parseSearchEngines(@get("searchEngines") || "") if Object.keys(@searchEnginesMap).length == 0 + @searchEnginesMap + # options.coffee and options.html only handle booleans and strings; therefore all defaults must be booleans # or strings defaults: @@ -78,9 +96,12 @@ root.Settings = Settings = nextPatterns: "next,more,>,\u2192,\xbb,\u226b,>>" # default/fall back search engine searchUrl: "http://www.google.com/search?q=" + # put in an example search engine + searchEngines: "w: http://www.wikipedia.org/w/index.php?title=Special:Search&search=%s" settingsVersion: Utils.getCurrentVersion() + # We use settingsVersion to coordinate any necessary schema changes. if Utils.compareVersions("1.42", Settings.get("settingsVersion")) != -1 Settings.set("scrollStepSize", parseFloat Settings.get("scrollStepSize")) |
