Browse Source

[search] Support searching by URI

Adds matching for URIs to the search bar, using the format
<scheme>://<host>/<path>

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
logmanoriginal 4 years ago
parent
commit
caa622ffec
1 changed files with 39 additions and 6 deletions
  1. 39 6
      static/search.js

+ 39 - 6
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(anchors != null && anchors.length > 0) {
+
+			var uriValue = anchors[0]; // First anchor is bridge URI
+
+			// Ignore "www."
+			if(uriValue.hostname.indexOf('www.') === 0) {
+				uriValue.hostname = uriValue.hostname.substr(4);
+			}
+
+		}
+
+		if(textValue != null || uriValue != null) {
 
-			if(textValue.match(regexMatch) == null && searchableElements[i].style.display != "none") {
+			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 = "none";
+				searchableElements[i].style.display = 'block';
 
-			} else if(textValue.match(regexMatch) != null) {
+			} else {
 
-				searchableElements[i].style.display = "block";
+				searchableElements[i].style.display = 'none';
 
 			}