excmd: x-www-form-urlencode query strings

Fixes #83.
This commit is contained in:
Colin Caine 2017-11-20 01:11:38 +00:00
parent b2a35c090d
commit ba99fdaf80

View file

@ -84,21 +84,53 @@ function hasScheme(uri: string) {
return uri.match(/^([\w-]+):/) return uri.match(/^([\w-]+):/)
} }
/** We use this over encodeURIComponent so that '+'s in non queries are not encoded. */
/** @hidden */
function searchURL(provider: string, query: string) {
if (SEARCH_URLS.has(provider)) {
const url = new URL(SEARCH_URLS.get(provider) + query)
// URL constructor doesn't convert +s because they're valid literals in
// the standard it adheres to. But they are special characters in
// x-www-form-urlencoded and e.g. google excepts query parameters in
// that format.
url.search = url.search.replace(/\+/g, '%2B')
return url
} else {
throw new TypeError(`Unknown provider: '${provider}'`)
}
}
/** If maybeURI doesn't have a schema, affix http:// */ /** If maybeURI doesn't have a schema, affix http:// */
/** @hidden */ /** @hidden */
function forceURI(maybeURI: string) { function forceURI(maybeURI: string): string {
if (hasScheme(maybeURI)) { try {
return maybeURI return new URL(maybeURI).href
} catch (e) {
if (e.name !== 'TypeError') throw e
} }
let urlarr = maybeURI.split(" ") // Else if search keyword:
if (SEARCH_URLS.get(urlarr[0]) != null){ try {
return SEARCH_URLS.get(urlarr[0]) + urlarr.slice(1,urlarr.length).join(" ") const args = maybeURI.split(' ')
} else if (urlarr[0].includes('.')) { return searchURL(args[0], args.slice(1).join(' ')).href
return "http://" + maybeURI } catch (e) {
} else { console.log(e)
return SEARCH_URLS.get("google") + maybeURI if (e.name !== 'TypeError') throw e
} }
// Else if it's a domain or something
try {
const url = new URL('http://' + maybeURI)
// Ignore unlikely domains
if (url.hostname.includes('.') || url.port || url.password) {
return url.href
}
} catch (e) {
if (e.name !== 'TypeError') throw e
}
// Else search google
return searchURL('google', maybeURI).href
} }
/** @hidden */ /** @hidden */