Files
ohayodash/ohayodash/static/js/search.js

82 lines
2.4 KiB
JavaScript
Executable File

var sindex = 0;
var cycle = false;
var sengine = "https://www.google.com/search?q="; // Default search engine
var providers = [];
fetch('providers.json')
.then(response => response.json())
.then(data => storeProviders(data));
function storeProviders(data) {
providers = data['providers'];
}
function start() {
var query = getParameterByName('q');
if (query) search(query.replaceAll("+", "%2B"));
document.getElementById('keywords').focus();
window.setInterval(function () {
updatetime();
}, 200);
}
function handleKeyPress(e) {
var key = e.keyCode || e.which;
var text = document.getElementById("keywords").value.replaceAll("+", "%2B");
if (key == 13) { // Search functions
search(text.trim());
}
if (key == 32) { //Space to go to search
document.getElementById("keywords").focus();
}
sindex = 0;
cycle = false;
}
function search(text) {
var option = text.substr(1, text.indexOf(' ') - 1) || text.substr(1);
var subtext = text.substr(2 + option.length);
if (text[0] === '/') {
providers.every(function (item) {
if ('/' + option == item['prefix']) {
if (text.indexOf(' ') > -1) {
window.location = item['url'] + item['search'] + subtext;
} else {
window.location = item['url'] + subtext;
}
return false;
}
return true;
});
} else if (validURL(text)) {
if (containsProtocol(text))
window.location = text;
else
window.location = "https://" + text;
} else {
window.location = sengine + text;
}
}
// Source: https://stackoverflow.com/questions/5717093/check-if-a-javascript-string-is-a-url
function validURL(str) {
var pattern = new RegExp('^(https?:\\/\\/)?' + // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
'(\\#[-a-z\\d_]*)?$', 'i'); // fragment locator
return !!pattern.test(str);
}
function containsProtocol(str) {
var pattern = new RegExp('^(https?:\\/\\/){1}.*', 'i');
return !!pattern.test(str);
}
String.prototype.replaceAll = function (search, replacement) {
var target = this;
return target.split(search).join(replacement);
};