-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
113 lines (98 loc) · 3.04 KB
/
background.js
File metadata and controls
113 lines (98 loc) · 3.04 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
chrome.app.runtime.onLaunched.addListener(function() {
chrome.app.window.create('index.html', {
innerBounds: {
width: 400,
height: 400
},
id: "UsbPosPrinter"
});
});
var port = 0;
var endpoint = 0x02;
var deviceInfo = { vendorId: 0x0416, productId: 0x5011};
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
if (message.type === 'version') {
sendResponse({ version: '0.1' });
} else if (message.type === 'print') {
requestAndPrint(message.lines);
}
});
chrome.runtime.onMessageExternal.addListener(function(message, sender, sendResponse) {
if (message.type === 'version') {
sendResponse({ version: '0.1' });
} else if (message.type === 'print') {
requestAndPrint(message.lines);
}
});
String.prototype.toBytes = function() {
var arr = []
for (var i=0; i < this.length; i++) {
arr.push(this[i].charCodeAt(0))
}
return arr
}
var print = function(printer, lines) {
var encodedLines = [];
for (var i = 0; i < lines.length; i++) {
var line = lines[i].toBytes().concat([0x01B, 0x64, 10]);
var encodedLines = encodedLines.concat(line)
}
var buffer = new Uint8Array(encodedLines).buffer;
var data = {
direction : "out",
endpoint : endpoint,
data : buffer
};
chrome.usb.bulkTransfer(printer, data, function(response) {
if (response.resultCode == 0) {
console.log("Success!");
} else {
console.log("Error", response);
}
});
chrome.usb.releaseInterface(printer, port, function() {
console.log('device released');
});
}
var gotPermissions = function(deviceCallback) {
chrome.usb.findDevices(deviceInfo, function(devices) {
if (devices && devices.length > 0) {
// use the first found device
var foundDevice = devices[0];
// now lets reset the device
chrome.usb.resetDevice(foundDevice, function() {
// perform some error checking to make sure we
// reset the device
if (!chrome.runtime.lastError) {
// now claim the interface using the port we
// specified
chrome.usb.claimInterface(foundDevice, port, function() {
if (!chrome.runtime.lastError) {
deviceCallback(foundDevice);
} else {
throw chrome.runtime.lastError.message;
}
})
} else {
throw chrome.runtime.lastError.message;
}
});
} else {
console.warn("Device not found!!");
}
});
};
var permissionObj = {permissions: [{'usbDevices': [deviceInfo] }]};
function requestAndPrint(lines) {
chrome.permissions.request( permissionObj, function(result) {
if (result) {
console.log('Have permissions');
gotPermissions(function(printer) {
print(printer, lines);
});
} else {
console.log('App was not granted the "usbDevices" permission.');
console.log(chrome.runtime.lastError);
}
});
}