-
Notifications
You must be signed in to change notification settings - Fork 2
/
LocalStream.ts
33 lines (29 loc) · 871 Bytes
/
LocalStream.ts
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
declare const chrome;
/***
* Un-encrypted stream used to communicate between an extensions popup script and background script.
*/
export class LocalStream {
/***
* Sends a message to the background script
* @param msg - The message to send
* @returns {Promise<T>} - Responds with the message from the `watch` method's sendResponse parameter.
*/
static send(msg){
return new Promise((resolve:any, reject:any) => {
chrome.runtime.sendMessage(msg, (response) => resolve(response))});
}
/***
* Watches for messages from the background script
* @param callback - A message parsing function
*/
static watch(callback:any) {
chrome.runtime.onMessage.addListener(
(request, sender, sendResponse) => {
if(sender.id !== chrome.runtime.id) return;
callback(request, sendResponse);
return true;
}
);
}
}
export default LocalStream;