forked from RMeurisse/Arduino-Builder-chrome-extension
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
32 lines (30 loc) · 1.05 KB
/
background.js
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
var uploader = require('./lib/flash');
// when the webpage sends a message and we receive it, pass on the info to the uploader and request a flash to the device.
chrome.runtime.onConnectExternal.addListener(function(port) {
port.onMessage.addListener(function(msg) {
// call flash process
uploader.flash(msg.board, msg.port, msg.file, function(error) {
// prepare the response object
var message = error ? {error: error.message} : {success: true};
// send back the status of the flash to the webpage so it knows when it's done/errored.
port.postMessage(message);
});
});
});
// Webpage sends normal message to check if extension is installed. Respond to this message.
chrome.runtime.onMessageExternal.addListener(
(message, sender, sendResponse) => {
if (message == 'check') {
sendResponse({
type: 'success'
});
return true;
} else if (message == 'ports') {
uploader.listPorts(function(error, ports) {
var message = error ? {error: error.message} : ports;
sendResponse({message});
});
return true;
}
}
);