forked from magicoflolis/Userscript-Plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathext.js
More file actions
34 lines (30 loc) · 927 Bytes
/
Copy pathext.js
File metadata and controls
34 lines (30 loc) · 927 Bytes
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
export const webext =
self.browser instanceof Object && self.browser instanceof Element === false
? self.browser
: self.chrome;
export const runtime = webext.runtime;
/******************************************************************************/
// The extension's service worker can be evicted at any time, so when we
// send a message, we try a few more times when the message fails to be sent.
export function sendMessage(msg) {
return new Promise((resolve, reject) => {
let i = 5;
const send = () => {
runtime
.sendMessage(msg)
.then((response) => {
resolve(response);
})
.catch((reason) => {
i -= 1;
if (i <= 0) {
reject(reason);
} else {
setTimeout(send, 200);
}
});
};
send();
});
}
/******************************************************************************/