From 57a33b89cce0fbd771a816c94ff1ad22eaa4794e Mon Sep 17 00:00:00 2001 From: Cade Scroggins Date: Thu, 13 Oct 2016 16:15:21 -0700 Subject: [PATCH] add ability to go directly to a path --- index.html | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/index.html b/index.html index b13a3f3..0fca8d7 100644 --- a/index.html +++ b/index.html @@ -219,6 +219,10 @@ // e.g. to search GitHub for potatoes you'd type "g:potatoes". searchDelimiter: ':', + // the delimiter between the key and a path. + // e.g. type "r/r/unixporn" to go to "reddit.com/r/unixporn". + pathDelimiter: '/', + // set to true to instantly redirect when a key is matched. // you'll have to put a space before any search queries to // prevent unwanted redirects. @@ -329,7 +333,8 @@ return false; } - var qSplit = q.split(config.searchDelimiter); + var qSplitSearch = q.split(config.searchDelimiter); + var qSplitPath = q.split(config.pathDelimiter); var qIsUrl = q.match(config.urlRegex); var qHasProtocol = q.match(config.protocolRegex); var redirect = ''; @@ -337,18 +342,32 @@ if (qIsUrl) redirect = qHasProtocol ? q : 'http://' + q; else redirect = config.defaultSearch + encodeURIComponent(q); + var breakLoop = false; + config.categories.forEach(function(category) { category.commands.forEach(function(command) { - if (qSplit[0] === command.key) { - if (qSplit[1] && command.search) { - qSplit.shift(); - var search = encodeURIComponent(qSplit[0].trim()); + var isSearch = qSplitSearch[0] === command.key; + var isPath = qSplitPath[0] === command.key; + + if (isSearch || isPath) { + if (qSplitSearch[1] && command.search) { + qSplitSearch.shift(); + var search = encodeURIComponent(qSplitSearch.join(config.searchDelimiter).trim()); redirect = command.url + command.search + search; + } else if (qSplitPath[1]) { + qSplitPath.shift(); + var path = qSplitPath.join(config.pathDelimiter).trim(); + redirect = command.url + '/' + path; } else { redirect = command.url; } + + breakLoop = true; + return; } }); + + if (breakLoop) return; }); window.location.href = redirect;