Implement "Readable" which applies readable.css

The initial code comes with no stylesheet.
This commit is contained in:
Christian Dywan 2012-07-05 21:56:40 +02:00
parent d319d40416
commit 66a47613b0
2 changed files with 69 additions and 3 deletions

View file

@ -13,6 +13,7 @@
#include "midori-browser.h" #include "midori-browser.h"
#include "midori-app.h"
#include "midori-array.h" #include "midori-array.h"
#include "midori-view.h" #include "midori-view.h"
#include "midori-preferences.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); 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 static gboolean
_action_navigation_activate (GtkAction* action, _action_navigation_activate (GtkAction* action,
MidoriBrowser* browser) MidoriBrowser* browser)
@ -5447,6 +5508,9 @@ static const GtkActionEntry entries[] =
{ "ScrollRight", NULL, { "ScrollRight", NULL,
N_("Scroll _Right"), "l", N_("Scroll _Right"), "l",
NULL, G_CALLBACK (_action_scroll_somewhere_activate) }, NULL, G_CALLBACK (_action_scroll_somewhere_activate) },
{ "Readable", NULL,
N_("_Readable"), "<Ctrl><Alt>R",
NULL, G_CALLBACK (_action_readable_activate) },
{ "Go", NULL, N_("_Go") }, { "Go", NULL, N_("_Go") },
{ "Back", GTK_STOCK_GO_BACK, { "Back", GTK_STOCK_GO_BACK,
@ -5756,6 +5820,7 @@ static const gchar* ui_markup =
"</menu>" "</menu>"
"<menuitem action='SourceView'/>" "<menuitem action='SourceView'/>"
"<menuitem action='Fullscreen'/>" "<menuitem action='Fullscreen'/>"
"<menuitem action='Readable'/>"
"</menu>" "</menu>"
"<menu action='Go'>" "<menu action='Go'>"
"<menuitem action='Back'/>" "<menuitem action='Back'/>"

View file

@ -1218,6 +1218,7 @@ midori_view_infobar_response_cb (GtkWidget* infobar,
{ {
void (*response_cb) (GtkWidget*, gint, gpointer); void (*response_cb) (GtkWidget*, gint, gpointer);
response_cb = g_object_get_data (G_OBJECT (infobar), "midori-infobar-cb"); response_cb = g_object_get_data (G_OBJECT (infobar), "midori-infobar-cb");
if (response_cb != NULL)
response_cb (infobar, response, data_object); response_cb (infobar, response, data_object);
gtk_widget_destroy (infobar); gtk_widget_destroy (infobar);
} }
@ -1230,6 +1231,7 @@ midori_view_info_bar_button_cb (GtkWidget* button,
gint response = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (button), "midori-infobar-response")); gint response = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (button), "midori-infobar-response"));
void (*response_cb) (GtkWidget*, gint, gpointer); void (*response_cb) (GtkWidget*, gint, gpointer);
response_cb = g_object_get_data (G_OBJECT (infobar), "midori-infobar-cb"); response_cb = g_object_get_data (G_OBJECT (infobar), "midori-infobar-cb");
if (response_cb != NULL)
response_cb (infobar, response, data_object); response_cb (infobar, response, data_object);
gtk_widget_destroy (infobar); gtk_widget_destroy (infobar);
} }
@ -1270,7 +1272,6 @@ midori_view_add_info_bar (MidoriView* view,
const gchar* button_text; const gchar* button_text;
g_return_val_if_fail (message != NULL, NULL); g_return_val_if_fail (message != NULL, NULL);
g_return_val_if_fail (response_cb != NULL, NULL);
va_start (args, first_button_text); va_start (args, first_button_text);