forked from tadas-s/isbnjs
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.html
125 lines (110 loc) · 3.12 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
<html>
<head>
<meta charset="utf-8" />
<title>ISBN Parser</title>
<style type="text/css">
body{
margin: 1rem;
font-family: sans-serif;
}
#title{
font-size: x-large;
margin: 1rem 0;
}
#intro{
color: #444;
}
.control{
margin: 1rem 0;
}
pre:not(:empty){
background-color: #eee;
white-space: pre-wrap;
}
pre:not(:empty):not(.inline){
padding: 1em;
max-width: 30em;
margin: 1em 0;
}
.no-js{
color: red;
padding: 1em;
background-color: #ccc;
}
pre.inline{
display: inline;
padding: 0.2rem;
}
</style>
<script type="application/javascript" src="./dist/isbn.js"></script>
</head>
<body>
<div id="title">ISBN Parser</div>
<p id="intro">This is a demo of what the <a href="https://github.com/inventaire/isbnjs">isbn3</a> JS module can deduce from an <a href="https://en.wikipedia.org/wiki/International_Standard_Book_Number">ISBN</a></p>
<div class="control">
ISBN: <input id="isbnInput" type="text" value="9781781682135" />
</div>
<strong>
<a href="https://github.com/inventaire/isbn3#parse"><pre class="inline">ISBN.parse</pre></a>
result:
</strong>
<pre id="result"></pre>
<strong>
<a href="https://github.com/inventaire/isbn3#audit"><pre class="inline">ISBN.audit</pre></a>
result:
</strong>
<pre id="audit"></pre>
<noscript>
<p class="no-js">This demo requires javascript</p>
</noscript>
<script type="application/javascript">
const queryIsbn = new URLSearchParams(window.location.search).get('isbn')
const debouncedUpdate = debounce(update, 100)
if (queryIsbn) isbnInput.value = queryIsbn
window.addEventListener('load', function () {
update()
isbnInput.addEventListener('keyup', debouncedUpdate)
})
var lastValue
function update () {
const currentValue = isbnInput.value.trim()
if (currentValue === lastValue) return
lastValue = currentValue
if (currentValue === '') {
result.innerHTML = 'Missing ISBN'
audit.innerHTML = 'Missing ISBN'
return
}
const isbnData = ISBN.parse(currentValue)
if (isbnData) {
result.innerHTML = JSON.stringify(isbnData, null, 2)
} else {
result.innerHTML = 'invalid ISBN'
}
const isbnAudit = ISBN.audit(currentValue)
audit.innerHTML = JSON.stringify(isbnAudit, null, 2)
updateQueryString(currentValue)
}
function updateQueryString (isbn) {
window.history.pushState({ isbn }, isbn, `?isbn=${isbn}`)
}
// Restore the ISBN from an History event
// cf https://developer.mozilla.org/en-US/docs/Web/API/History_API
window.onpopstate = function ({ state }) {
const { isbn } = state
if (isbn) isbnInput.value = isbn
}
// Inspired by https://gist.github.com/nmsdvid/8807205
function debounce(func, wait) {
var timeout
return function () {
clearTimeout(timeout)
timeout = setTimeout(() => {
timeout = null
func.apply(this, arguments)
}, wait)
}
}
</script>
</body>
</html>