[epiphany/pgriffis/web-extension/runtime-send-message: 12/15] WebExtensions: Implement compat with chrome.extension.lastError




commit 51f518a55ea5350e3ba616151c34c360b7c9084c
Author: Patrick Griffis <pgriffis igalia com>
Date:   Thu Jun 2 10:25:59 2022 -0500

    WebExtensions: Implement compat with chrome.extension.lastError
    
    Part-of: <https://gitlab.gnome.org/GNOME/epiphany/-/merge_requests/1122>

 .../resources/js/webextensions-common.js                  | 15 +++++++++++++--
 embed/web-process-extension/resources/js/webextensions.js |  1 +
 2 files changed, 14 insertions(+), 2 deletions(-)
---
diff --git a/embed/web-process-extension/resources/js/webextensions-common.js 
b/embed/web-process-extension/resources/js/webextensions-common.js
index a3b03457a..411411bad 100644
--- a/embed/web-process-extension/resources/js/webextensions-common.js
+++ b/embed/web-process-extension/resources/js/webextensions-common.js
@@ -36,7 +36,7 @@ class EphyEventListener {
         for (const listener of this._listeners) {
             const ret = listener.callback (message, sender, reply_callback);
             if (typeof ret === 'object' && typeof ret.then === 'function') {
-                ret.then(x => { reply_callback(x); }).catch(x => { reply_callback(); })
+                ret.then(x => { reply_callback(x); }).catch(x => { reply_callback(); });
                 handled = true;
             } else if (ret === true) {
                 // We expect listener.callback to call `reply_callback`.
@@ -57,11 +57,21 @@ const ephy_message = function (fn, args) {
 
     return new Promise (function (resolve, reject) {
         const resolve_wrapper = function (x) {
+            window.browser.extension.lastError = null;
             if (callback !== undefined)
                 callback (x);
             resolve (x);
         };
-        ephy_send_message (fn, args, resolve_wrapper, reject);
+        const reject_wrapper = function (x) {
+            if (callback !== undefined) {
+                window.browser.extension.lastError = new Error(x);
+                callback ();
+                return;
+            }
+            reject(x);
+        };
+
+        ephy_send_message (fn, args, resolve_wrapper, reject_wrapper);
     });
 };
 
@@ -73,6 +83,7 @@ window.browser.runtime = {
     sendMessage: function (...args) {
         return ephy_message ('runtime.sendMessage', args);
     },
+    lastError: null,
 };
 
 
diff --git a/embed/web-process-extension/resources/js/webextensions.js 
b/embed/web-process-extension/resources/js/webextensions.js
index 0577555ad..c82b807f2 100644
--- a/embed/web-process-extension/resources/js/webextensions.js
+++ b/embed/web-process-extension/resources/js/webextensions.js
@@ -65,6 +65,7 @@ window.browser.runtime.onInstalled = new EphyEventListener ();
 window.browser.runtime.onMessageExternal = new EphyEventListener ();
 window.browser.runtime.sendNativeMessage = function (...args) { return ephy_message 
('runtime.sendNativeMessage', args); };
 window.browser.runtime.getBackgroundPage = window.browser.extension.getBackgroundPage;
+Object.defineProperty(window.browser.runtime, 'lastError', { get: function() { return 
window.browser.extension.lastError; } });
 
 
 window.browser.pageAction = {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]