[epiphany/pgriffis/web-extension-beastify: 3/7] WebExtensions: Implement tabs.sendMessage




commit e4bc6ed73d0097fab8ff1ee7af1e3b0226dc5c1e
Author: Patrick Griffis <pgriffis igalia com>
Date:   Fri May 20 12:59:07 2022 -0500

    WebExtensions: Implement tabs.sendMessage
    
    Part-of: <https://gitlab.gnome.org/GNOME/epiphany/-/merge_requests/1114>

 .../resources/js/webextensions.js                  |  3 +-
 src/webextension/api/tabs.c                        | 34 ++++++++++++++++++++++
 2 files changed, 36 insertions(+), 1 deletion(-)
---
diff --git a/embed/web-process-extension/resources/js/webextensions.js 
b/embed/web-process-extension/resources/js/webextensions.js
index efae76f29..3f1eb4ce8 100644
--- a/embed/web-process-extension/resources/js/webextensions.js
+++ b/embed/web-process-extension/resources/js/webextensions.js
@@ -45,7 +45,8 @@ window.browser.tabs = {
     removeCSS: function (args, cb) { return ephy_message ('tabs.removeCSS', args, cb); },
     onUpdated: {
       addListener: function (cb) { tabs_listeners.push({callback: cb}); }
-    }
+    },
+    sendMessage: function (...args) { return ephy_message ('tabs.sendMessage', args, null); },
 };
 
 window.browser.notifications = {
diff --git a/src/webextension/api/tabs.c b/src/webextension/api/tabs.c
index 4f24fc357..739d96887 100644
--- a/src/webextension/api/tabs.c
+++ b/src/webextension/api/tabs.c
@@ -184,12 +184,46 @@ tabs_handler_execute_script (EphyWebExtension *self,
   return NULL;
 }
 
+static char *
+tabs_handler_send_message (EphyWebExtension *self,
+                           char             *name,
+                           JSCValue         *args)
+{
+  g_autoptr (JSCValue) message_value = NULL;
+  g_autofree char *serialized_message = NULL;
+  g_autofree char *code = NULL;
+  EphyShell *shell = ephy_shell_get_default ();
+
+  if (!jsc_value_is_array (args))
+    return NULL;
+
+  message_value = jsc_value_object_get_property_at_index (args, 1);
+  if (!message_value)
+    return NULL;
+
+  serialized_message = jsc_value_to_json (message_value, 0);
+  code = g_strdup_printf ("runtimeSendMessage(JSON.parse('%s'));", serialized_message);
+
+  g_warning ("tabs.sendMessage doesn't currently support tabId and will run in the activeTab!");
+
+  webkit_web_view_run_javascript_in_world (WEBKIT_WEB_VIEW (ephy_shell_get_active_web_view (shell)),
+                                           code,
+                                           ephy_web_extension_get_guid (self),
+                                           NULL,
+                                           NULL,
+                                           NULL);
+
+  /* FIXME: Return message response. */
+  return NULL;
+}
+
 static EphyWebExtensionApiHandler tabs_handlers[] = {
   {"query", tabs_handler_query},
   {"insertCSS", tabs_handler_insert_css},
   {"removeCSS", tabs_handler_remove_css},
   {"get", tabs_handler_get},
   {"executeScript", tabs_handler_execute_script},
+  {"sendMessage", tabs_handler_send_message},
   {NULL, NULL},
 };
 


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