|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 Yomitan Authors |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +import {EventDispatcher} from '../core/event-dispatcher.js'; |
| 19 | +import {toError} from '../core/to-error.js'; |
| 20 | + |
| 21 | +/** |
| 22 | + * @augments EventDispatcher<import('web-extension').Events> |
| 23 | + */ |
| 24 | +export class WebExtension extends EventDispatcher { |
| 25 | + constructor() { |
| 26 | + super(); |
| 27 | + /** @type {boolean} */ |
| 28 | + this._unloaded = false; |
| 29 | + } |
| 30 | + |
| 31 | + /** @type {boolean} */ |
| 32 | + get unloaded() { |
| 33 | + return this._unloaded; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @param {string} path |
| 38 | + * @returns {string} |
| 39 | + */ |
| 40 | + getUrl(path) { |
| 41 | + return chrome.runtime.getURL(path); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @param {unknown} message |
| 46 | + * @param {(response: unknown) => void} responseCallback |
| 47 | + * @throws {Error} |
| 48 | + */ |
| 49 | + sendMessage(message, responseCallback) { |
| 50 | + try { |
| 51 | + chrome.runtime.sendMessage(message, responseCallback); |
| 52 | + } catch (error) { |
| 53 | + this.triggerUnloaded(); |
| 54 | + throw toError(error); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @param {unknown} message |
| 60 | + * @returns {Promise<unknown>} |
| 61 | + */ |
| 62 | + sendMessagePromise(message) { |
| 63 | + return new Promise((resolve, reject) => { |
| 64 | + try { |
| 65 | + this.sendMessage(message, (response) => { |
| 66 | + const error = this.getLastError(); |
| 67 | + if (error !== null) { |
| 68 | + reject(error); |
| 69 | + } else { |
| 70 | + resolve(response); |
| 71 | + } |
| 72 | + }); |
| 73 | + } catch (error) { |
| 74 | + reject(error); |
| 75 | + } |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @param {unknown} message |
| 81 | + */ |
| 82 | + sendMessageIgnoreResponse(message) { |
| 83 | + this.sendMessage(message, () => { |
| 84 | + // Clear the last error |
| 85 | + this.getLastError(); |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @returns {?Error} |
| 91 | + */ |
| 92 | + getLastError() { |
| 93 | + const {lastError} = chrome.runtime; |
| 94 | + if (typeof lastError !== 'undefined') { |
| 95 | + const {message} = lastError; |
| 96 | + return new Error(typeof message === 'string' ? message : 'An unknown web extension error occured'); |
| 97 | + } |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + /** */ |
| 102 | + triggerUnloaded() { |
| 103 | + if (this._unloaded) { return; } |
| 104 | + this._unloaded = true; |
| 105 | + this.trigger('unloaded', {}); |
| 106 | + } |
| 107 | +} |
0 commit comments