-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
262 lines (249 loc) · 10.5 KB
/
index.html
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Open Search</title>
<meta name="author" content="David Urbansky">
<link rel="shortcut icon" href="/favicon.ico"/>
<link rel="stylesheet" href="css/styles.min.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link
href="https://fonts.googleapis.com/css2?family=Caveat&family=Indie+Flower&family=Josefin+Sans:ital,wght@0,300;0,400;1,700&family=Poppins:wght@200;400;600;900&display=swap"
rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.global.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<!-- tracking script for opensearch.cc, comment out for self-hosting -->
<script defer data-domain="opensearch.cc" src="https://plausible.io/js/script.js"></script>
</head>
<body>
<div id="main">
<header>
<div id="logo">
<img src="img/o.svg" :class="{'rotating':loading}"><img src="img/pen-search.svg">
</div>
<div class="search-bar">
<input type="search" v-model="query" @keyup.enter="search">
<button @click="randomSearch"><img src="img/random.svg"/></button>
<button @click="search"><img src="img/search-o.svg"/></button>
</div>
<div v-if="searchLog" id="search-log">
{{ searchLog }}
</div>
</header>
<main>
<img v-if="loading" src="img/loading.gif"/>
<div v-else-if="dynamicComponent">
<component :is="dynamicComponent"></component>
</div>
</main>
<footer>
<hr>
This experimental search is powered by free APIs and artificial intelligence. If you have an API, you can
contribute to the <a href="https://github.com/ddsky/open-search">Open Search project</a>.
</footer>
</div>
<script>
// if true https and wss will be used, for localhost, probably this should be false
// prod:
const SECURE = true;
const API_URL = 'api.opensearch.cc';
// dev:
// const SECURE = false;
// const API_URL = 'localhost:4567';
const app = Vue.createApp({
data() {
return {
// search via 'http' api or 'websocket' API
searchMode: 'http',
socketUrl: null,
socket: null,
dynamicComponent: null,
loading: false,
query: '',
searchLog: null,
writingLog: false,
exampleQueries: [
'weather in London, UK tomorrow',
'tell me a random cat fact',
'show me a dog meme',
'show me drinks I can make with gin',
'chicken soup recipes',
'muffin recipes',
'tell me something wholesome',
'tell me a dad joke',
'tell me a chuck norris joke',
'find books about climate change',
'what is todays calendar?',
'show me a comic',
'whats the bitcoin price now?',
'what\'s the gender of people called Ashley?',
'what is behind the zip code 01187 in Germany?',
'when is the next spacex launch',
'when is sunrise in Tokyo, Japan?',
'look for universities named technical university',
'random quote please',
'find me a name for a baby boy',
'what do groundhogs pretend for this year',
'what has happened in London recently?',
'best games for batman fans',
'wild west shooters',
'show propjet engine airplanes',
'which airports does Edinburgh have?',
'What\'s the air quality like in Beijing China?',
'tell me something about penguins',
'what should I do before I die?',
'how many calories does 30 minutes of rowing burn?',
'What is Michael Jordan\'s net worth?',
'which city has more than 20 million people?',
'convert 100€ to usd',
'tell me about argentina',
'define the word uniqueness',
'what exercises can I do for my biceps?',
'which historic events happened in May 1824',
'who was mark twain',
'list planets that are at most 100 light years away from earth',
'what rhymes with cage',
'riddle me',
'what time is it in Los Angeles right now?'
]
}
},
mounted() {
this.socketUrl = API_URL + "/socket";
if (SECURE) {
this.socketUrl = 'wss://' + this.socketUrl;
} else {
this.socketUrl = 'ws://' + this.socketUrl;
}
this.socket = new WebSocket(this.socketUrl);
this.socket.onopen = () => {
console.log('connected to websocket, switching to websocket mode');
this.searchMode = 'websocket';
};
this.socket.onclose = () => {
console.log('connection to websocket closed');
};
this.socket.onmessage = (event) => {
console.log(event.data);
const messageContent = event.data;
if (messageContent.indexOf('<') < 0) {
this.addLogMessage(messageContent);
} else {
this.parseHtmlResult(messageContent);
this.loading = false;
}
};
},
methods: {
sendMessageOverSocket() {
this.socket.send(this.query);
},
addLogMessage(message) {
// is log is being written, wait until it is finished
if (this.writingLog) {
setTimeout(() => {
this.addLogMessage(message);
}, 100);
return;
}
this.writingLog = true;
if (this.searchLog == null) {
this.searchLog = '';
} else {
this.searchLog += ', ';
}
let index = 0;
const typeWriterEffect = () => {
if (index < message.length) {
this.searchLog += message.charAt(index);
index++;
setTimeout(typeWriterEffect, 40);
} else {
this.writingLog = false;
}
};
typeWriterEffect();
},
randomSearch() {
// pick a random query from the exampleQueries array
this.query = this.exampleQueries[Math.floor(Math.random() * this.exampleQueries.length)];
this.search();
},
search() {
this.loading = true;
this.searchLog = null;
if (this.searchMode === 'websocket' && this.socket.readyState === 3) {
this.socket = new WebSocket(this.socketUrl);
setTimeout(() => {
this.search();
}, 500);
return;
}
if (this.searchMode === 'websocket' && this.socket.readyState === 1) {
console.log('search via websocket');
this.searchWebSocket();
} else {
console.log('search via http');
console.log(this.searchMode, this.socket.readyState);
this.searchHttp();
}
},
// search via the websocket API
searchWebSocket() {
this.sendMessageOverSocket();
},
// search via the http API
searchHttp() {
// axios.get('http://localhost:4567/search?query=' + encodeURIComponent(this.query))
let httpRequestUrl = API_URL + '/search?query=' + encodeURIComponent(this.query);
if (SECURE) {
httpRequestUrl = 'https://' + httpRequestUrl;
} else {
httpRequestUrl = 'http://' + httpRequestUrl;
}
axios.get(httpRequestUrl)
.then(response => {
this.parseHtmlResult(response.data);
})
.catch(error => {
console.error(error);
}).finally(() => {
this.loading = false;
})
},
parseHtmlResult(messageContent) {
let startPos = messageContent.indexOf('<div id="app">') + 14;
let template = messageContent.substring(startPos, messageContent.indexOf('<script>', startPos) - 8);
startPos = messageContent.indexOf('this.jsonData=') + 14
const jsonData = messageContent.substring(messageContent.indexOf('this.jsonData=') + 14, messageContent.indexOf('};', startPos) + 1);
startPos = messageContent.indexOf('<style>') + 7;
const style = messageContent.substring(startPos, messageContent.indexOf('</style>', startPos));
console.log(style);
console.log(template);
console.log(jsonData);
console.log(JSON.parse(jsonData));
// add styles to the template
const styleTag = document.createElement('style');
styleTag.type = 'text/css';
styleTag.innerHTML = style;
document.head.appendChild(styleTag);
this.dynamicComponent = this.createDynamicComponent(template, JSON.parse(jsonData));
},
createDynamicComponent(template, jsonData) {
return {
template: template,
data() {
return {
jsonData: jsonData
};
}
};
}
}
});
app.mount('#main');
</script>
</body>
</html>