[epiphany/pgriffis/web-extension-beastify: 7/12] WebExtension: Update tabs.insertCSS/removeCSS to respect tabId




commit 784adf97f724e423a205d8a95b2a70820336af29
Author: Patrick Griffis <pgriffis igalia com>
Date:   Sun May 22 18:45:07 2022 -0500

    WebExtension: Update tabs.insertCSS/removeCSS to respect tabId
    
    Part-of: <https://gitlab.gnome.org/GNOME/epiphany/-/merge_requests/1116>

 .../resources/js/webextensions.js                  |  4 +-
 src/webextension/api/tabs.c                        | 58 ++++++++++++++++++++--
 2 files changed, 56 insertions(+), 6 deletions(-)
---
diff --git a/embed/web-process-extension/resources/js/webextensions.js 
b/embed/web-process-extension/resources/js/webextensions.js
index 4c1e221e0..f0edce2ce 100644
--- a/embed/web-process-extension/resources/js/webextensions.js
+++ b/embed/web-process-extension/resources/js/webextensions.js
@@ -41,8 +41,8 @@ window.browser.tabs = {
     executeScript: function (...args) { return ephy_message ('tabs.executeScript', args, null); },
     query: function (args, cb) { return ephy_message ('tabs.query', args, cb); },
     get: function (args, cb) { return ephy_message ('tabs.get', args, cb); },
-    insertCSS: function (args, cb) { return ephy_message ('tabs.insertCSS', args, cb); },
-    removeCSS: function (args, cb) { return ephy_message ('tabs.removeCSS', args, cb); },
+    insertCSS: function (...args) { return ephy_message ('tabs.insertCSS', args, null); },
+    removeCSS: function (...args) { return ephy_message ('tabs.removeCSS', args, null); },
     onUpdated: {
       addListener: function (cb) { tabs_listeners.push({callback: cb}); }
     },
diff --git a/src/webextension/api/tabs.c b/src/webextension/api/tabs.c
index 558762ab2..1804c5501 100644
--- a/src/webextension/api/tabs.c
+++ b/src/webextension/api/tabs.c
@@ -185,11 +185,36 @@ tabs_handler_insert_css (EphyWebExtension *self,
                          JSCValue         *args)
 {
   EphyShell *shell = ephy_shell_get_default ();
-  WebKitUserContentManager *ucm = webkit_web_view_get_user_content_manager (WEBKIT_WEB_VIEW 
(ephy_shell_get_active_web_view (shell)));
+  WebKitUserContentManager *ucm;
   WebKitUserStyleSheet *css = NULL;
   g_autoptr (JSCValue) code = NULL;
+  g_autoptr (JSCValue) obj = NULL;
+  g_autoptr (JSCValue) tab_id_value = NULL;
+  WebKitWebView *target_web_view;
+
+  /* This takes an optional first argument so it's either:
+   * [tabId:int, details:obj], or [details:obj] */
+  if (!jsc_value_is_array (args))
+    return NULL;
+
+  obj = jsc_value_object_get_property_at_index (args, 0);
+  if (!jsc_value_is_object (obj)) {
+    g_object_unref (obj);
+    tab_id_value = jsc_value_object_get_property_at_index (args, 0);
+    obj = jsc_value_object_get_property_at_index (args, 1);
+  }
+
+  if (!tab_id_value)
+    target_web_view = WEBKIT_WEB_VIEW (ephy_shell_get_active_web_view (shell));
+  else
+    target_web_view = get_web_view_for_tab_id (shell, jsc_value_to_int32 (tab_id_value), NULL);
+
+  if (!target_web_view)
+    return NULL;
+
+  ucm = webkit_web_view_get_user_content_manager (target_web_view);
 
-  code = jsc_value_object_get_property (args, "code");
+  code = jsc_value_object_get_property (obj, "code");
   css = ephy_web_extension_add_custom_css (self, jsc_value_to_string (code));
 
   if (css)
@@ -206,9 +231,34 @@ tabs_handler_remove_css (EphyWebExtension *self,
   EphyShell *shell = ephy_shell_get_default ();
   JSCValue *code;
   WebKitUserStyleSheet *css = NULL;
-  WebKitUserContentManager *ucm = webkit_web_view_get_user_content_manager (WEBKIT_WEB_VIEW 
(ephy_shell_get_active_web_view (shell)));
+  WebKitUserContentManager *ucm;
+  g_autoptr (JSCValue) obj = NULL;
+  g_autoptr (JSCValue) tab_id_value = NULL;
+  WebKitWebView *target_web_view;
+
+  /* This takes an optional first argument so it's either:
+   * [tabId:int, details:obj], or [details:obj] */
+  if (!jsc_value_is_array (args))
+    return NULL;
+
+  obj = jsc_value_object_get_property_at_index (args, 0);
+  if (!jsc_value_is_object (obj)) {
+    g_object_unref (obj);
+    tab_id_value = jsc_value_object_get_property_at_index (args, 0);
+    obj = jsc_value_object_get_property_at_index (args, 1);
+  }
+
+  if (!tab_id_value)
+    target_web_view = WEBKIT_WEB_VIEW (ephy_shell_get_active_web_view (shell));
+  else
+    target_web_view = get_web_view_for_tab_id (shell, jsc_value_to_int32 (tab_id_value), NULL);
+
+  if (!target_web_view)
+    return NULL;
+
+  ucm = webkit_web_view_get_user_content_manager (target_web_view);
 
-  code = jsc_value_object_get_property (args, "code");
+  code = jsc_value_object_get_property (obj, "code");
   css = ephy_web_extension_get_custom_css (self, jsc_value_to_string (code));
   if (css)
     webkit_user_content_manager_remove_style_sheet (ucm, css);


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