]> git.armaanb.net Git - chorizo.git/blobdiff - browser.c
lariza.usage.1: Fix erroneous dots and wrapping
[chorizo.git] / browser.c
index 3b62ef3a42eaddcc345b5393d127be9f270daea6..11e34da963ff17c7876f3f3df08a918cf9661183 100644 (file)
--- a/browser.c
+++ b/browser.c
@@ -48,6 +48,7 @@ static gboolean menu_web_view(WebKitWebView *, WebKitContextMenu *, GdkEvent *,
                               WebKitHitTestResult *, gpointer);
 static gboolean quit_if_nothing_active(void);
 static gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
+static void run_user_scripts(WebKitWebView *);
 static void search(gpointer, gint);
 static void show_web_view(WebKitWebView *, gpointer);
 static Window tabbed_launch(void);
@@ -79,7 +80,6 @@ static gboolean cooperative_alone = TRUE;
 static gboolean cooperative_instances = TRUE;
 static int cooperative_pipe_fp = 0;
 static gchar *download_dir = "/var/tmp";
-static gboolean enable_webgl = FALSE;
 static gboolean enable_console_to_stdout = FALSE;
 static Window embed = 0;
 static gchar *fifo_suffix = "main";
@@ -92,47 +92,6 @@ static gchar *search_text = NULL;
 static gboolean tabbed_automagic = TRUE;
 static gchar *user_agent = NULL;
 
-static gchar *feed_html_header =
-"<!DOCTYPE html>"
-"<html>"
-"    <head>"
-"        <meta charset=\"UTF-8\">"
-"        <title>Feeds</title>"
-"    </head>"
-"    <body>"
-"        <p>Feeds found on this page:</p>"
-"        <ul>"
-;
-
-static gchar *feed_html_footer =
-"        </ul>"
-"    </body>"
-"</html>"
-;
-
-static gchar *grab_feeds =
-"a = document.querySelectorAll('"
-"    html > head > link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
-"    html > head > link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
-"');"
-"if (a.length == 0)"
-"    null;"
-"else"
-"{"
-"    out = '';"
-"    for (i = 0; i < a.length; i++)"
-"    {"
-"        url = encodeURIComponent(a[i].href);"
-"        if ('title' in a[i] && a[i].title != '')"
-"            title = encodeURIComponent(a[i].title);"
-"        else"
-"            title = url;"
-"        out += '<li><a href=\"' + url + '\">' + title + '</a></li>';"
-"    }"
-"    out;"
-"}"
-;
-
 
 void
 client_destroy(GtkWidget *widget, gpointer data)
@@ -253,8 +212,7 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show)
     if (enable_console_to_stdout)
         webkit_settings_set_enable_write_console_messages_to_stdout(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
 
-    if (enable_webgl)
-        webkit_settings_set_enable_webgl(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
+    webkit_settings_set_enable_developer_extras(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
 
     c->location = gtk_entry_new();
     g_signal_connect(G_OBJECT(c->location), "key-press-event",
@@ -379,19 +337,42 @@ changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
 {
     struct Client *c = (struct Client *)data;
     gdouble p;
+    gchar *grab_feeds =
+        "a = document.querySelectorAll('"
+        "    html > head > link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
+        "    html > head > link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
+        "');"
+        "if (a.length == 0)"
+        "    null;"
+        "else"
+        "{"
+        "    out = '';"
+        "    for (i = 0; i < a.length; i++)"
+        "    {"
+        "        url = encodeURIComponent(a[i].href);"
+        "        if ('title' in a[i] && a[i].title != '')"
+        "            title = encodeURIComponent(a[i].title);"
+        "        else"
+        "            title = url;"
+        "        out += '<li><a href=\"' + url + '\">' + title + '</a></li>';"
+        "    }"
+        "    out;"
+        "}";
 
     p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
     if (p == 1)
     {
         p = 0;
 
-        /* The page has loaded fully. We now run a short JavaScript
-         * snippet that operates on the DOM. It tries to grab all
+        /* The page has loaded fully. We now run the short JavaScript
+         * snippet above that operates on the DOM. It tries to grab all
          * occurences of <link rel="alternate" ...>, i.e. RSS/Atom feed
          * references. */
         webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
                                        grab_feeds, NULL,
                                        grab_feeds_finished, c);
+
+        run_user_scripts(WEBKIT_WEB_VIEW(c->web_view));
     }
     gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
 }
@@ -668,10 +649,6 @@ grab_environment_configuration(void)
     if (e != NULL)
         enable_console_to_stdout = TRUE;
 
-    e = g_getenv(__NAME_UPPERCASE__"_ENABLE_EXPERIMENTAL_WEBGL");
-    if (e != NULL)
-        enable_webgl = TRUE;
-
     e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
     if (e != NULL)
         fifo_suffix = g_strdup(e);
@@ -698,17 +675,18 @@ grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
 {
     struct Client *c = (struct Client *)data;
     WebKitJavascriptResult *js_result;
-    JSValueRef value;
-    JSGlobalContextRef context;
+    JSCValue *value;
+    JSCException *exception;
     GError *err = NULL;
-    JSStringRef js_str_value;
-    gsize str_length;
+    gchar *str_value;
 
     g_free(c->feed_html);
     c->feed_html = NULL;
 
     /* This was taken almost verbatim from the example in WebKit's
-     * documentation. */
+     * documentation:
+     *
+     * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#webkit-web-view-run-javascript-finish */
 
     js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object),
                                                       result, &err);
@@ -719,16 +697,18 @@ grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
         return;
     }
 
-    context = webkit_javascript_result_get_global_context(js_result);
-    value = webkit_javascript_result_get_value(js_result);
-
-    if (JSValueIsString(context, value))
+    value = webkit_javascript_result_get_js_value(js_result);
+    if (jsc_value_is_string(value))
     {
-        js_str_value = JSValueToStringCopy(context, value, NULL);
-        str_length = JSStringGetMaximumUTF8CStringSize(js_str_value);
-        c->feed_html = (gchar *)g_malloc(str_length);
-        JSStringGetUTF8CString(js_str_value, c->feed_html, str_length);
-        JSStringRelease(js_str_value);
+        str_value = jsc_value_to_string(value);
+        exception = jsc_context_get_exception(jsc_value_get_context(value));
+        if (exception != NULL)
+        {
+            fprintf(stderr, __NAME__": Error running javascript: %s\n",
+                    jsc_exception_get_message(exception));
+        }
+        else
+            c->feed_html = str_value;
 
         gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
                                           GTK_ENTRY_ICON_PRIMARY,
@@ -777,10 +757,26 @@ hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers
 }
 
 void
-icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event, gpointer data)
+icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
+              gpointer data)
 {
     struct Client *c = (struct Client *)data;
     gchar *d;
+    gchar *data_template =
+        "data:text/html,"
+        "<!DOCTYPE html>"
+        "<html>"
+        "    <head>"
+        "        <meta charset=\"UTF-8\">"
+        "        <title>Feeds</title>"
+        "    </head>"
+        "    <body>"
+        "        <p>Feeds found on this page:</p>"
+        "        <ul>"
+        "        %s"
+        "        </ul>"
+        "    </body>"
+        "</html>";
 
     if (c->feed_html != NULL)
     {
@@ -797,10 +793,7 @@ icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event, g
          *
          * [0]: https://tools.ietf.org/html/rfc2397
          * [1]: https://en.wikipedia.org/wiki/Data_URI_scheme */
-        d = g_strdup_printf("data:text/html,%s%s%s",
-                            feed_html_header,
-                            c->feed_html,
-                            feed_html_footer);
+        d = g_strdup_printf(data_template, c->feed_html);
         webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
         g_free(d);
     }
@@ -1131,6 +1124,36 @@ remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
     return TRUE;
 }
 
+void
+run_user_scripts(WebKitWebView *web_view)
+{
+    gchar *base = NULL, *path = NULL, *contents = NULL;
+    const gchar *entry = NULL;
+    GDir *scriptdir = NULL;
+
+    base = g_build_filename(g_get_user_config_dir(), __NAME__, "user-scripts", NULL);
+    scriptdir = g_dir_open(base, 0, NULL);
+    if (scriptdir != NULL)
+    {
+        while ((entry = g_dir_read_name(scriptdir)) != NULL)
+        {
+            path = g_build_filename(base, entry, NULL);
+            if (g_str_has_suffix(path, ".js"))
+            {
+                if (g_file_get_contents(path, &contents, NULL, NULL))
+                {
+                    webkit_web_view_run_javascript(web_view, contents, NULL, NULL, NULL);
+                    g_free(contents);
+                }
+            }
+            g_free(path);
+        }
+        g_dir_close(scriptdir);
+    }
+
+    g_free(base);
+}
+
 void
 search(gpointer data, gint direction)
 {