[epiphany/pgriffis/web-extension-beastify: 3/6] WebExtensions: Fix tabs.executeScript




commit 25b9e6531b477f0a6fd825aa735a02decbf26742
Author: Patrick Griffis <pgriffis igalia com>
Date:   Fri May 20 13:00:21 2022 -0500

    WebExtensions: Fix tabs.executeScript

 src/webextension/api/tabs.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)
---
diff --git a/src/webextension/api/tabs.c b/src/webextension/api/tabs.c
index 739d96887..1fafb3dbf 100644
--- a/src/webextension/api/tabs.c
+++ b/src/webextension/api/tabs.c
@@ -156,20 +156,29 @@ tabs_handler_execute_script (EphyWebExtension *self,
   g_autofree char *code = NULL;
   EphyShell *shell = ephy_shell_get_default ();
 
-  if (jsc_value_is_array (args)) {
+  /* This takes an optional first argument so its either:
+   * [tabId:str, 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_warning ("tabs.executeScript doesn't currently support tabId and will run in the activeTab!");
+    g_object_unref (obj);
     obj = jsc_value_object_get_property_at_index (args, 1);
-  } else {
-    obj = g_object_ref (args);
   }
 
   file_value = jsc_value_object_get_property (obj, "file");
   code_value = jsc_value_object_get_property (obj, "code");
 
-  if (code_value)
+  if (jsc_value_is_string (code_value))
     code = jsc_value_to_string (code_value);
-  else if (file_value) {
-    g_autofree char *resource_path = jsc_value_to_string (code_value);
-    code = g_strdup (ephy_web_extension_get_resource_as_string (self, resource_path));
+  else if (jsc_value_is_string (file_value)) {
+    g_autofree char *resource_path = jsc_value_to_string (file_value);
+    code = ephy_web_extension_get_resource_as_string (self, resource_path[0] == '/' ? resource_path + 1 : 
resource_path);
+  } else {
+    g_warning ("tabs.executeScript called without usable code");
+    return NULL;
   }
 
   if (code) {


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