-
Notifications
You must be signed in to change notification settings - Fork 2
/
search.js
64 lines (62 loc) · 1.53 KB
/
search.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
String.prototype.replaceChars = function(character, replacement) {
var str = this;
var a;
var b;
for (var i = 0; i < str.length; i++) {
if (str.charAt(i) == character) {
a = str.substr(0, i) + replacement;
b = str.substr(i + 1);
str = a + b;
}
}
return str;
}
function search(query) {
switch (query.substr(0, 2)) {
// reddit
case "-r":
query = query.substr(3);
window.location =
"https://www.reddit.com/r/" +
query.replaceChars(" ", "");
break;
// youtube
case "-y":
query = query.substr(3);
window.location =
"https://www.youtube.com/results?search_query=" +
query.replaceChars(" ", "+");
break;
// wikipedia
case "-w":
query = query.substr(3);
window.location =
"https://en.wikipedia.org/w/index.php?search=" +
query.replaceChars(" ", "%20");
break;
// wikipedia
case "-a":
query = query.substr(3);
window.location =
"https://arxiv.org/search/?searchtype=all&order=-announced_date_first&size=50&query=" +
query.replaceChars(" ", "%20");
break;
// default
default:
window.location="https://www.google.com/search?q=" +
query.replaceChars("", "+");
}
}
window.onload = function() {
// search
var searchinput = document.getElementById("searchbox");
// tab
if (!!searchinput) {
searchinput.addEventListener("keypress", function(a) {
if (a.keyCode == 13) {
var query = this.value;
search(query);
}
});
}
}