-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
174 lines (146 loc) · 4.36 KB
/
index.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* Cookie functions */
function setCookie(name, value, exp_days) {
var d = new Date();
d.setTime(d.getTime() + (exp_days*24*60*60*1000));
var expires = "expires=" + d.toGMTString();
document.cookie = name + "=" + value + ";" + expires + ";path=/";
}
function getCookie(name) {
var cname = name + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i < ca.length; i++){
var c = ca[i];
while(c.charAt(0) == ' '){
c = c.substring(1);
}
if(c.indexOf(cname) == 0){
return c.substring(cname.length, c.length);
}
}
return "";
}
/* Engine selection */
const englist = [
"DuckDuckGo",
"Google",
"Ecosia",
"Brave",
"Bing",
"Yahoo"
];
const queryurl = [
"https://duckduckgo.com/?q=",
"https://www.google.com/search?q=",
"https://www.ecosia.org/search?q=",
"https://search.brave.com/search?q=",
"https://www.bing.com/search?q=",
"https://search.yahoo.com/search?p="
];
function setSearchEngine(search)
{
if (englist.includes(search))
{
setCookie("engine", search, 20000);
return true;
}
return false;
}
function setSelectDefault()
{
document.getElementById('settings_engine').value = getCookie("engine");
}
function getSearchEngine()
{
let engine = getCookie("engine");
let baseurl = null;
for(let k = 0; k < englist.length; k++)
{
if(engine == englist[k])
{
baseurl = queryurl[k];
}
}
if(!baseurl)
{
console.log("Invalid default search engine (got " + engine + ")");
engine = "DuckDuckGo";
}
setSearchEngine(engine);
setPlaceholder(engine);
setSelectDefault();
return baseurl;
}
const searchBox = document.getElementById('searchbar')
searchBox.addEventListener('keydown', (e) => {
if (e.code == 'Enter')
{
let textfield = document.getElementById('searchbar').value;
if(textfield)
{
var regex = new RegExp('^(http://|https://)');
let url;
if(regex.test(textfield))
{
url = textfield;
} else {
url = getSearchEngine() + encodeURIComponent(textfield);
}
window.location.href = url;
}
}
})
function choose(val)
{
if(val) return "block";
else return "none";
}
function showSettings()
{
if (typeof showSettings.openc == 'undefined')
showSettings.openc = 0;
showSettings.openc++;
showSettings.openc %= 2;
let showMenu = `
visibility: visible;
transition: all 0.3s ease;
transform: translateX(0px);
`;
let hideMenu = `
visibility: hidden;
transition: all 0.3s ease;
transform:
`;
let vw = window.innerWidth;
if(vw >= 1536)
{
hideMenu += "translateX(-230.4px);"
} else if(vw < 1536 && vw > 992)
{
hideMenu += "translateX(-15vw);"
} else {
hideMenu += "translateX(-148.8px);"
}
let settings_height = document.documentElement.getBoundingClientRect().height;
document.querySelector('.settings_menu').style.cssText = "min-height: " + settings_height + "px";
const settings = document.querySelector('.settings_menu');
showSettings.openc ? (settings.style.cssText = showMenu) : (settings.style.cssText = hideMenu);
document.getElementById("settings_open").setAttribute("style", "display: " + choose(showSettings.openc));
document.getElementById("settings").setAttribute("style", "display: " + choose(!showSettings.openc));
// Hide "Settings applied"
document.getElementById("settings_applied").setAttribute("style", "display: none");
}
function setPlaceholder(eng)
{
let searchbar = document.getElementById("searchbar");
searchbar.placeholder = 'Search with ' + eng;
}
function applySettings()
{
let engine = document.getElementById("settings_engine").value;
if(setSearchEngine(engine))
document.getElementById("settings_applied").setAttribute("style", "display: block");
setPlaceholder(engine);
setSelectDefault();
}
window.onload = getSearchEngine();