-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain2.js
219 lines (194 loc) · 4.93 KB
/
main2.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// To some extent, a rewrite
let brings = {}
let bringbox = document.getElementById("bring")
LOG = x => console.debug(x)
/* Storage stuff */
let data = {
brings: {},
defaultCmd: false,
startScripts: [],
}
if ("bn-data" in localStorage) {
data = JSON.parse(localStorage.getItem("bn-data"))
} else {
save()
}
function process(obj) {
let res = {};
for (key in obj) {
res[key] = new Function("arg", obj[key])
}
return res
}
// merge builtins and customs
brings = { ...brings, ...process(data.brings) }
function save() {
localStorage.setItem("bn-data", JSON.stringify(data))
}
// url stuff
if (window.location.href.includes('?')) {
bringbox.value = decodeURIComponent(location.search.slice(1))
}
if (window.location.href.includes('?run=')) {
bringbox.value = decodeURIComponent(location.search.slice(5).replace(/\+/g, "%20"))
setTimeout(e => run(decodeURIComponent(location.search.slice(5).replace(/\+/g, "%20"))), 200)
}
// log
function output(text) {
var m = document.createElement("message")
m.innerHTML = text
m.onclick = (event) => {
event.target.remove()
}
document.getElementById('messages').appendChild(m)
}
// keygrabbing
window.onkeydown = (ev) => {
if (ev.key == "Enter") {
document.getElementById("enter").click()
} else if (ev.key != "Tab") bringbox.focus()
}
// the big command
function run(c) {
if (!c) return // blank
if (c.startsWith('https://') || c.startsWith('//')) {
brings.go(c) // url
return
}
let [cm, ...arg] = c.split(' ')
arg = arg.join(" "); cm = cm.toLowerCase()
if (cm in brings) {
brings[cm](arg)
} else if (data.defaultCmd) {
brings[data.defaultCmd](c)
} else {
output(`No command '${cm}' found.`)
}
}
// theme color changing
function setTheme(color) {
themecolorstyle.innerHTML = `:root{--theme-color:${color}}`
}
function manageColor() {
let c = bringbox.value
const col = { normal: '#00ff69', notpresent: '#00ff6988' }
if (!c) setTheme(col.normal)
else if (c.split(' ')[0].toLowerCase() in brings) {
setTheme(col.normal)
} else {
setTheme(col.notpresent)
}
}
bringbox.addEventListener('input', manageColor)
// for copypastability/backwards compatibility only
function add(name, fn) {
if (name in brings) return
brings[name] = fn
}
function addSE() {
let cmdname = document.getElementById('cmdname').value
let cmdurl = document.getElementById('cmdurl').value
data.brings[cmdname] = `location.replace(atob('${btoa(cmdurl)}').replace(/%s/g,arg))`
brings = { ...brings, ...process(data.brings) }
save()
}
//// begin commands ////
add("add-se", () => {
let popup = document.getElementById('new-popup')
popup.showModal();
setTimeout(() => {
if (!popup.open) {
output("Please use the '>' button for this command")
}
}, 100)
})
add('manage', () => {
location.href = './manage'
})
add('set-default', arg => {
data.defaultCmd = arg.split` `[0]
save()
output("Done")
})
function se(url, query) {//Search engine
window.location = url.replace(/%s/g, encodeURIComponent(query))
}
// horrible disgusting function
function popup(title, text) {
var win = document.createElement("window")
win.innerHTML = title.replace(/</g, "<")
var cl = document.createElement("i")
cl.classList.add("fa")
cl.classList.add("fa-times")
cl.classList.add("win-close")
cl.style.float = "right"
cl.style.display = "block"
cl.onclick = (event) => {
event.target.parentNode.remove()
}
var code = document.createElement("div")
code.innerHTML = text
document.getElementById("windows").appendChild(win)
win.appendChild(cl)
win.appendChild(code)
}
add("clog", (l) => {
console.log(l)
})
add("d", (term) => {
se("https://duckduckgo.com/?q=%s", term)
})
add("g", (term) => {
se("https://google.com/search?q=%s", term)
})
add("go", (url) => {
window.location.replace(/^https?:\/\/.+$/.test(url) ? url : "https://" + url)
})
add("show", (text) => {
output(text.replace(/</g, "<"))
})
add("save", (text) => {
if (text == "") {
output(localStorage.getItem("save")?.replace(/</g, "<"))
} else {
localStorage.setItem("save", text)
output("saved")
}
})
add("yt", (term) => {
se("https://youtube.com/results?search_query=%s", term)
})
add("ama", (term) => {
se("https://www.amazon.com/s?k=%s", term)
})
add("w", (term) => {
se("https://en.wikipedia.org/w/index.php?search=%s&title=Special%3ASearch&go=Go&ns0=1", term)
})
add("how", (term) => {
se("https://www.wikihow.com/wikiHowTo?search=%s", term)
})
add("gh", (term) => {
se("https://github.com/search?q=%s", term)
})
add("popup", (text) => {
popup("custom window", text.replace(/</g, "<"))
})
add("help", () => {
location = "/brings"
})
add("date", () => {
output(new Date().toDateString())
})
add("time", () => {
output(new Date().toTimeString())
})
add("new", (a) => {
location = `http://${a}.new`
})
add("weather", (a) => {
output(`<img src='https://wttr.in/${a}_pq0.png' />`)
})
add("forecast", (a) => {
popup("Weather forecast", `<img src='https://wttr.in/${a}_q.png' />`)
})
add("mdn", term => se("https://developer.mozilla.org/en-US/search?q=%s", term))