aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephen Blott2015-05-14 10:05:19 +0100
committerStephen Blott2015-05-14 10:05:19 +0100
commitca388d13bfb04c3b72bc57bb99f2821f63c41948 (patch)
tree97b682723114859c4f5952428b71320ba32fedba
parentd9742b3eddd2cfbd655841c5467607dfeb0a203d (diff)
parent2dcd2aa91c7c9912a9fb7a290fa206467359cc33 (diff)
downloadvimium-ca388d13bfb04c3b72bc57bb99f2821f63c41948.tar.bz2
Merge branch 'vomnibar-filter'
-rw-r--r--background_scripts/completion.coffee48
1 files changed, 42 insertions, 6 deletions
diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee
index 349b3b8b..bd72fae5 100644
--- a/background_scripts/completion.coffee
+++ b/background_scripts/completion.coffee
@@ -52,7 +52,7 @@ class Suggestion
<span class="vimiumReset vomnibarTitle">#{@highlightQueryTerms Utils.escapeHtml @title}</span>
</div>
<div class="vimiumReset vomnibarBottomHalf">
- <span class="vimiumReset vomnibarUrl">#{@shortenUrl @highlightQueryTerms Utils.escapeHtml @url}</span>
+ <span class="vimiumReset vomnibarUrl">#{@highlightQueryTerms Utils.escapeHtml @shortenUrl()}</span>
#{relevancyHtml}
</div>
"""
@@ -68,8 +68,6 @@ class Suggestion
a.href = url
a.hostname
- shortenUrl: (url) -> @stripTrailingSlash(url).replace(/^https?:\/\//, "")
-
stripTrailingSlash: (url) ->
url = url.substring(url, url.length - 1) if url[url.length - 1] == "/"
url
@@ -129,6 +127,32 @@ class Suggestion
previous = range
mergedRanges
+ # Simplify a suggestion's URL (by removing those parts which aren't useful for display or comparison).
+ shortenUrl: () ->
+ return @shortUrl if @shortUrl?
+ url = @url
+ for [ filter, replacements ] in @stripPatterns
+ if new RegExp(filter).test url
+ for replace in replacements
+ url = url.replace replace, ""
+ @shortUrl = url
+
+ # Patterns to strip from URLs; of the form [ [ filter, replacements ], [ filter, replacements ], ... ]
+ # - filter is a regexp string; a URL must match this regexp first.
+ # - replacements (itself a list) is a list of regexp objects, each of which is removed from URLs matching
+ # the filter.
+ #
+ # Note. This includes site-specific patterns for very-popular sites with URLs which don't work well in the
+ # vomnibar.
+ #
+ stripPatterns: [
+ # Google search specific replacements; this replaces query parameters which are known to not be helpful.
+ [ "^https?://www\.google\.(com|ca|com\.au|co\.uk|ie)/.*[&?]q="
+ "ei gws_rd url ved usg sa usg sig2".split(/\s+/).map (param) -> new RegExp "\&#{param}=[^&]+" ]
+
+ # General replacements; replaces leading and trailing fluff.
+ [ '.', [ "^https?://", "\\W+$" ].map (re) -> new RegExp re ]
+ ]
class BookmarkCompleter
folderSeparator: "/"
@@ -566,11 +590,23 @@ class MultiCompleter
@filter @mostRecentQuery...
prepareSuggestions: (queryTerms, suggestions) ->
+ # Compute suggestion relevancies and sort.
suggestion.computeRelevancy queryTerms for suggestion in suggestions
suggestions.sort (a, b) -> b.relevancy - a.relevancy
- for suggestion in suggestions[0...@maxResults]
- suggestion.generateHtml()
- suggestion
+
+ # Simplify URLs and remove duplicates (duplicate simplified URLs, that is).
+ count = 0
+ seenUrls = {}
+ suggestions =
+ for suggestion in suggestions
+ url = suggestion.shortenUrl()
+ continue if seenUrls[url]
+ break if ++count == @maxResults
+ seenUrls[url] = suggestion
+
+ # Generate HTML for the remaining suggestions and return them.
+ suggestion.generateHtml() for suggestion in suggestions
+ suggestions
# Utilities which help us compute a relevancy score for a given item.
RankingUtils =