[epiphany/mcatanzaro/adblocker-tweaks: 18/18] web-extension: log to JS console when blocking resources



commit 5426fa9c07ae5bcb9493cf7018c88687bc08bbec
Author: Michael Catanzaro <mcatanzaro igalia com>
Date:   Mon May 6 14:08:24 2019 -0500

    web-extension: log to JS console when blocking resources
    
    This will make it easier to see what the adblocker is blocking.

 embed/web-extension/ephy-web-extension.c | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)
---
diff --git a/embed/web-extension/ephy-web-extension.c b/embed/web-extension/ephy-web-extension.c
index c03949bc4..88367b458 100644
--- a/embed/web-extension/ephy-web-extension.c
+++ b/embed/web-extension/ephy-web-extension.c
@@ -34,6 +34,7 @@
 
 #include <gio/gio.h>
 #include <glib/gi18n.h>
+#include <glib/gprintf.h>
 #include <gtk/gtk.h>
 #include <jsc/jsc.h>
 #include <libsoup/soup.h>
@@ -99,6 +100,34 @@ static const char introspection_xml[] =
 
 G_DEFINE_TYPE (EphyWebExtension, ephy_web_extension, G_TYPE_OBJECT)
 
+static void
+log_to_js_console (EphyWebExtension *extension,
+                   WebKitWebPage    *page,
+                   const char       *str,
+                   ...)
+{
+  g_autoptr(JSCContext) js_context = NULL;
+  g_autoptr(JSCValue) js_console = NULL;
+  g_autoptr(JSCValue) js_result = NULL;
+  g_autofree char *expanded_str = NULL;
+  WebKitFrame *frame;
+  va_list args;
+  int ret;
+
+  va_start (args, str);
+  ret = g_vasprintf (&expanded_str, str, args);
+  g_assert (ret > 0);
+  va_end (args);
+
+  frame = webkit_web_page_get_main_frame (page);
+  js_context = webkit_frame_get_js_context_for_script_world (frame, extension->script_world);
+  js_console = jsc_context_get_value (js_context, "console");
+  js_result = jsc_value_object_invoke_method (js_console,
+                                              "log",
+                                              G_TYPE_STRING, expanded_str,
+                                              G_TYPE_NONE);
+}
+
 static gboolean
 should_use_adblocker (const char       *request_uri,
                       const char       *page_uri,
@@ -175,7 +204,8 @@ web_page_send_request (WebKitWebPage     *web_page,
                                              modified_uri ? modified_uri : request_uri,
                                              page_uri);
     if (!result) {
-      LOG ("Refused to load %s", request_uri);
+      LOG ("Adblocker refused to load %s", request_uri);
+      log_to_js_console (extension, web_page, _("Epiphany adblocker refused to load %s"), request_uri);
       return TRUE;
     }
   }


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