enhance the query parser

This commit is contained in:
Cade Scroggins 2017-07-16 20:39:57 -07:00
parent 41e7bc5003
commit 7466cd1994

View file

@ -19,7 +19,7 @@
['r', 'Reddit', 'https://www.reddit.com', '/search?q={}'],
['s', 'SoundCloud', 'https://soundcloud.com/discover', '/search?q={}'],
['T', 'TPB', 'https://thepiratebay.org', '/search/{}'],
['w', 'Twitch', 'https://www.twitch.tv/directory/following', false],
['w', 'Twitch', 'https://www.twitch.tv/directory/following', null],
['t', 'Twitter', 'https://twitter.com', '/search?q={}'],
['Y', 'YIFY', 'https://yts.ag/browse-movies/0/1080p/all/7/latest', '/browse-movies/{}/1080p/all/0/rating'],
['y', 'YouTube', 'https://youtube.com/feed/subscriptions', '/results?search_query={}'],
@ -123,8 +123,10 @@
list-style: none;
}
a {
a,
a:focus {
color: inherit;
outline: 0;
}
#clock {
@ -134,6 +136,7 @@
}
#search-form {
transition: background .2s;
background: #111;
z-index: 2;
}
@ -236,7 +239,8 @@
opacity: 0;
}
.command:hover .command-name::after {
.command a:hover .command-name::after,
.command a:focus .command-name::after {
transform: translateX(0);
opacity: 1;
}
@ -249,7 +253,6 @@
box-sizing: border-box;
width: 100%;
height: 100%;
transition: opacity .3s;
visibility: hidden;
opacity: 0;
}
@ -707,12 +710,12 @@
this._urlRegex = /^(?:(http|https)?:\/\/)?(?:[\w-]+\.)+([a-z]|[A-Z]|[0-9]){2,6}/i;
}
generateRedirect(query) {
let redirectUrl;
parse(query) {
const res = { query: query };
if (query.match(this._urlRegex)) {
const hasProtocol = query.match(this._protocolRegex);
redirectUrl = hasProtocol ? query : 'http://' + query;
res.redirect = hasProtocol ? query : 'http://' + query;
} else {
const splitSearch = query.split(this._searchDelimiter);
const splitPath = query.split(this._pathDelimiter);
@ -723,23 +726,24 @@
if (isSearch || isPath) {
if (splitSearch[1] && searchPath) {
redirectUrl = this._prepSearch(url, searchPath, splitSearch);
res.query = this._shiftAndTrim(splitSearch, this._searchDelimiter);
res.redirect = this._prepSearch(url, searchPath, res.query);
} else if (splitPath[1]) {
redirectUrl = this._prepPath(url, splitPath);
res.redirect = this._prepPath(url, splitPath);
} else {
redirectUrl = url;
res.redirect = url;
}
return true;
}
if (key === '*') {
redirectUrl = this._prepSearch(url, searchPath, query);
res.redirect = this._prepSearch(url, searchPath, query);
}
});
}
return redirectUrl;
return res;
}
instantRedirect(query, callback) {
@ -763,10 +767,6 @@
_prepSearch(url, searchPath, query) {
if (!searchPath) return url;
const baseUrl = this._stripUrlPath(url);
query = query.constructor !== Array ? query.trim()
: this._shiftAndTrim(query, this._searchDelimiter);
const urlQuery = encodeURIComponent(query);
searchPath = searchPath.replace('{}', urlQuery);
return baseUrl + searchPath;
@ -897,7 +897,7 @@
const query = this._inputEl.value.trim();
this._clearInput();
this._suggester.success(query);
this._redirect(this._queryParser.generateRedirect(query));
this._redirect(this._queryParser.parse(query).redirect);
}
_submitWithValue(value) {