-
Notifications
You must be signed in to change notification settings - Fork 23
/
printer.html
49 lines (44 loc) · 1.08 KB
/
printer.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
<html>
<body>
<textarea id="printContent"></textarea>
<input type="submit" onclick="connectAndPrint()" value="Print"/>
<P>Type text into box and click on submit button.
<script>
var device;
function setup(device) {
return device.open()
.then(() => device.selectConfiguration(1))
.then(() => device.claimInterface(0))
}
function print() {
var string = document.getElementById("printContent").value + "\n";
var encoder = new TextEncoder();
var data = encoder.encode(string);
device.transferOut(1, data)
.catch(error => { console.log(error); })
}
function connectAndPrint() {
if (device == null) {
navigator.usb.requestDevice({ filters: [{ vendorId: 1046 }] })
.then(selectedDevice => {
device = selectedDevice;
console.log(device);
return setup(device);
})
.then(() => print())
.catch(error => { console.log(error); })
}
else
print();
}
navigator.usb.getDevices()
.then(devices => {
if (devices.length > 0) {
device = devices[0];
return setup(device);
}
})
.catch(error => { console.log(error); });
</script>
</body>
</html>