[Xfce4-commits] <midori:master> Implement "Readable" which applies readable.css

Christian Dywan noreply at xfce.org
Thu Jul 5 21:58:01 CEST 2012


Updating branch refs/heads/master
         to 66a47613b0d810142d6e7d9793562f112e29fd25 (commit)
       from d319d4041642eac95974b46e67a05d4b26bccab3 (commit)

commit 66a47613b0d810142d6e7d9793562f112e29fd25
Author: Christian Dywan <christian at twotoasts.de>
Date:   Thu Jul 5 21:56:40 2012 +0200

    Implement "Readable" which applies readable.css
    
    The initial code comes with no stylesheet.

 midori/midori-browser.c |   65 +++++++++++++++++++++++++++++++++++++++++++++++
 midori/midori-view.c    |    7 +++--
 2 files changed, 69 insertions(+), 3 deletions(-)

diff --git a/midori/midori-browser.c b/midori/midori-browser.c
index 7c64822..b6d3e5c 100644
--- a/midori/midori-browser.c
+++ b/midori/midori-browser.c
@@ -13,6 +13,7 @@
 
 #include "midori-browser.h"
 
+#include "midori-app.h"
 #include "midori-array.h"
 #include "midori-view.h"
 #include "midori-preferences.h"
@@ -3609,6 +3610,66 @@ _action_scroll_somewhere_activate (GtkAction*     action,
         webkit_web_view_move_cursor (web_view, GTK_MOVEMENT_VISUAL_POSITIONS, 1);
 }
 
+static void
+_action_readable_activate (GtkAction*     action,
+                           MidoriBrowser* browser)
+{
+    GtkWidget* view = midori_browser_get_current_tab (browser);
+    gchar* filename;
+    gchar* stylesheet;
+    gint i;
+    gchar* script;
+    gchar* exception;
+
+    if (!view)
+        return;
+
+    filename = midori_app_find_res_filename ("readable.css");
+    stylesheet = NULL;
+    if (!g_file_get_contents (filename, &stylesheet, NULL, NULL))
+    {
+        g_free (filename);
+        g_free (stylesheet);
+        midori_view_add_info_bar (MIDORI_VIEW (view), GTK_MESSAGE_ERROR,
+            "Stylesheet readable.css not found", NULL, view,
+            GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
+        return;
+    }
+
+    i = 0;
+    while (stylesheet[i])
+    {
+        /* Replace line breaks with spaces */
+        if (stylesheet[i] == '\n' || stylesheet[i] == '\r')
+            stylesheet[i] = ' ';
+        /* Change all single quotes to double quotes */
+        else if (stylesheet[i] == '\'')
+            stylesheet[i] = '\"';
+        i++;
+    }
+
+    script = g_strdup_printf (
+        "(function () {"
+        "var style = document.createElement ('style');"
+        "style.setAttribute ('type', 'text/css');"
+        "style.appendChild (document.createTextNode ('%s'));"
+        "var head = document.getElementsByTagName ('head')[0];"
+        "if (head) head.appendChild (style);"
+        "else document.documentElement.insertBefore"
+        "(style, document.documentElement.firstChild);"
+        "}) ();", stylesheet);
+    g_free (stylesheet);
+    exception = NULL;
+    if (!midori_view_execute_script (MIDORI_VIEW (view), script, &exception))
+    {
+        midori_view_add_info_bar (MIDORI_VIEW (view), GTK_MESSAGE_ERROR,
+            exception, NULL, view,
+            GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
+        g_free (exception);
+    }
+    g_free (script);
+}
+
 static gboolean
 _action_navigation_activate (GtkAction*     action,
                              MidoriBrowser* browser)
@@ -5447,6 +5508,9 @@ static const GtkActionEntry entries[] =
     { "ScrollRight", NULL,
         N_("Scroll _Right"), "l",
         NULL, G_CALLBACK (_action_scroll_somewhere_activate) },
+    { "Readable", NULL,
+       N_("_Readable"), "<Ctrl><Alt>R",
+        NULL, G_CALLBACK (_action_readable_activate) },
 
     { "Go", NULL, N_("_Go") },
     { "Back", GTK_STOCK_GO_BACK,
@@ -5756,6 +5820,7 @@ static const gchar* ui_markup =
                 "</menu>"
                 "<menuitem action='SourceView'/>"
                 "<menuitem action='Fullscreen'/>"
+                "<menuitem action='Readable'/>"
             "</menu>"
             "<menu action='Go'>"
                 "<menuitem action='Back'/>"
diff --git a/midori/midori-view.c b/midori/midori-view.c
index 4f67687..3095712 100644
--- a/midori/midori-view.c
+++ b/midori/midori-view.c
@@ -1218,7 +1218,8 @@ midori_view_infobar_response_cb (GtkWidget* infobar,
 {
     void (*response_cb) (GtkWidget*, gint, gpointer);
     response_cb = g_object_get_data (G_OBJECT (infobar), "midori-infobar-cb");
-    response_cb (infobar, response, data_object);
+    if (response_cb != NULL)
+        response_cb (infobar, response, data_object);
     gtk_widget_destroy (infobar);
 }
 #else
@@ -1230,7 +1231,8 @@ midori_view_info_bar_button_cb (GtkWidget* button,
     gint response = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (button), "midori-infobar-response"));
     void (*response_cb) (GtkWidget*, gint, gpointer);
     response_cb = g_object_get_data (G_OBJECT (infobar), "midori-infobar-cb");
-    response_cb (infobar, response, data_object);
+    if (response_cb != NULL)
+        response_cb (infobar, response, data_object);
     gtk_widget_destroy (infobar);
 }
 #endif
@@ -1270,7 +1272,6 @@ midori_view_add_info_bar (MidoriView*    view,
     const gchar* button_text;
 
     g_return_val_if_fail (message != NULL, NULL);
-    g_return_val_if_fail (response_cb != NULL, NULL);
 
     va_start (args, first_button_text);
 


More information about the Xfce4-commits mailing list