/** * This file is part of Haketilo. * * Function: Export the browser API object. * * Copyright (C) 2021, 2022 Wojtek Kosior * * This program is free software: you can redistribute it and/or modify * it under the terms of the CC0 1.0 Universal License as published by * the Creative Commons Corporation. * * 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 * CC0 1.0 Universal License for more details. */ #IF MOZILLA #EXPORT globalThis.browser AS browser #ELIF CHROMIUM /* Use promises for sendMessage(). */ function response_cb(resolve, reject, response) { if (arguments.length < 3) reject(chrome.runtime.lastError); else resolve(response); } function replacement_sendMessage(old_sendMessage, ...args) { let callbacks, prom = new Promise((...cbs) => callbacks = cbs); old_sendMessage(...args, resp => response_cb(...callbacks, resp)); return prom; } for (const api_object of [chrome.tabs, chrome.runtime].filter(a => a)) { const old_sendMessage = api_object.sendMessage; api_object.sendMessage = (...args) => replacement_sendMessage(old_sendMessage, ...args); } #EXPORT chrome AS browser #ENDIF