-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathprinter-tool.html
More file actions
115 lines (104 loc) · 3.2 KB
/
Copy pathprinter-tool.html
File metadata and controls
115 lines (104 loc) · 3.2 KB
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
<html>
<head>
<title>Printer Tool</title>
</head>
<body>
<p>
<button id="print">Print</button>
<span id="status">No printer detected.</span>
</p>
<p>
<textarea id="data" cols="80", rows="24"></textarea>
</p>
<p>
Supported ASCII escape codes:
<ul>
<li><HT></li>
<li><LF></li>
<li><FF></li>
<li><CR></li>
<li><DLE></li>
<li><CAN></li>
<li><ESC></li>
<li><FS></li>
<li><GS></li>
</ul>
These strings are replaced with their equivalent ASCII codes before the data is sent to the printer.
</p>
<script>
let status = document.getElementById('status');
let device;
navigator.usb.getDevices().then((devices) => {
if (devices.length > 0) {
device = devices[0];
status.textContent = 'Printer ready.';
}
if (devices.length > 1) {
status.textContent = 'More than one printer detected, using the first one.';
}
});
navigator.usb.addEventListener('connect', (e) => {
if (device) {
status.textContent = 'More than one printer detected, using the first one.';
} else {
device = e.device;
status.textContent = 'Printer ready.';
}
});
navigator.usb.addEventListener('disconnect', (e) => {
if (device == e.device) {
device = null;
status.textContent = 'Printer disconnected.';
}
});
document.getElementById('print').addEventListener('click', async () => {
if (!device) {
try {
device = await navigator.usb.requestDevice({ filters: [
{ classCode: 7, subclassCode: 1 }, // USB printer
{ vendorId: 0x04b8 }, // Epson
{ vendorId: 0x0a5f }, // Zebra
]});
status.textContent = 'Printer ready.';
} catch (e) {
status.textContent = 'No device selected.';
return;
}
}
await device.open();
try {
await device.selectConfiguration(1);
await device.claimInterface(0);
await device.selectAlternateInterface(0, 0);
let outEndpoint;
for (let endpoint of device.configuration.interfaces[0].alternate.endpoints) {
if (endpoint.direction == 'out') {
outEndpoint = endpoint;
break;
}
}
if (!outEndpoint) {
status.textContent = 'No OUT endpoint found.';
return;
}
const data = document.getElementById('data').value
.replaceAll('<HT>', '\x09')
.replaceAll('<LF>', '\x0A')
.replaceAll('<FF>', '\x0C')
.replaceAll('<CR>', '\x0D')
.replaceAll('<DLE>', '\x10')
.replaceAll('<CAN>', '\x18')
.replaceAll('<ESC>', '\x1B')
.replaceAll('<FS>', '\x1C')
.replaceAll('<GS>', '\x1D');
const buffer = (new TextEncoder()).encode(data);
await device.transferOut(outEndpoint.endpointNumber, buffer);
} catch (e) {
status.textContent = `${e.name}: ${e.message}`;
} finally {
await device.close();
}
});
</script>
</body>
</html>