forked from bizzycola/qrcode-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
192 lines (159 loc) · 7.31 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
<!DOCTYPE html>
<html>
<head>
<title>QR Code Generator</title>
<link href="lib/tailwind.min.css" rel="stylesheet" type="text/css" />
<style>
html,
body,
#app {
height: 100vh;
margin: 0;
padding: 0;
font-family: 'Inter', sans-serif;
}
svg {
width: 256px;
height: 256px;
}
.mwh {
min-width: 380px;
min-height: 290px;
}
a {
color: rgb(34, 125, 230);
}
a:hover {
color: rgb(0, 105, 224);
}
</style>
</head>
<body>
<div class="w-full h-full bg-gray-800 absolute flex flex-row justify-center items-center">
<div class="bg-gray-300 m-auto shadow-lg p-5 rounded w-1/4 h-auto mwh">
<div class="flex flex-col justify-start w-full h-full items-center">
<!--TEXT-->
<div class="flex flex-row items-center h-14 rounded border-gray-900 bg-gray-800 border-1 text-white mb-2 w-full">
<a class="flex-2 p-2 block flex flex-row items-center justify-center hover:bg-gray-700 h-full" href="https://github.com/zxing/zxing/wiki/Barcode-Contents" target="_blank">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
aria-hidden="true" role="img" width="1em" height="1em"
preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" style="width: 1em !important; height: 1em !important;">
<g fill="none">
<path
d="M13 22h-3v-3h3v3zm0-5h-3v-.007c0-1.65 0-3.075.672-4.073a6.304 6.304 0 0 1 1.913-1.62c.334-.214.649-.417.914-.628a3.712 3.712 0 0 0 1.332-3.824A3.033 3.033 0 0 0 9 8H6a6 6 0 0 1 6-6a6.04 6.04 0 0 1 5.434 3.366a6.017 6.017 0 0 1-.934 6.3c-.453.502-.96.95-1.514 1.337a7.248 7.248 0 0 0-1.316 1.167A4.23 4.23 0 0 0 13 17z"
fill="currentColor" />
</g>
</svg>
</a>
<input type="text" placeholder="Content"
class="h-full pr-4 pl-1 flex-1 text-current bg-transparent focus:outline-none" id="linkBox"
value="https://google.com">
<button class="h-full flex-2 pr-2 pl-2 border-l-gray-900 border-l-1 hover:bg-gray-700"
id="genBtn">Generate</button>
</div>
<!--WIFI-->
<div class="flex flex-row items-center h-14 pl-1 rounded border-gray-900 bg-gray-800 border-1 text-white mb-2 w-full max-w-full">
<input type="text" placeholder="Wifi SSID"
class="h-full pl-1 text-current bg-transparent focus:outline-none w-full" id="ssidBox"
value="">
<input type="password" placeholder="Password"
class="h-full pl-1 text-current bg-transparent focus:outline-none w-full" id="pwdBox"
value="">
<button class="h-full pl-2 text-right pr-2 border-l-gray-900 border-l-1 hover:bg-gray-700 rounded-r-md"
id="genWifiBtn">Generate</button>
</div>
<div id="qrcode" class="m-2"></div>
<!-- download links -->
<div>
<a href="#" id="dlpng" class="mr-2 text-blue">PNG</a>
<select id="imgSize" name="imgSize" class="mt-2 rounded-md border-0 py-1.5 pl-3 pr-10 text-gray-900 ring-1 ring-inset ring-gray-300 focus:ring-2 focus:ring-indigo-600 sm:text-sm sm:leading-6 inline">
<option value="256" selected>256px</option>
<option value="512">512px</option>
<option value="1024">1024px</option>
<option value="2048">2048px</option>
</select>
<a href="#" id="dlsvg" class="ml-2">SVG</a>
</div>
<canvas id="canv" style="display: none" width="512" height="512" />
</div>
</div>
</div>
<script src="lib/qrcode.min.js"></script>
<script type="module">
import { Canvg } from 'https://cdn.skypack.dev/canvg@^4.0.0'
let lastQuery = 'https://google.com'
const size = document.getElementById('imgSize')
function genSvg(val) {
return new QRCode({
content: val,
container: 'svg-viewbox',
join: true,
width: size.value,
height: size.value
}).svg()
}
function genPng(val) {
const canvas = document.getElementById('canv')
canvas.width = size.value
canvas.height = size.value
const ctx = canvas.getContext('2d')
const svg = genSvg(val)
const rend = Canvg.fromString(ctx, svg)
rend.start()
return canvas.toDataURL('img/png')
}
// Wifi QR Code
// Format: WIFI:S:SSID_NAME;H:true;T:WPA2;P:PASSWORD;;
window.onload = () => {
// Check query params
const params = new Proxy(new URLSearchParams(window.location.search), {
get: (searchParams, prop) => searchParams.get(prop),
});
let btn = document.getElementById("genBtn")
let link = document.getElementById("linkBox")
let qr = document.getElementById("qrcode");
let genWifiBtn = document.getElementById("genWifiBtn")
let ssidBox = document.getElementById("ssidBox")
let pwdBox = document.getElementById("pwdBox")
// Standard QR
btn.onclick = () => {
qr.innerHTML = genSvg(link.value)
lastQuery = link.value
}
// Wifi QR
genWifiBtn.onclick = () => {
const ssid = ssidBox.value
const pwd = pwdBox.value
const wifi = `WIFI:S:${ssid};H:true;T:WPA2;P:${pwd};;`
qr.innerHTML = genSvg(wifi)
lastQuery = wifi
}
const qlink = params.link
if(qlink && qlink.trim() !== '') {
link.value = qlink
qr.innerHTML = genSvg(qlink)
lastQuery = qlink
}
else
qr.innerHTML = genSvg('https://google.com')
// Download
var dlPng = document.getElementById('dlpng')
var dlSvg = document.getElementById('dlsvg')
dlPng.onclick = () => {
var link = document.createElement('a')
link.download = `${encodeURI(lastQuery)}.png`
link.href = genPng(lastQuery)
link.click()
}
dlSvg.onclick = () => {
const svgBlob = new Blob([genSvg(lastQuery)], {type:"image/svg+xml;charset=utf-8"})
const svgUrl = URL.createObjectURL(svgBlob)
let link = document.createElement('a')
link.download = `${encodeURI(lastQuery)}.svg`
link.href = svgUrl
link.click()
}
}
</script>
</body>
</html>