From caa622ffeca1067080b81a7a74950a60074a9e9b Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Fri, 20 Jul 2018 22:44:09 +0200 Subject: [PATCH] [search] Support searching by URI Adds matching for URIs to the search bar, using the format :/// Searching by URI scheme is also supported: "http://" (returns all bridges with 'http' scheme) "https://" (returns all bridges with 'https' scheme) The following examples are equivalent and will return both of the Facebook bridges (FacebookBridge and FB2Bridge): "https://www.facebook.com/facebook" "https://www.facebook.com/facebook?..." "https://www.facebook.com" "http://www.facebook.com" "https://facebook.com" "http://facebook.com" "facebook.com" "facebook" Notice: When the URI scheme is omitted, the search algorithm falls back to regex matching. Searching for "www.facebook.com" doesn't work, as it is missing the schema and doesn't match via regex! Omitting the 'www.', however, does work. This was a design decision for some bridges specify their URI with and others without 'www.' A search term can still be specified in the browser URL using parameter 'q' => '?q=searchterm'. References #743 --- static/search.js | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/static/search.js b/static/search.js index 3ddd99b..daf3287 100644 --- a/static/search.js +++ b/static/search.js @@ -3,20 +3,53 @@ function search() { var searchTerm = document.getElementById('searchfield').value; var searchableElements = document.getElementsByTagName('section'); - var regexMatch = new RegExp(searchTerm, "i"); + var regexMatch = new RegExp(searchTerm, 'i'); + + // Attempt to create anchor from search term (will default to 'localhost' on failure) + var searchTermUri = document.createElement('a'); + searchTermUri.href = searchTerm; + + if(searchTermUri.hostname == 'localhost') { + searchTermUri = null; + } else { + + // Ignore "www." + if(searchTermUri.hostname.indexOf('www.') === 0) { + searchTermUri.hostname = searchTermUri.hostname.substr(4); + } + + } for(var i = 0; i < searchableElements.length; i++) { var textValue = searchableElements[i].getAttribute('data-ref'); - if(textValue != null) { + var anchors = searchableElements[i].getElementsByTagName('a'); - if(textValue.match(regexMatch) == null && searchableElements[i].style.display != "none") { + if(anchors != null && anchors.length > 0) { - searchableElements[i].style.display = "none"; + var uriValue = anchors[0]; // First anchor is bridge URI - } else if(textValue.match(regexMatch) != null) { + // Ignore "www." + if(uriValue.hostname.indexOf('www.') === 0) { + uriValue.hostname = uriValue.hostname.substr(4); + } - searchableElements[i].style.display = "block"; + } + + if(textValue != null || uriValue != null) { + + if(textValue.match(regexMatch) != null || + uriValue.hostname.match(regexMatch) || + searchTermUri != null && + uriValue.hostname != 'localhost' && ( + uriValue.href.match(regexMatch) != null || + uriValue.hostname == searchTermUri.hostname)) { + + searchableElements[i].style.display = 'block'; + + } else { + + searchableElements[i].style.display = 'none'; }