-
Notifications
You must be signed in to change notification settings - Fork 5
/
minidisc-label.html
159 lines (139 loc) · 4.02 KB
/
minidisc-label.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
<html>
<head>
<title>MiniDisc Label Printer</title>
</head>
<body>
<p>Enter up to 5 titles:</p>
<p>
<input type="text" id="label0" size="80" value=""></input><br>
<input type="text" id="label1" size="80" value=""></input><br>
<input type="text" id="label2" size="80" value=""></input><br>
<input type="text" id="label3" size="80" value=""></input><br>
<input type="text" id="label4" size="80" value=""></input>
</p>
<p>
<button id="print">Print</button>
<span id="status">No printer detected.</span>
</p>
<p>
Raw ZPL:
</p>
<textarea id="label" cols="80" rows="35"></textarea>
<p>
Click to copy to clipboard. Paste into
<a href="https://labelary.com/viewer.html">Labelary Online ZPL Viewer</a>
to preview.
</p>
<script>
const kNumLabels = 5;
let status = document.getElementById('status');
let device;
let label = document.getElementById('label');
function updateLabel() {
const kLeftMargin = 15;
const kTopMargin = 25;
const kLabelHeight = 30;
const kLabelWidth = 470;
const kPreamble = [
'^XA',
'^PR2',
'^PW609',
'^LL203',
'^LH0,0',
'^LS0',
'^LT0',
'^CFQ',
];
const body = [];
let x = kLeftMargin;
let y = kTopMargin;
body.push(`^FO${x},${y}^GB10,1,1^FS`);
body.push(`^FO${x+230},${y}^GB10,1,1^FS`);
body.push(`^FO${x+460},${y}^GB10,1,1^FS`);
for (let i = 0; i < kNumLabels; ++i) {
const input = document.getElementById(`label${i}`);
body.push(`^FO${x},${y+2}^FD${input.value}^FS`);
y += kLabelHeight;
body.push(`^FO${x},${y}^GB10,1,1^FS`);
body.push(`^FO${x+230},${y}^GB10,1,1^FS`);
body.push(`^FO${x+460},${y}^GB10,1,1^FS`);
}
const kEpilogue = [
'^XZ',
];
label.value = kPreamble.concat(body, kEpilogue).join('\n');
}
updateLabel();
label.onclick = () => {
label.select();
document.execCommand('copy');
};
for (let i = 0; i < kNumLabels; ++i) {
const input = document.getElementById(`label${i}`);
input.oninput = () => {
updateLabel();
};
}
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: [
{ 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 buffer = (new TextEncoder()).encode(label.value);
await device.transferOut(outEndpoint.endpointNumber, buffer);
status.textContent = 'Document printed.';
} catch (e) {
status.textContent = `${e.name}: ${e.message}`;
} finally {
await device.close();
}
});
</script>
</body>
</html>