Implement the Userstyles Addon panel

This commit is contained in:
Christian Dywan 2008-08-18 01:24:26 +02:00
parent b9e19d79d6
commit 9bf1b23feb
2 changed files with 64 additions and 8 deletions

View file

@ -597,6 +597,51 @@ _js_script_from_file (JSContextRef js_context,
return result; return result;
} }
static gboolean
_js_style_from_file (JSContextRef js_context,
const gchar* filename,
gchar** exception)
{
gboolean result = FALSE;
gchar* style;
GError* error = NULL;
guint i, n;
gchar* style_script;
if (g_file_get_contents (filename, &style, NULL, &error))
{
n = strlen (style);
for (i = 0; i < n; i++)
{
/* Replace line breaks with spaces */
if (style[i] == '\n' || style[i] == '\r')
style[i] = ' ';
/* Change all single quotes to double quotes */
if (style[i] == '\'')
style[i] = '\"';
}
style_script = g_strdup_printf (
"window.addEventListener ('DOMContentLoaded',"
"function () {"
"var mystyle = document.createElement(\"style\");"
"mystyle.setAttribute(\"type\", \"text/css\");"
"mystyle.appendChild(document.createTextNode('%s'));"
"document.getElementsByTagName(\"head\")[0].appendChild(mystyle);"
"}, true);",
style);
if (gjs_script_eval (js_context, style_script, exception))
result = TRUE;
g_free (style_script);
g_free (style);
}
else
{
*exception = g_strdup (error->message);
g_error_free (error);
}
return result;
}
static void static void
midori_web_widget_window_object_cleared_cb (GtkWidget* web_widget, midori_web_widget_window_object_cleared_cb (GtkWidget* web_widget,
WebKitWebFrame* web_frame, WebKitWebFrame* web_frame,
@ -617,28 +662,38 @@ midori_web_widget_window_object_cleared_cb (GtkWidget* web_widget,
if (!uri) if (!uri)
return; return;
addon_files = _addons_get_files(addons); addon_files = _addons_get_files (addons);
list = addon_files; list = addon_files;
while (addon_files) while (addon_files)
{ {
fullname = addon_files->data; fullname = addon_files->data;
includes = NULL; includes = NULL;
excludes = NULL; excludes = NULL;
if (!_metadata_from_file (fullname, &includes, &excludes, NULL, NULL)) if (addons->kind == MIDORI_ADDON_USER_SCRIPTS &&
!_metadata_from_file (fullname, &includes, &excludes, NULL, NULL))
{ {
addon_files = addon_files->next; addon_files = g_slist_next (addon_files);
continue; continue;
} }
if (includes || excludes) if (includes || excludes)
if (!_may_load_script (uri, &includes, &excludes)) if (!_may_load_script (uri, &includes, &excludes))
{ {
addon_files = addon_files->next; addon_files = g_slist_next (addon_files);
continue; continue;
} }
g_slist_free (includes); g_slist_free (includes);
g_slist_free (excludes); g_slist_free (excludes);
exception = NULL; exception = NULL;
if (!_js_script_from_file (js_context, fullname, &exception)) if (addons->kind == MIDORI_ADDON_USER_SCRIPTS &&
!_js_script_from_file (js_context, fullname, &exception))
{
message = g_strdup_printf ("console.error ('%s');", exception);
gjs_script_eval (js_context, message, NULL);
g_free (message);
g_free (exception);
}
else if (addons->kind == MIDORI_ADDON_USER_STYLES &&
!_js_style_from_file (js_context, fullname, &exception))
{ {
message = g_strdup_printf ("console.error ('%s');", exception); message = g_strdup_printf ("console.error ('%s');", exception);
gjs_script_eval (js_context, message, NULL); gjs_script_eval (js_context, message, NULL);
@ -745,7 +800,8 @@ midori_addons_set_kind (MidoriAddons* addons,
addons->kind = kind; addons->kind = kind;
if (kind == MIDORI_ADDON_USER_SCRIPTS) if (kind == MIDORI_ADDON_USER_SCRIPTS
|| kind == MIDORI_ADDON_USER_STYLES)
g_signal_connect (addons->web_widget, "window-object-cleared", g_signal_connect (addons->web_widget, "window-object-cleared",
G_CALLBACK (midori_web_widget_window_object_cleared_cb), addons); G_CALLBACK (midori_web_widget_window_object_cleared_cb), addons);

View file

@ -3488,13 +3488,13 @@ midori_browser_init (MidoriBrowser* browser)
panel, toolbar, panel, toolbar,
STOCK_SCRIPTS, _("Userscripts")); STOCK_SCRIPTS, _("Userscripts"));
/* Userstyles */ /* Userstyles */
/*panel = midori_addons_new (GTK_WIDGET (browser), MIDORI_ADDON_USER_STYLES); panel = midori_addons_new (GTK_WIDGET (browser), MIDORI_ADDON_USER_STYLES);
gtk_widget_show (panel); gtk_widget_show (panel);
toolbar = midori_addons_get_toolbar (MIDORI_ADDONS (panel)); toolbar = midori_addons_get_toolbar (MIDORI_ADDONS (panel));
gtk_widget_show (toolbar); gtk_widget_show (toolbar);
midori_panel_append_page (MIDORI_PANEL (browser->panel), midori_panel_append_page (MIDORI_PANEL (browser->panel),
panel, toolbar, panel, toolbar,
STOCK_STYLES, _("Userstyles"));*/ STOCK_STYLES, _("Userstyles"));
/* Extensions */ /* Extensions */
panel = midori_addons_new (GTK_WIDGET (browser), MIDORI_ADDON_EXTENSIONS); panel = midori_addons_new (GTK_WIDGET (browser), MIDORI_ADDON_EXTENSIONS);