-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.js
94 lines (80 loc) · 2.87 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
var stylishHTML = function (conf) {
$("*").css("background", conf.background);
$("*").css("color", conf.foreground);
$("#logo").html(conf.greeting_text);
$("#cheat li span:first-child").css("color", conf.background);
if (tinycolor(conf.foreground).isLight()) {
$("#cheat li span:first-child").css("background","rgba(200,200,200, 0.8)");
$("#cheat li span:last-child ").css("background","rgba(0,0,0, 0.4)");
} else {
$("#cheat li span:first-child").css("background","rgba(0,0,0, 0.4)");
$("#cheat li span:last-child ").css("background","rgba(200,200,200, 0.8)");
}
};
$(function () {
var conf = {};
var notfavs = new RegExp("^:[u|s] (.*)$");
var search = new RegExp("^:s (.*)$");
var site = new RegExp("^:u (.*)$");
var input = $("#box").val();
$.getJSON("web.json", function (object) {
$.each(object.favourites, function (key, val) {
$("#cheat ul").append("<li><a href='" + val.url + "' target='_blank'><span>" + val.key + "</span><span>" + val.url + "</span></a></li>");
stylishHTML(object);
});
$("ul li").sort(function (a, b) {
return ($(a).width() < $(b).width()) ? -1 : ($(a).width() > $(b).width()) ? 1 : 0;
}).appendTo("ul");
conf = object;
});
$(document).keydown(function (e) {
if (e.shiftKey && e.keyCode === 59) {
$("#box").focus();
}
if (e.keyCode === 27) {
$("#box").blur();
$("#box").val("");
$("#cheatp").slideUp();
$("#holder").html("type :<key> and hit enter");
$("#cheat ul li").each(function () {
$(this).removeClass("dis");
});
}
});
$("#box").keyup(function (event) {
input = $(this).val();
if (input.length == 0 || notfavs.test(input)) {
$("#cheatp").slideUp();
} else {
$("#cheatp").slideDown();
}
$("#cheat ul li").each(function () {
$(this).addClass("dis");
if ($(this).html().indexOf(input) > 0) {
$(this).removeClass("dis");
}
});
});
$("form").on("submit", function (e) {
e.preventDefault();
e.stopPropagation();
if (search.test(input)) {
link = conf.search_engine + encodeURIComponent(input.replace(/^:s /g, ""));
} else if (site.test(input)) {
link = input.replace(/^:u /g, "");
} else {
$.each(conf.favourites, function () {
if (this.key === input) {
link = this.url;
}
});
}
if (!~link.indexOf("http")) {
link = "http://" + link;
}
window.open(link, "_blank");
$("#box").blur();
$("#box").val("");
$("#cheatp").slideUp();
});
});