Project

General

Profile

« Previous | Next » 

Revision cf838016

Added by koszko over 1 year ago

restore chromium support

View differences:

common/browser.js
3 3
 *
4 4
 * Function: Export the browser API object.
5 5
 *
6
 * Copyright (C) 2021 Wojtek Kosior
6
 * Copyright (C) 2021, 2022 Wojtek Kosior
7 7
 *
8 8
 * This program is free software: you can redistribute it and/or modify
9 9
 * it under the terms of the CC0 1.0 Universal License as published by
......
21 21

  
22 22
#ELIF CHROMIUM
23 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

  
24 44
#EXPORT  chrome  AS browser
25 45

  
26 46
#ENDIF

Also available in: Unified diff