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
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
</head>
<body>
<!-- Note the usage of `type=module` here as this is an ES6 module -->
<script type="module">
// Use ES module import syntax to import functionality from the module
// that we have compiled.
//
// Note that the `default` import is an initialization function which
// will "boot" the module and make it ready to use. Currently browsers
// don't support natively imported WebAssembly as an ES module, but
// eventually the manual initialization won't be required!
// import { search, default as init } from './tinysearch_engine.js';
import { search, default as init } from './tinysearch_engine.js';
window.search = search;
async function run() {
// First up we need to actually load the wasm file, so we use the
// default export to inform it where the wasm file is located on the
// server, and then we wait on the returned promise to wait for the
// wasm to be loaded.
//
// Note that instead of a string here you can also pass in an instance
// of `WebAssembly.Module` which allows you to compile your own module.
// Also note that the promise, when resolved, yields the wasm module's
// exports which is the same as importing the `*_bg` module in other
// modes
await init('./tinysearch_engine_bg.wasm');
}
run();
</script>
<script>
// And afterwards we can use all the functionality defined in wasm.
function doSearch() {
let value = document.getElementById("demo").value;
const arr = search(value, 10);
let ul = document.getElementById("results");
ul.innerHTML = "";
for (i = 0; i < arr.length; i++) {
var li = document.createElement("li");
let elem = arr[i];
let elemlink = document.createElement('a');
elemlink.innerHTML = elem[0];
elemlink.setAttribute('href', elem[1]);
li.appendChild(elemlink);
ul.appendChild(li);
}
}
</script>
<h2>Search:</h2>
<input type="text" id="demo" onkeyup="doSearch()">
<h2>Results:</h2>
<ul id="results">
</ul>
</body>
</html>