-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
132 lines (122 loc) · 3.85 KB
/
worker.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
export default {
async fetch(request) {
const url = new URL(request.url)
const hasParams = url.searchParams.toString().length > 0
if (url.pathname === '/' && !hasParams) {
return new Response(HTML, {
headers: {
'content-type': 'text/html;charset=UTF-8'
}
})
}
const target = url.searchParams.get('url')
if (!target) {
return new Response('No URL specified', { status: 400 })
}
// Check if input looks like a domain or URL
const isLikelyDomainOrURL = /^(https?:\/\/)?([a-zA-Z0-9.-]+\.[a-zA-Z]{2,})(\/.*)?$/.test(target)
if (isLikelyDomainOrURL) {
const formattedURL = target.startsWith('http') ? target : `https://${target}`
return Response.redirect(formattedURL, 302)
}
// Define bangs for supported search engines
const bangs = {
'!g': 'https://www.google.com/search?q=',
'!y': 'https://search.yahoo.com/search?p=',
'!br': 'https://search.brave.com/search?q=',
'!ddg': 'https://duckduckgo.com/?q=',
'!medium': 'https://medium.com/search?q=',
'!w': 'https://en.wikipedia.org/wiki/Special:Search?search=',
'!a': 'https://www.amazon.com/s?k=',
'!eb': 'https://www.ebay.com/sch/i.html?_nkw=',
'!ae': 'https://aliexpress.com/wholesale?SearchText=',
'!et': 'https://www.etsy.com/search?q=',
'!yt': 'https://www.youtube.com/results?search_query=',
'!r': 'https://www.reddit.com/search?q=',
'!gh': 'https://github.com/search?q=',
'!npm': 'https://www.npmjs.com/search?q=',
'!dh': 'https://hub.docker.com/search?q='
}
const trimmedTarget = target.trim()
const [bang, ...queryParts] = trimmedTarget.split(/\s+/)
const query = queryParts.join(' ').trim()
if (bang in bangs) {
const searchURL = `${bangs[bang]}${encodeURIComponent(query)}`
return Response.redirect(searchURL, 302)
}
// Default to Google if no valid bang is found
const googleSearchURL = `https://www.google.com/search?q=${encodeURIComponent(target)}`
return Response.redirect(googleSearchURL, 302)
}
}
const HTML = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>Hyperbox</title>
<style>
body {
margin: 0;
background-color: black;
color: lightgrey;
font-family: 'Arial', sans-serif;
}
input {
background-color: black;
border: 1px solid lightgrey;
padding: .5rem 1rem;
margin-bottom: .75rem;
border-radius: 1rem;
;
width: 300px;
color: lightgrey;
}
button {
padding: .5rem 2rem;
background-color: black;
border: 1px solid lightgrey;
color: lightgrey;
border-radius: .5rem;
;
}
</style>
</head>
<body>
<table width="100%" height="100%" border="0">
<tr>
<td align="center" valign="middle">⠀</td>
</tr>
<tr>
<td align="center" valign="middle"></td>
</tr>
<tr>
<td align="center" valign="middle">⠀</td>
</tr>
<tr>
<td align="center" valign="middle"></td>
</tr>
<tr>
<td align="center" valign="middle">⠀</td>
</tr>
<tr>
<td align="center" valign="middle"></td>
</tr>
<tr>
<td align="center" valign="middle">Search the web or type a URL</td>
</tr>
<tr>
<td align="center" valign="middle">⠀</td>
</tr>
<tr>
<td align="center" valign="middle">
<form action="/" method="GET">
<input type="text" name="url" required autofocus autocomplete="off">
<br>
<button type="submit">Enter</button>
</form>
</td>
</tr>
</table>
</body>
</html>`