1
|
/**
|
2
|
* This file is part of Haketilo.
|
3
|
*
|
4
|
* Function: Export the browser API object.
|
5
|
*
|
6
|
* Copyright (C) 2021, 2022 Wojtek Kosior
|
7
|
*
|
8
|
* This program is free software: you can redistribute it and/or modify
|
9
|
* it under the terms of the CC0 1.0 Universal License as published by
|
10
|
* the Creative Commons Corporation.
|
11
|
*
|
12
|
* This program is distributed in the hope that it will be useful,
|
13
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15
|
* CC0 1.0 Universal License for more details.
|
16
|
*/
|
17
|
|
18
|
#IF MOZILLA
|
19
|
|
20
|
#EXPORT globalThis.browser AS browser
|
21
|
|
22
|
#ELIF CHROMIUM
|
23
|
|
24
|
/* Use promises for sendMessage(). */
|
25
|
function response_cb(resolve, reject, response) {
|
26
|
if (arguments.length < 3)
|
27
|
reject(chrome.runtime.lastError);
|
28
|
else
|
29
|
resolve(response);
|
30
|
}
|
31
|
|
32
|
function replacement_sendMessage(old_sendMessage, ...args) {
|
33
|
let callbacks, prom = new Promise((...cbs) => callbacks = cbs);
|
34
|
old_sendMessage(...args, resp => response_cb(...callbacks, resp));
|
35
|
return prom;
|
36
|
}
|
37
|
|
38
|
for (const api_object of [chrome.tabs, chrome.runtime].filter(a => a)) {
|
39
|
const old_sendMessage = api_object.sendMessage;
|
40
|
api_object.sendMessage =
|
41
|
(...args) => replacement_sendMessage(old_sendMessage, ...args);
|
42
|
}
|
43
|
|
44
|
#EXPORT chrome AS browser
|
45
|
|
46
|
#ENDIF
|