File indexing completed on 2024-04-28 05:32:49

0001 /*
0002     Copyright (C) 2017, 2019 Kai Uwe Broulik <kde@privat.broulik.de>
0003     Copyright (C) 2018 David Edmundson <davidedmundson@kde.org>
0004 
0005     This program is free software; you can redistribute it and/or
0006     modify it under the terms of the GNU General Public License as
0007     published by the Free Software Foundation; either version 3 of
0008     the License, or (at your option) any later version.
0009 
0010     This program is distributed in the hope that it will be useful,
0011     but WITHOUT ANY WARRANTY; without even the implied warranty of
0012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013     GNU General Public License for more details.
0014 
0015     You should have received a copy of the GNU General Public License
0016     along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017  */
0018 
0019 function sendMessage(subsystem, action, payload) {
0020     let data = {
0021         subsystem: subsystem,
0022         action: action,
0023         payload: payload
0024     };
0025 
0026     if (chrome.runtime && chrome.runtime.sendMessage) {
0027         return new Promise((resolve, reject) => {
0028             chrome.runtime.sendMessage(data, (reply) => {
0029                 if (chrome.runtime.lastError) {
0030                     if (chrome.runtime.lastError.message === "The message port closed before a response was received.") {
0031                         resolve();
0032                     } else {
0033                         reject(chrome.runtime.lastError);
0034                     }
0035                     return;
0036                 }
0037 
0038                 if (reply && reply.rejected) {
0039                     reject(reply);
0040                 } else {
0041                     resolve(reply);
0042                 }
0043             });
0044         });
0045     }
0046 
0047     return browser.runtime.sendMessage(data);
0048 }