forked from SimGus/chrome-extension-v3-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice-worker.js
More file actions
55 lines (46 loc) · 1.89 KB
/
Copy pathservice-worker.js
File metadata and controls
55 lines (46 loc) · 1.89 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
// This is the service worker script, which executes in its own context
// when the extension is installed or refreshed (or when you access its console).
// It would correspond to the background script in chrome extensions v2.
console.log("This prints to the console of the service worker (background script)")
// Importing and using functionality from external files is also possible.
importScripts('service-worker-utils.js')
// If you want to import a file that is deeper in the file hierarchy of your
// extension, simply do `importScripts('path/to/file.js')`.
// The path should be relative to the file `manifest.json`.
let devices;
// devices = getHidDevices();
navigator.hid.getDevices().then(devs => {devices = devs});
// Example of a simple user data object
const user = {
username: 'demo-user'
};
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
console.log('receive msg:', message);
let cmd = message.cmd;
let data = message.data;
if (cmd === 'num_devices') {
sendResponse(devices.length);
} else if (cmd == "open_device") {
let idx = data;
if (idx >= devices.length) {
sendResponse('Not enough num of devices:', devices.length);
}
devices[idx].open().then(() => {
sendResponse('device opened for idx:', idx);
});
// await devices[idx].open();
// sendResponse('device opened for idx:', idx);
} else if (cmd == "close_device") {
let idx = data;
if (idx >= devices.length) {
sendResponse('Not enough num of devices:', devices.length);
}
devices[idx].close().then(() => {
sendResponse('device closed for idx:', idx);
});
// await devices[idx].close();
// sendResponse('device closed for idx:', idx);
}
// https://github.com/mozilla/webextension-polyfill/issues/130
return true;
});