A fork of https://github.com/ueokande/vim-vixen
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.1 KiB
44 lines
1.1 KiB
6 years ago
|
const trimStart = (str) => {
|
||
|
// NOTE String.trimStart is available on Firefox 61
|
||
|
return str.replace(/^\s+/, '');
|
||
|
};
|
||
|
|
||
6 years ago
|
const SUPPORTED_PROTOCOLS = ['http:', 'https:', 'ftp:', 'mailto:', 'about:'];
|
||
6 years ago
|
|
||
6 years ago
|
const searchUrl = (keywords, searchSettings) => {
|
||
6 years ago
|
try {
|
||
6 years ago
|
let u = new URL(keywords);
|
||
|
if (SUPPORTED_PROTOCOLS.includes(u.protocol.toLowerCase())) {
|
||
|
return u.href;
|
||
6 years ago
|
}
|
||
6 years ago
|
} catch (e) {
|
||
|
// fallthrough
|
||
|
}
|
||
|
if (keywords.includes('.') && !keywords.includes(' ')) {
|
||
|
return 'http://' + keywords;
|
||
|
}
|
||
|
let template = searchSettings.engines[searchSettings.default];
|
||
|
let query = keywords;
|
||
6 years ago
|
|
||
6 years ago
|
let first = trimStart(keywords).split(' ')[0];
|
||
|
if (Object.keys(searchSettings.engines).includes(first)) {
|
||
|
template = searchSettings.engines[first];
|
||
|
query = trimStart(trimStart(keywords).slice(first.length));
|
||
6 years ago
|
}
|
||
6 years ago
|
return template.replace('{}', encodeURIComponent(query));
|
||
6 years ago
|
};
|
||
|
|
||
6 years ago
|
const normalizeUrl = (url) => {
|
||
|
try {
|
||
|
let u = new URL(url);
|
||
|
if (SUPPORTED_PROTOCOLS.includes(u.protocol.toLowerCase())) {
|
||
|
return u.href;
|
||
|
}
|
||
|
} catch (e) {
|
||
|
// fallthrough
|
||
|
}
|
||
|
return 'http://' + url;
|
||
|
};
|
||
|
|
||
6 years ago
|
export { searchUrl, normalizeUrl };
|