-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathbackground.js
64 lines (54 loc) · 1.98 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
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
/**
* Copyright (c) 2024, Tidepool Project
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the associated License, which is identical to the BSD 2-Clause
* License as published by the Open Source Initiative at opensource.org.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the License for more details.
*
* You should have received a copy of the License along with this program; if
* not, you can obtain one from Tidepool Project at tidepool.org.
*/
let reply = undefined;
let port = undefined;
/* global chrome */
function connect() {
const hostName = 'org.tidepool.uploader_helper';
console.log('Connecting to native messaging host', hostName);
port = chrome.runtime.connectNative(hostName);
port.onMessage.addListener(onNativeMessage);
port.onDisconnect.addListener(onDisconnected);
}
function sendNativeMessage(message) {
port.postMessage(message);
console.log('Sent message:', JSON.stringify(message));
}
function onNativeMessage(message) {
if (message.msgType == 'info') {
console.log(message.details);
} else {
reply(message);
}
}
function onDisconnected() {
console.log('Disconnected: ' + chrome.runtime.lastError.message);
port = null;
reply({ msgType: 'error', details: 'Disconnected: ' + chrome.runtime.lastError.message });
}
chrome.runtime.onMessageExternal.addListener(function(request, sender, sendResponse) {
console.log('Received message from the web page:', request);
if (!port) {
connect();
}
if (port) {
sendNativeMessage(request);
reply = sendResponse;
return true; // indicates we will asynchronously use sendResponse
} else {
sendResponse({ msgType: 'error', details: 'Not connected.'});
}
}
);