2009-05-19 19:01:09 +00:00
|
|
|
/*
|
2012-01-08 00:28:42 +00:00
|
|
|
Copyright (C) 2009-2012 Christian Dywan <christian@twotoasts.de>
|
|
|
|
Copyright (C) 2009-2012 Alexander Butenko <a.butenka@gmail.com>
|
2009-05-19 19:01:09 +00:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
See the file COPYING for the full license text.
|
|
|
|
*/
|
|
|
|
#include <midori/midori.h>
|
2012-08-27 19:49:36 +00:00
|
|
|
#include "midori-core.h"
|
2011-10-31 21:10:39 +00:00
|
|
|
#include <glib/gstdio.h>
|
2009-05-19 19:01:09 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
2009-07-18 13:14:26 +00:00
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2009-05-19 19:01:09 +00:00
|
|
|
|
2009-12-18 18:56:10 +00:00
|
|
|
#define SIGNATURE_SIZE 8
|
|
|
|
#define USE_PATTERN_MATCHING 1
|
2010-01-02 02:47:56 +00:00
|
|
|
#define CUSTOM_LIST_NAME "custom.list"
|
2009-10-14 01:33:05 +00:00
|
|
|
#define ADBLOCK_FILTER_VALID(__filter) \
|
|
|
|
(__filter && (g_str_has_prefix (__filter, "http") \
|
|
|
|
|| g_str_has_prefix (__filter, "file")))
|
2010-04-05 21:20:45 +00:00
|
|
|
#define ADBLOCK_FILTER_SET(__filter,__active) \
|
2011-05-12 15:30:26 +00:00
|
|
|
__filter[4] = __active ? (__filter[5] == ':' ? 's' : ':') : '-'
|
2010-04-05 21:20:45 +00:00
|
|
|
#define ADBLOCK_FILTER_IS_SET(__filter) \
|
2011-05-12 15:30:26 +00:00
|
|
|
(__filter[4] != '-' && __filter[5] != '-')
|
2010-04-08 19:39:05 +00:00
|
|
|
#ifdef G_ENABLE_DEBUG
|
|
|
|
#define adblock_debug(dmsg, darg1, darg2) \
|
2012-07-19 18:04:25 +00:00
|
|
|
do { if (midori_debug ("adblock:1")) g_debug (dmsg, darg1, darg2); } while (0)
|
2010-04-08 19:39:05 +00:00
|
|
|
#else
|
|
|
|
#define adblock_debug(dmsg, darg1, darg2) /* nothing */
|
|
|
|
#endif
|
2009-10-14 01:33:05 +00:00
|
|
|
|
2011-12-18 14:52:31 +00:00
|
|
|
static GHashTable* pattern = NULL;
|
|
|
|
static GHashTable* keys = NULL;
|
|
|
|
static GHashTable* optslist = NULL;
|
|
|
|
static GHashTable* urlcache = NULL;
|
|
|
|
static GHashTable* blockcssprivate = NULL;
|
2012-04-12 21:17:02 +00:00
|
|
|
static GHashTable* navigationwhitelist = NULL;
|
2011-12-18 14:52:31 +00:00
|
|
|
static GString* blockcss = NULL;
|
2012-09-07 17:51:32 +00:00
|
|
|
static GList* update_list = NULL;
|
|
|
|
static gboolean update_done = FALSE;
|
2009-09-09 21:53:30 +00:00
|
|
|
|
2012-09-01 15:54:30 +00:00
|
|
|
static void
|
2009-09-23 17:22:01 +00:00
|
|
|
adblock_parse_file (gchar* path);
|
|
|
|
|
2012-08-31 23:57:17 +00:00
|
|
|
static gboolean
|
|
|
|
adblock_file_is_up_to_date (gchar* path);
|
|
|
|
|
2011-10-30 14:19:06 +00:00
|
|
|
static void
|
|
|
|
adblock_reload_rules (MidoriExtension* extension,
|
|
|
|
gboolean custom_only);
|
|
|
|
|
2009-10-19 21:16:05 +00:00
|
|
|
static gchar*
|
2011-12-18 01:07:55 +00:00
|
|
|
adblock_build_js (const gchar* uri)
|
2009-10-19 21:16:05 +00:00
|
|
|
{
|
2012-01-08 00:28:42 +00:00
|
|
|
gchar* domain;
|
|
|
|
const gchar* style;
|
2011-12-18 01:07:55 +00:00
|
|
|
GString* subdomain;
|
|
|
|
GString* code;
|
|
|
|
int cnt = 0, blockscnt = 0;
|
|
|
|
gchar** subdomains;
|
2012-01-08 00:28:42 +00:00
|
|
|
|
|
|
|
domain = midori_uri_parse_hostname (uri, NULL);
|
|
|
|
subdomains = g_strsplit (domain, ".", -1);
|
|
|
|
g_free (domain);
|
|
|
|
if (!subdomains)
|
|
|
|
return NULL;
|
2011-12-18 01:07:55 +00:00
|
|
|
|
|
|
|
code = g_string_new (
|
2009-10-19 21:16:05 +00:00
|
|
|
"window.addEventListener ('DOMContentLoaded',"
|
|
|
|
"function () {"
|
2009-12-23 14:35:09 +00:00
|
|
|
" if (document.getElementById('madblock'))"
|
|
|
|
" return;"
|
2011-12-18 01:07:55 +00:00
|
|
|
" public = '");
|
|
|
|
|
2012-01-08 00:28:42 +00:00
|
|
|
cnt = g_strv_length (subdomains) - 1;
|
2011-12-18 01:07:55 +00:00
|
|
|
subdomain = g_string_new (subdomains [cnt]);
|
|
|
|
g_string_prepend_c (subdomain, '.');
|
|
|
|
cnt--;
|
2012-01-08 00:28:42 +00:00
|
|
|
while (cnt >= 0)
|
2011-12-18 01:07:55 +00:00
|
|
|
{
|
|
|
|
g_string_prepend (subdomain, subdomains[cnt]);
|
2012-01-08 00:28:42 +00:00
|
|
|
if ((style = g_hash_table_lookup (blockcssprivate, subdomain->str)))
|
2011-12-18 01:07:55 +00:00
|
|
|
{
|
2012-01-08 00:28:42 +00:00
|
|
|
g_string_append (code, style);
|
2011-12-18 01:07:55 +00:00
|
|
|
g_string_append_c (code, ',');
|
|
|
|
blockscnt++;
|
|
|
|
}
|
|
|
|
g_string_prepend_c (subdomain, '.');
|
|
|
|
cnt--;
|
|
|
|
}
|
|
|
|
g_string_free (subdomain, TRUE);
|
|
|
|
g_strfreev (subdomains);
|
|
|
|
|
|
|
|
if (blockscnt == 0)
|
2012-01-08 00:28:42 +00:00
|
|
|
return g_string_free (code, TRUE);
|
2011-12-18 01:07:55 +00:00
|
|
|
|
|
|
|
g_string_append (code,
|
|
|
|
" zz-non-existent {display: none !important}';"
|
2009-11-20 00:00:48 +00:00
|
|
|
" var mystyle = document.createElement('style');"
|
|
|
|
" mystyle.setAttribute('type', 'text/css');"
|
2009-12-23 14:35:09 +00:00
|
|
|
" mystyle.setAttribute('id', 'madblock');"
|
2009-11-20 00:00:48 +00:00
|
|
|
" mystyle.appendChild(document.createTextNode(public));"
|
|
|
|
" var head = document.getElementsByTagName('head')[0];"
|
|
|
|
" if (head) head.appendChild(mystyle);"
|
2011-12-18 01:07:55 +00:00
|
|
|
"}, true);");
|
|
|
|
return g_string_free (code, FALSE);
|
2009-10-19 21:16:05 +00:00
|
|
|
}
|
|
|
|
|
2011-10-08 15:07:55 +00:00
|
|
|
static GString*
|
2011-06-06 14:30:30 +00:00
|
|
|
adblock_fixup_regexp (const gchar* prefix,
|
|
|
|
gchar* src);
|
2009-09-09 21:53:30 +00:00
|
|
|
|
2011-12-18 14:52:31 +00:00
|
|
|
static void
|
|
|
|
adblock_destroy_db ()
|
|
|
|
{
|
|
|
|
if (blockcss)
|
|
|
|
g_string_free (blockcss, TRUE);
|
|
|
|
blockcss = NULL;
|
|
|
|
|
|
|
|
g_hash_table_destroy (pattern);
|
|
|
|
pattern = NULL;
|
|
|
|
g_hash_table_destroy (optslist);
|
|
|
|
optslist = NULL;
|
|
|
|
g_hash_table_destroy (urlcache);
|
|
|
|
urlcache = NULL;
|
|
|
|
g_hash_table_destroy (blockcssprivate);
|
|
|
|
blockcssprivate = NULL;
|
2012-04-12 21:17:02 +00:00
|
|
|
g_hash_table_destroy (navigationwhitelist);
|
|
|
|
navigationwhitelist = NULL;
|
2011-12-18 14:52:31 +00:00
|
|
|
}
|
|
|
|
|
2009-12-18 18:56:10 +00:00
|
|
|
static void
|
|
|
|
adblock_init_db ()
|
|
|
|
{
|
|
|
|
pattern = g_hash_table_new_full (g_str_hash, g_str_equal,
|
|
|
|
(GDestroyNotify)g_free,
|
|
|
|
(GDestroyNotify)g_regex_unref);
|
|
|
|
keys = g_hash_table_new_full (g_str_hash, g_str_equal,
|
|
|
|
(GDestroyNotify)g_free,
|
|
|
|
(GDestroyNotify)g_regex_unref);
|
2011-07-02 18:11:24 +00:00
|
|
|
optslist = g_hash_table_new_full (g_str_hash, g_str_equal,
|
|
|
|
NULL,
|
|
|
|
(GDestroyNotify)g_free);
|
2011-05-29 02:57:31 +00:00
|
|
|
urlcache = g_hash_table_new_full (g_str_hash, g_str_equal,
|
|
|
|
(GDestroyNotify)g_free,
|
|
|
|
(GDestroyNotify)g_free);
|
2011-12-18 01:07:55 +00:00
|
|
|
blockcssprivate = g_hash_table_new_full (g_str_hash, g_str_equal,
|
|
|
|
(GDestroyNotify)g_free,
|
|
|
|
(GDestroyNotify)g_free);
|
2012-04-12 21:17:02 +00:00
|
|
|
navigationwhitelist = g_hash_table_new_full (g_direct_hash, g_str_equal,
|
|
|
|
NULL,
|
|
|
|
(GDestroyNotify)g_free);
|
2011-12-18 01:07:55 +00:00
|
|
|
|
2011-06-05 15:44:52 +00:00
|
|
|
if (blockcss && blockcss->len > 0)
|
|
|
|
g_string_free (blockcss, TRUE);
|
|
|
|
blockcss = g_string_new ("z-non-exist");
|
2009-07-22 19:42:33 +00:00
|
|
|
}
|
|
|
|
|
2009-09-23 17:22:01 +00:00
|
|
|
static void
|
2011-10-30 14:19:06 +00:00
|
|
|
adblock_download_notify_status_cb (WebKitDownload* download,
|
|
|
|
GParamSpec* pspec,
|
|
|
|
MidoriExtension* extension)
|
2009-09-23 17:22:01 +00:00
|
|
|
{
|
2012-09-07 17:51:32 +00:00
|
|
|
if (update_done)
|
2009-12-21 22:57:31 +00:00
|
|
|
return;
|
2012-09-07 17:51:32 +00:00
|
|
|
|
|
|
|
if (webkit_download_get_status (download) == WEBKIT_DOWNLOAD_STATUS_FINISHED)
|
|
|
|
{
|
|
|
|
GList* li = NULL;
|
|
|
|
for (li = update_list; li != NULL; li = g_list_next (li))
|
|
|
|
{
|
|
|
|
gchar* uri = g_strdup (webkit_download_get_destination_uri (download) + 7);
|
|
|
|
if (g_strcmp0 (li->data, uri))
|
|
|
|
update_list = g_list_remove (update_list, li->data);
|
|
|
|
g_free (uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_list_length (update_list) == 0)
|
|
|
|
{
|
|
|
|
adblock_reload_rules (extension, FALSE);
|
|
|
|
update_done = TRUE;
|
|
|
|
}
|
2009-09-23 17:22:01 +00:00
|
|
|
}
|
|
|
|
|
2010-02-03 17:26:44 +00:00
|
|
|
static gchar*
|
|
|
|
adblock_get_filename_for_uri (const gchar* uri)
|
|
|
|
{
|
|
|
|
gchar* filename;
|
|
|
|
gchar* folder;
|
|
|
|
gchar* path;
|
|
|
|
|
2011-05-12 15:30:26 +00:00
|
|
|
if (!ADBLOCK_FILTER_IS_SET (uri))
|
2010-02-03 17:26:44 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!strncmp (uri, "file", 4))
|
|
|
|
return g_strndup (uri + 7, strlen (uri) - 7);
|
|
|
|
|
2012-08-27 19:49:36 +00:00
|
|
|
folder = g_build_filename (midori_paths_get_cache_dir (), "adblock", NULL);
|
2010-02-03 17:26:44 +00:00
|
|
|
katze_mkdir_with_parents (folder, 0700);
|
|
|
|
|
|
|
|
filename = g_compute_checksum_for_string (G_CHECKSUM_MD5, uri, -1);
|
|
|
|
path = g_build_filename (folder, filename, NULL);
|
|
|
|
|
|
|
|
g_free (filename);
|
|
|
|
g_free (folder);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2009-09-23 17:22:01 +00:00
|
|
|
static void
|
2010-01-02 20:34:17 +00:00
|
|
|
adblock_reload_rules (MidoriExtension* extension,
|
|
|
|
gboolean custom_only)
|
2009-09-23 17:22:01 +00:00
|
|
|
{
|
2010-02-03 17:26:44 +00:00
|
|
|
gchar* path;
|
2010-01-02 20:34:17 +00:00
|
|
|
gchar* custom_list;
|
|
|
|
gchar** filters;
|
2009-09-23 17:22:01 +00:00
|
|
|
guint i = 0;
|
2011-11-09 20:41:54 +00:00
|
|
|
MidoriApp* app = midori_extension_get_app (extension);
|
|
|
|
MidoriWebSettings* settings = katze_object_get_object (app, "settings");
|
2009-12-21 22:57:31 +00:00
|
|
|
|
2011-12-18 14:52:31 +00:00
|
|
|
if (pattern)
|
|
|
|
adblock_destroy_db ();
|
2009-12-18 18:56:10 +00:00
|
|
|
adblock_init_db ();
|
2009-09-23 17:22:01 +00:00
|
|
|
|
2010-01-02 02:47:56 +00:00
|
|
|
custom_list = g_build_filename (midori_extension_get_config_dir (extension),
|
|
|
|
CUSTOM_LIST_NAME, NULL);
|
|
|
|
adblock_parse_file (custom_list);
|
|
|
|
g_free (custom_list);
|
2010-01-02 20:34:17 +00:00
|
|
|
|
|
|
|
filters = midori_extension_get_string_list (extension, "filters", NULL);
|
|
|
|
if (!custom_only && filters && *filters)
|
|
|
|
{
|
2010-02-03 17:26:44 +00:00
|
|
|
while (filters[i] != NULL)
|
2010-01-02 20:34:17 +00:00
|
|
|
{
|
2010-02-03 17:26:44 +00:00
|
|
|
path = adblock_get_filename_for_uri (filters[i]);
|
|
|
|
if (!path)
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-09-01 15:54:30 +00:00
|
|
|
if (!adblock_file_is_up_to_date (path))
|
2010-01-02 20:34:17 +00:00
|
|
|
{
|
|
|
|
WebKitNetworkRequest* request;
|
|
|
|
WebKitDownload* download;
|
|
|
|
gchar* destination = g_filename_to_uri (path, NULL, NULL);
|
|
|
|
|
2010-02-03 17:26:44 +00:00
|
|
|
request = webkit_network_request_new (filters[i]);
|
2010-01-02 20:34:17 +00:00
|
|
|
download = webkit_download_new (request);
|
|
|
|
g_object_unref (request);
|
|
|
|
webkit_download_set_destination_uri (download, destination);
|
2012-09-07 17:51:32 +00:00
|
|
|
update_list = g_list_prepend (update_list, path);
|
2010-01-02 20:34:17 +00:00
|
|
|
g_free (destination);
|
|
|
|
g_signal_connect (download, "notify::status",
|
2011-10-30 14:19:06 +00:00
|
|
|
G_CALLBACK (adblock_download_notify_status_cb), extension);
|
2010-01-02 20:34:17 +00:00
|
|
|
webkit_download_start (download);
|
|
|
|
}
|
2012-09-01 15:54:30 +00:00
|
|
|
else
|
|
|
|
adblock_parse_file (path);
|
2011-10-30 14:19:06 +00:00
|
|
|
g_free (path);
|
2010-02-03 17:26:44 +00:00
|
|
|
i++;
|
2010-01-02 20:34:17 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-23 17:22:01 +00:00
|
|
|
g_strfreev (filters);
|
2011-11-09 20:41:54 +00:00
|
|
|
g_string_append (blockcss, " {display: none !important}\n");
|
2010-01-02 20:34:17 +00:00
|
|
|
|
2011-10-30 14:19:06 +00:00
|
|
|
midori_web_settings_add_style (settings, "adblock-blockcss", blockcss->str);
|
|
|
|
g_object_unref (settings);
|
2009-09-23 17:22:01 +00:00
|
|
|
}
|
|
|
|
|
2009-10-14 01:33:05 +00:00
|
|
|
static void
|
|
|
|
adblock_preferences_render_tick_cb (GtkTreeViewColumn* column,
|
|
|
|
GtkCellRenderer* renderer,
|
|
|
|
GtkTreeModel* model,
|
|
|
|
GtkTreeIter* iter,
|
|
|
|
MidoriExtension* extension)
|
|
|
|
{
|
|
|
|
gchar* filter;
|
|
|
|
|
|
|
|
gtk_tree_model_get (model, iter, 0, &filter, -1);
|
|
|
|
|
|
|
|
g_object_set (renderer,
|
|
|
|
"activatable", ADBLOCK_FILTER_VALID (filter),
|
2010-04-05 21:20:45 +00:00
|
|
|
"active", ADBLOCK_FILTER_VALID (filter) && ADBLOCK_FILTER_IS_SET (filter),
|
2009-10-14 01:33:05 +00:00
|
|
|
NULL);
|
|
|
|
|
|
|
|
g_free (filter);
|
|
|
|
}
|
2009-07-24 21:55:57 +00:00
|
|
|
|
2009-07-17 19:37:36 +00:00
|
|
|
static void
|
|
|
|
adblock_preferences_renderer_text_edited_cb (GtkCellRenderer* renderer,
|
|
|
|
const gchar* tree_path,
|
|
|
|
const gchar* new_text,
|
|
|
|
GtkTreeModel* model)
|
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
|
|
|
|
if (gtk_tree_model_get_iter_from_string (model, &iter, tree_path))
|
|
|
|
gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, new_text, -1);
|
|
|
|
}
|
|
|
|
|
2009-10-14 01:33:05 +00:00
|
|
|
static void
|
2010-02-03 17:26:44 +00:00
|
|
|
adblock_preferences_renderer_toggle_toggled_cb (GtkCellRendererToggle* renderer,
|
|
|
|
const gchar* path,
|
|
|
|
GtkTreeModel* model)
|
2009-10-14 01:33:05 +00:00
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
|
|
|
|
if (gtk_tree_model_get_iter_from_string (model, &iter, path))
|
|
|
|
{
|
|
|
|
gchar* filter;
|
|
|
|
|
|
|
|
gtk_tree_model_get (model, &iter, 0, &filter, -1);
|
|
|
|
|
|
|
|
if (ADBLOCK_FILTER_VALID (filter))
|
|
|
|
{
|
2010-04-05 21:20:45 +00:00
|
|
|
ADBLOCK_FILTER_SET (filter, TRUE);
|
2010-02-03 17:26:44 +00:00
|
|
|
if (gtk_cell_renderer_toggle_get_active (renderer))
|
|
|
|
{
|
2011-10-28 20:47:17 +00:00
|
|
|
if (midori_uri_is_http (filter))
|
2010-02-03 17:26:44 +00:00
|
|
|
{
|
|
|
|
gchar* filename = adblock_get_filename_for_uri (filter);
|
|
|
|
g_unlink (filename);
|
|
|
|
g_free (filename);
|
|
|
|
}
|
2010-04-05 21:20:45 +00:00
|
|
|
ADBLOCK_FILTER_SET (filter, FALSE);
|
2010-02-03 17:26:44 +00:00
|
|
|
}
|
2009-10-14 01:33:05 +00:00
|
|
|
|
|
|
|
gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, filter, -1);
|
|
|
|
|
|
|
|
g_free (filter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
adblock_preferences_render_text_cb (GtkTreeViewColumn* column,
|
|
|
|
GtkCellRenderer* renderer,
|
|
|
|
GtkTreeModel* model,
|
|
|
|
GtkTreeIter* iter,
|
|
|
|
MidoriExtension* extension)
|
|
|
|
{
|
|
|
|
gchar* filter;
|
|
|
|
|
|
|
|
gtk_tree_model_get (model, iter, 0, &filter, -1);
|
|
|
|
|
|
|
|
if (ADBLOCK_FILTER_VALID (filter))
|
2010-04-05 21:20:45 +00:00
|
|
|
ADBLOCK_FILTER_SET (filter, TRUE);
|
2009-10-14 01:33:05 +00:00
|
|
|
|
|
|
|
g_object_set (renderer,
|
|
|
|
"text", filter,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
g_free (filter);
|
|
|
|
}
|
|
|
|
|
2009-07-17 19:37:36 +00:00
|
|
|
static void
|
|
|
|
adblock_preferences_model_row_changed_cb (GtkTreeModel* model,
|
|
|
|
GtkTreePath* path,
|
|
|
|
GtkTreeIter* iter,
|
|
|
|
MidoriExtension* extension)
|
|
|
|
{
|
|
|
|
gsize length = gtk_tree_model_iter_n_children (model, NULL);
|
|
|
|
gchar** filters = g_new (gchar*, length + 1);
|
|
|
|
guint i = 0;
|
2009-09-23 17:22:01 +00:00
|
|
|
gboolean need_reload = FALSE;
|
2009-07-17 19:37:36 +00:00
|
|
|
|
|
|
|
if (gtk_tree_model_iter_children (model, iter, NULL))
|
|
|
|
do
|
|
|
|
{
|
|
|
|
gchar* filter;
|
|
|
|
gtk_tree_model_get (model, iter, 0, &filter, -1);
|
2009-10-14 01:33:05 +00:00
|
|
|
if (filter && *filter)
|
2009-09-23 17:22:01 +00:00
|
|
|
{
|
|
|
|
filters[i++] = filter;
|
|
|
|
need_reload = TRUE;
|
|
|
|
}
|
2009-10-03 00:03:49 +00:00
|
|
|
else
|
|
|
|
g_free (filter);
|
2009-07-17 19:37:36 +00:00
|
|
|
}
|
|
|
|
while (gtk_tree_model_iter_next (model, iter));
|
2009-10-14 01:17:23 +00:00
|
|
|
filters[i] = NULL;
|
|
|
|
midori_extension_set_string_list (extension, "filters", filters, i);
|
2009-10-03 11:24:43 +00:00
|
|
|
g_free (filters);
|
2009-09-23 17:22:01 +00:00
|
|
|
if (need_reload)
|
2010-01-02 20:34:17 +00:00
|
|
|
adblock_reload_rules (extension, FALSE);
|
2009-07-17 19:37:36 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 20:05:20 +00:00
|
|
|
static void
|
|
|
|
adblock_preferences_model_row_deleted_cb (GtkTreeModel* model,
|
|
|
|
GtkTreePath* path,
|
|
|
|
MidoriExtension* extension)
|
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
adblock_preferences_model_row_changed_cb (model, path, &iter, extension);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
adblock_preferences_add_clicked_cb (GtkWidget* button,
|
|
|
|
GtkTreeModel* model)
|
|
|
|
{
|
2011-11-22 20:40:32 +00:00
|
|
|
GtkEntry* entry = GTK_IS_ENTRY (button)
|
|
|
|
? button : g_object_get_data (G_OBJECT (button), "entry");
|
2009-07-17 20:05:20 +00:00
|
|
|
gtk_list_store_insert_with_values (GTK_LIST_STORE (model),
|
2009-10-14 21:00:47 +00:00
|
|
|
NULL, 0, 0, gtk_entry_get_text (entry), -1);
|
|
|
|
gtk_entry_set_text (entry, "");
|
2009-07-17 20:05:20 +00:00
|
|
|
}
|
|
|
|
|
2009-11-20 19:53:41 +00:00
|
|
|
static void
|
|
|
|
adblock_preferences_edit_clicked_cb (GtkWidget* button,
|
|
|
|
GtkTreeViewColumn* column)
|
|
|
|
{
|
|
|
|
GdkEvent* event = gtk_get_current_event ();
|
|
|
|
GtkTreeView* treeview = g_object_get_data (G_OBJECT (button), "treeview");
|
|
|
|
GtkTreeModel* model;
|
|
|
|
GtkTreeIter iter;
|
|
|
|
if (katze_tree_view_get_selected_iter (treeview, &model, &iter))
|
|
|
|
{
|
|
|
|
gchar* path = gtk_tree_model_get_string_from_iter (model, &iter);
|
|
|
|
GtkTreePath* tree_path = gtk_tree_path_new_from_string (path);
|
|
|
|
/* gtk_cell_renderer_start_editing */
|
|
|
|
gtk_tree_view_set_cursor (treeview, tree_path, column, TRUE);
|
|
|
|
gtk_tree_path_free (tree_path);
|
|
|
|
g_free (path);
|
|
|
|
}
|
|
|
|
gdk_event_free (event);
|
|
|
|
}
|
|
|
|
|
2009-07-17 20:05:20 +00:00
|
|
|
static void
|
|
|
|
adblock_preferences_remove_clicked_cb (GtkWidget* button,
|
|
|
|
GtkTreeView* treeview)
|
|
|
|
{
|
|
|
|
GtkTreeModel* model;
|
|
|
|
GtkTreeIter iter;
|
|
|
|
if (katze_tree_view_get_selected_iter (treeview, &model, &iter))
|
|
|
|
gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
|
|
|
|
}
|
|
|
|
|
2009-10-14 21:00:47 +00:00
|
|
|
#if GTK_CHECK_VERSION (2, 18, 0)
|
|
|
|
static gboolean
|
|
|
|
adblock_activate_link_cb (GtkWidget* label,
|
|
|
|
const gchar* uri)
|
|
|
|
{
|
2009-12-05 00:04:31 +00:00
|
|
|
MidoriBrowser* browser = midori_browser_get_for_widget (label);
|
|
|
|
gint n = midori_browser_add_uri (browser, uri);
|
|
|
|
if (n > -1)
|
|
|
|
midori_browser_set_current_page (browser, n);
|
|
|
|
return n > -1;
|
2009-10-14 21:00:47 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-12-29 22:49:10 +00:00
|
|
|
static void
|
|
|
|
adblock_preferences_response_cb (GtkWidget* dialog,
|
|
|
|
gint response,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
gtk_widget_destroy (dialog);
|
|
|
|
}
|
|
|
|
|
2009-05-19 19:01:09 +00:00
|
|
|
static GtkWidget*
|
|
|
|
adblock_get_preferences_dialog (MidoriExtension* extension)
|
|
|
|
{
|
|
|
|
MidoriApp* app;
|
|
|
|
GtkWidget* browser;
|
|
|
|
const gchar* dialog_title;
|
|
|
|
GtkWidget* dialog;
|
2011-10-26 17:46:50 +00:00
|
|
|
GtkWidget* content_area;
|
2009-05-19 19:01:09 +00:00
|
|
|
gint width, height;
|
|
|
|
GtkWidget* xfce_heading;
|
|
|
|
GtkWidget* hbox;
|
|
|
|
GtkListStore* liststore;
|
|
|
|
GtkWidget* treeview;
|
|
|
|
GtkTreeViewColumn* column;
|
|
|
|
GtkCellRenderer* renderer_text;
|
2009-10-14 01:33:05 +00:00
|
|
|
GtkCellRenderer* renderer_toggle;
|
2009-05-19 19:01:09 +00:00
|
|
|
GtkWidget* scrolled;
|
|
|
|
gchar** filters;
|
|
|
|
GtkWidget* vbox;
|
|
|
|
GtkWidget* button;
|
2009-10-14 21:00:47 +00:00
|
|
|
gchar* description;
|
|
|
|
GtkWidget* entry;
|
2009-05-19 19:01:09 +00:00
|
|
|
#if HAVE_OSX
|
|
|
|
GtkWidget* icon;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
app = midori_extension_get_app (extension);
|
|
|
|
browser = katze_object_get_object (app, "browser");
|
|
|
|
|
|
|
|
dialog_title = _("Configure Advertisement filters");
|
|
|
|
dialog = gtk_dialog_new_with_buttons (dialog_title, GTK_WINDOW (browser),
|
|
|
|
GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
|
|
|
|
#if !HAVE_OSX
|
2009-11-28 18:40:57 +00:00
|
|
|
#if !HAVE_HILDON
|
2009-05-19 19:01:09 +00:00
|
|
|
GTK_STOCK_HELP, GTK_RESPONSE_HELP,
|
2009-11-28 18:40:57 +00:00
|
|
|
#endif
|
2009-05-19 19:01:09 +00:00
|
|
|
GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
|
|
|
|
#endif
|
|
|
|
NULL);
|
2012-07-28 09:06:40 +00:00
|
|
|
katze_widget_add_class (gtk_dialog_get_widget_for_response (
|
|
|
|
GTK_DIALOG (dialog), GTK_RESPONSE_HELP), "help_button");
|
2011-10-26 17:46:50 +00:00
|
|
|
content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
|
2009-05-19 19:01:09 +00:00
|
|
|
g_signal_connect (dialog, "destroy",
|
|
|
|
G_CALLBACK (gtk_widget_destroyed), &dialog);
|
|
|
|
gtk_window_set_icon_name (GTK_WINDOW (dialog), GTK_STOCK_PROPERTIES);
|
|
|
|
/* TODO: Implement some kind of help function */
|
|
|
|
gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
|
|
|
|
GTK_RESPONSE_HELP, FALSE);
|
|
|
|
sokoke_widget_get_text_size (dialog, "M", &width, &height);
|
|
|
|
gtk_window_set_default_size (GTK_WINDOW (dialog), width * 52, -1);
|
|
|
|
g_signal_connect (dialog, "response",
|
2009-12-29 22:49:10 +00:00
|
|
|
G_CALLBACK (adblock_preferences_response_cb), NULL);
|
2009-05-19 19:01:09 +00:00
|
|
|
/* TODO: We need mnemonics */
|
|
|
|
if ((xfce_heading = sokoke_xfce_header_new (
|
|
|
|
gtk_window_get_icon_name (GTK_WINDOW (dialog)), dialog_title)))
|
2011-10-26 17:46:50 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (content_area), xfce_heading, FALSE, FALSE, 0);
|
2011-10-01 13:12:04 +00:00
|
|
|
hbox = gtk_hbox_new (FALSE, 0);
|
2011-10-26 17:46:50 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 12);
|
2009-10-14 21:00:47 +00:00
|
|
|
vbox = gtk_vbox_new (FALSE, 0);
|
|
|
|
gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 4);
|
|
|
|
button = gtk_label_new (NULL);
|
|
|
|
description = g_strdup_printf (_(
|
|
|
|
"Type the address of a preconfigured filter list in the text entry "
|
|
|
|
"and click \"Add\" to add it to the list. "
|
|
|
|
"You can find more lists at %s."),
|
|
|
|
#if GTK_CHECK_VERSION (2, 18, 0)
|
|
|
|
"<a href=\"http://easylist.adblockplus.org/\">easylist.adblockplus.org</a>");
|
|
|
|
#else
|
|
|
|
"<u>http://easylist.adblockplus.org/</u>");
|
|
|
|
#endif
|
|
|
|
#if GTK_CHECK_VERSION (2, 18, 0)
|
|
|
|
g_signal_connect (button, "activate-link",
|
|
|
|
G_CALLBACK (adblock_activate_link_cb), NULL);
|
|
|
|
#endif
|
|
|
|
gtk_label_set_markup (GTK_LABEL (button), description);
|
|
|
|
g_free (description);
|
|
|
|
gtk_label_set_line_wrap (GTK_LABEL (button), TRUE);
|
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 4);
|
2011-05-15 14:45:14 +00:00
|
|
|
entry = katze_uri_entry_new (NULL);
|
2009-10-14 21:00:47 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 4);
|
2009-05-19 19:01:09 +00:00
|
|
|
liststore = gtk_list_store_new (1, G_TYPE_STRING);
|
|
|
|
treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL (liststore));
|
|
|
|
gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (treeview), FALSE);
|
|
|
|
column = gtk_tree_view_column_new ();
|
2009-10-14 01:33:05 +00:00
|
|
|
renderer_toggle = gtk_cell_renderer_toggle_new ();
|
|
|
|
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (column), renderer_toggle, FALSE);
|
|
|
|
gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (column), renderer_toggle,
|
|
|
|
(GtkCellLayoutDataFunc)adblock_preferences_render_tick_cb,
|
|
|
|
extension, NULL);
|
|
|
|
g_signal_connect (renderer_toggle, "toggled",
|
|
|
|
G_CALLBACK (adblock_preferences_renderer_toggle_toggled_cb), liststore);
|
|
|
|
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
|
|
|
|
column = gtk_tree_view_column_new ();
|
2009-05-19 19:01:09 +00:00
|
|
|
renderer_text = gtk_cell_renderer_text_new ();
|
|
|
|
gtk_tree_view_column_pack_start (column, renderer_text, TRUE);
|
2009-07-17 19:37:36 +00:00
|
|
|
g_object_set (renderer_text, "editable", TRUE, NULL);
|
|
|
|
g_signal_connect (renderer_text, "edited",
|
|
|
|
G_CALLBACK (adblock_preferences_renderer_text_edited_cb), liststore);
|
2009-10-14 01:33:05 +00:00
|
|
|
gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (column), renderer_text,
|
|
|
|
(GtkCellLayoutDataFunc)adblock_preferences_render_text_cb,
|
|
|
|
extension, NULL);
|
2009-05-19 19:01:09 +00:00
|
|
|
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
|
|
|
|
scrolled = gtk_scrolled_window_new (NULL, NULL);
|
|
|
|
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
|
|
|
|
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
|
|
|
|
gtk_container_add (GTK_CONTAINER (scrolled), treeview);
|
|
|
|
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled),
|
|
|
|
GTK_SHADOW_IN);
|
2009-10-14 21:00:47 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), scrolled, TRUE, TRUE, 5);
|
2009-05-19 19:01:09 +00:00
|
|
|
|
|
|
|
filters = midori_extension_get_string_list (extension, "filters", NULL);
|
|
|
|
if (filters != NULL)
|
|
|
|
{
|
|
|
|
gsize i = 0;
|
|
|
|
while (filters[i++] != NULL)
|
|
|
|
gtk_list_store_insert_with_values (GTK_LIST_STORE (liststore),
|
|
|
|
NULL, i - 1, 0, filters[i -1], -1);
|
|
|
|
}
|
|
|
|
g_strfreev (filters);
|
2010-02-03 17:30:48 +00:00
|
|
|
g_object_connect (liststore,
|
|
|
|
"signal::row-inserted",
|
|
|
|
adblock_preferences_model_row_changed_cb, extension,
|
|
|
|
"signal::row-changed",
|
|
|
|
adblock_preferences_model_row_changed_cb, extension,
|
|
|
|
"signal::row-deleted",
|
|
|
|
adblock_preferences_model_row_deleted_cb, extension,
|
|
|
|
NULL);
|
2009-05-19 19:01:09 +00:00
|
|
|
|
|
|
|
g_object_unref (liststore);
|
|
|
|
vbox = gtk_vbox_new (FALSE, 4);
|
|
|
|
gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 4);
|
|
|
|
button = gtk_button_new_from_stock (GTK_STOCK_ADD);
|
2012-06-27 21:58:07 +00:00
|
|
|
g_object_set_data (G_OBJECT (dialog), "entry", entry);
|
2009-10-14 21:00:47 +00:00
|
|
|
g_object_set_data (G_OBJECT (button), "entry", entry);
|
2009-07-17 20:05:20 +00:00
|
|
|
g_signal_connect (button, "clicked",
|
|
|
|
G_CALLBACK (adblock_preferences_add_clicked_cb), liststore);
|
2011-11-22 20:40:32 +00:00
|
|
|
g_signal_connect (entry, "activate",
|
|
|
|
G_CALLBACK (adblock_preferences_add_clicked_cb), liststore);
|
2009-05-19 19:01:09 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
|
|
|
|
button = gtk_button_new_from_stock (GTK_STOCK_EDIT);
|
2009-11-20 19:53:41 +00:00
|
|
|
g_object_set_data (G_OBJECT (button), "treeview", treeview);
|
|
|
|
g_signal_connect (button, "clicked",
|
|
|
|
G_CALLBACK (adblock_preferences_edit_clicked_cb), column);
|
2009-05-19 19:01:09 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
|
|
|
|
button = gtk_button_new_from_stock (GTK_STOCK_REMOVE);
|
2009-07-17 20:05:20 +00:00
|
|
|
g_signal_connect (button, "clicked",
|
|
|
|
G_CALLBACK (adblock_preferences_remove_clicked_cb), treeview);
|
2009-05-19 19:01:09 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
|
|
|
|
button = gtk_label_new (""); /* This is an invisible separator */
|
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 8);
|
|
|
|
gtk_widget_set_sensitive (button, FALSE);
|
|
|
|
button = gtk_label_new (""); /* This is an invisible separator */
|
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 12);
|
|
|
|
button = gtk_button_new_from_stock (GTK_STOCK_GO_DOWN);
|
|
|
|
gtk_widget_set_sensitive (button, FALSE);
|
|
|
|
gtk_box_pack_end (GTK_BOX (vbox), button, FALSE, FALSE, 0);
|
|
|
|
button = gtk_button_new_from_stock (GTK_STOCK_GO_UP);
|
|
|
|
gtk_widget_set_sensitive (button, FALSE);
|
|
|
|
gtk_box_pack_end (GTK_BOX (vbox), button, FALSE, FALSE, 0);
|
|
|
|
|
|
|
|
#if HAVE_OSX
|
|
|
|
hbox = gtk_hbox_new (FALSE, 0);
|
|
|
|
button = gtk_button_new ();
|
|
|
|
icon = gtk_image_new_from_stock (GTK_STOCK_HELP, GTK_ICON_SIZE_BUTTON);
|
|
|
|
gtk_button_set_image (GTK_BUTTON (button), icon);
|
|
|
|
/* TODO: Implement some kind of help function */
|
|
|
|
gtk_widget_set_sensitive (button, FALSE);
|
|
|
|
/* g_signal_connect (button, "clicked",
|
|
|
|
G_CALLBACK (adblock_preferences_help_clicked_cb), dialog); */
|
|
|
|
gtk_box_pack_end (GTK_BOX (hbox),
|
|
|
|
button, FALSE, FALSE, 4);
|
2011-10-26 17:46:50 +00:00
|
|
|
gtk_box_pack_end (GTK_BOX (content_area),
|
2011-10-01 13:12:04 +00:00
|
|
|
hbox, FALSE, FALSE, 0);
|
2009-05-19 19:01:09 +00:00
|
|
|
#endif
|
2011-10-26 17:46:50 +00:00
|
|
|
gtk_widget_show_all (content_area);
|
2009-05-19 19:01:09 +00:00
|
|
|
|
|
|
|
g_object_unref (browser);
|
|
|
|
|
|
|
|
return dialog;
|
|
|
|
}
|
|
|
|
|
2012-06-27 21:58:07 +00:00
|
|
|
static GtkWidget*
|
|
|
|
adblock_show_preferences_dialog (MidoriExtension* extension,
|
|
|
|
const gchar* uri)
|
2009-05-19 19:01:09 +00:00
|
|
|
{
|
|
|
|
static GtkWidget* dialog = NULL;
|
|
|
|
|
|
|
|
if (!dialog)
|
|
|
|
{
|
|
|
|
dialog = adblock_get_preferences_dialog (extension);
|
|
|
|
g_signal_connect (dialog, "destroy",
|
|
|
|
G_CALLBACK (gtk_widget_destroyed), &dialog);
|
|
|
|
gtk_widget_show (dialog);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
gtk_window_present (GTK_WINDOW (dialog));
|
2012-06-27 21:58:07 +00:00
|
|
|
|
|
|
|
if (uri != NULL)
|
|
|
|
{
|
|
|
|
GtkWidget* entry = g_object_get_data (G_OBJECT (dialog), "entry");
|
|
|
|
gtk_entry_set_text (GTK_ENTRY (entry), uri);
|
|
|
|
}
|
|
|
|
return dialog;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
adblock_open_preferences_cb (MidoriExtension* extension)
|
|
|
|
{
|
|
|
|
adblock_show_preferences_dialog (extension, NULL);
|
2009-05-19 19:01:09 +00:00
|
|
|
}
|
|
|
|
|
2011-11-28 19:48:55 +00:00
|
|
|
static inline gint
|
|
|
|
adblock_check_rule (GRegex* regex,
|
|
|
|
const gchar* patt,
|
|
|
|
const gchar* req_uri,
|
|
|
|
const gchar* page_uri)
|
2009-07-16 12:13:27 +00:00
|
|
|
{
|
2011-11-28 19:48:55 +00:00
|
|
|
gchar* opts;
|
|
|
|
|
|
|
|
if (!g_regex_match_full (regex, req_uri, -1, 0, 0, NULL, NULL))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
opts = g_hash_table_lookup (optslist, patt);
|
|
|
|
if (opts && g_regex_match_simple (",third-party", opts,
|
2009-12-18 18:56:10 +00:00
|
|
|
G_REGEX_CASELESS, G_REGEX_MATCH_NOTEMPTY))
|
|
|
|
{
|
|
|
|
if (page_uri && g_regex_match_full (regex, page_uri, -1, 0, 0, NULL, NULL))
|
2011-11-28 19:48:55 +00:00
|
|
|
return FALSE;
|
2009-12-18 18:56:10 +00:00
|
|
|
}
|
|
|
|
/* TODO: Domain opt check */
|
2011-12-18 01:07:55 +00:00
|
|
|
#ifdef G_ENABLE_DEBUG
|
2011-11-28 19:48:55 +00:00
|
|
|
adblock_debug ("blocked by pattern regexp=%s -- %s", g_regex_get_pattern (regex), req_uri);
|
2011-12-18 01:07:55 +00:00
|
|
|
#endif
|
2011-11-28 19:48:55 +00:00
|
|
|
return TRUE;
|
2009-12-18 18:56:10 +00:00
|
|
|
}
|
2009-11-20 00:00:48 +00:00
|
|
|
|
2010-06-19 13:52:16 +00:00
|
|
|
static inline gboolean
|
2011-11-28 19:48:55 +00:00
|
|
|
adblock_is_matched_by_pattern (const gchar* req_uri,
|
|
|
|
const gchar* page_uri)
|
2009-12-18 18:56:10 +00:00
|
|
|
{
|
|
|
|
GHashTableIter iter;
|
2011-07-02 18:11:24 +00:00
|
|
|
gpointer patt, regex;
|
2009-12-18 18:56:10 +00:00
|
|
|
|
|
|
|
if (USE_PATTERN_MATCHING == 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
g_hash_table_iter_init (&iter, pattern);
|
2011-07-02 18:11:24 +00:00
|
|
|
while (g_hash_table_iter_next (&iter, &patt, ®ex))
|
2009-11-20 00:00:48 +00:00
|
|
|
{
|
2011-11-28 19:48:55 +00:00
|
|
|
if (adblock_check_rule (regex, patt, req_uri, page_uri))
|
|
|
|
return TRUE;
|
2009-11-20 00:00:48 +00:00
|
|
|
}
|
2009-12-18 18:56:10 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-06-19 13:52:16 +00:00
|
|
|
static inline gboolean
|
2011-11-28 19:48:55 +00:00
|
|
|
adblock_is_matched_by_key (const gchar* req_uri,
|
|
|
|
const gchar* page_uri)
|
2009-12-18 18:56:10 +00:00
|
|
|
{
|
|
|
|
gchar* uri;
|
2009-12-21 22:57:31 +00:00
|
|
|
gint len;
|
|
|
|
int pos = 0;
|
2010-01-10 21:52:06 +00:00
|
|
|
GList* regex_bl = NULL;
|
2011-10-08 15:07:55 +00:00
|
|
|
GString* guri;
|
2011-11-28 19:48:55 +00:00
|
|
|
gboolean ret = FALSE;
|
2011-11-28 19:58:57 +00:00
|
|
|
gchar sig[SIGNATURE_SIZE + 1];
|
2011-10-08 15:07:55 +00:00
|
|
|
|
2011-11-29 00:32:31 +00:00
|
|
|
memset (&sig[0], 0, sizeof (sig));
|
2011-11-28 19:48:55 +00:00
|
|
|
/* Signatures are made on pattern, so we need to convert url to a pattern as well */
|
2011-10-08 15:07:55 +00:00
|
|
|
guri = adblock_fixup_regexp ("", (gchar*)req_uri);
|
|
|
|
uri = guri->str;
|
|
|
|
len = guri->len;
|
2009-12-18 18:56:10 +00:00
|
|
|
|
2009-12-21 22:57:31 +00:00
|
|
|
for (pos = len - SIGNATURE_SIZE; pos >= 0; pos--)
|
|
|
|
{
|
2011-11-28 19:58:57 +00:00
|
|
|
GRegex* regex;
|
|
|
|
strncpy (sig, uri + pos, SIGNATURE_SIZE);
|
|
|
|
regex = g_hash_table_lookup (keys, sig);
|
2011-07-02 18:11:24 +00:00
|
|
|
|
2011-11-28 19:48:55 +00:00
|
|
|
/* Dont check if regex is already blacklisted */
|
|
|
|
if (!regex || g_list_find (regex_bl, regex))
|
|
|
|
continue;
|
|
|
|
ret = adblock_check_rule (regex, sig, req_uri, page_uri);
|
2011-11-29 00:32:31 +00:00
|
|
|
if (ret)
|
2011-11-28 19:48:55 +00:00
|
|
|
break;
|
2011-11-28 19:58:57 +00:00
|
|
|
regex_bl = g_list_prepend (regex_bl, regex);
|
2009-11-20 00:00:48 +00:00
|
|
|
}
|
2011-10-08 15:07:55 +00:00
|
|
|
g_string_free (guri, TRUE);
|
2010-01-10 21:52:06 +00:00
|
|
|
g_list_free (regex_bl);
|
2011-11-28 19:48:55 +00:00
|
|
|
return ret;
|
2009-07-16 12:13:27 +00:00
|
|
|
}
|
|
|
|
|
2009-12-18 18:56:10 +00:00
|
|
|
static gboolean
|
2011-07-02 18:11:24 +00:00
|
|
|
adblock_is_matched (const gchar* req_uri,
|
2009-12-18 18:56:10 +00:00
|
|
|
const gchar* page_uri)
|
|
|
|
{
|
2011-05-29 02:57:31 +00:00
|
|
|
gchar* value;
|
|
|
|
|
|
|
|
if ((value = g_hash_table_lookup (urlcache, req_uri)))
|
|
|
|
{
|
|
|
|
if (value[0] == '0')
|
|
|
|
return FALSE;
|
|
|
|
else
|
|
|
|
return TRUE;
|
|
|
|
}
|
2009-12-18 18:56:10 +00:00
|
|
|
|
2011-07-02 18:19:16 +00:00
|
|
|
if (adblock_is_matched_by_key (req_uri, page_uri))
|
|
|
|
{
|
|
|
|
g_hash_table_insert (urlcache, g_strdup (req_uri), g_strdup("1"));
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (adblock_is_matched_by_pattern (req_uri, page_uri))
|
2011-05-29 02:57:31 +00:00
|
|
|
{
|
|
|
|
g_hash_table_insert (urlcache, g_strdup (req_uri), g_strdup("1"));
|
2009-12-18 18:56:10 +00:00
|
|
|
return TRUE;
|
2011-05-29 02:57:31 +00:00
|
|
|
}
|
|
|
|
g_hash_table_insert (urlcache, g_strdup (req_uri), g_strdup("0"));
|
|
|
|
return FALSE;
|
2009-12-18 18:56:10 +00:00
|
|
|
}
|
|
|
|
|
2010-01-14 01:19:38 +00:00
|
|
|
static gchar*
|
2010-01-17 17:28:11 +00:00
|
|
|
adblock_prepare_urihider_js (GList* uris)
|
2010-01-14 01:19:38 +00:00
|
|
|
{
|
2010-01-17 17:28:11 +00:00
|
|
|
GList* li = NULL;
|
2011-06-06 16:47:19 +00:00
|
|
|
GString* js = g_string_new (
|
2010-01-14 01:19:38 +00:00
|
|
|
"(function() {"
|
2010-01-17 17:28:11 +00:00
|
|
|
"function getElementsByAttribute (strTagName, strAttributeName, arrAttributeValue) {"
|
2010-01-14 01:19:38 +00:00
|
|
|
" var arrElements = document.getElementsByTagName (strTagName);"
|
|
|
|
" var arrReturnElements = new Array();"
|
2010-01-17 17:28:11 +00:00
|
|
|
" for (var j=0; j<arrAttributeValue.length; j++) {"
|
|
|
|
" var strAttributeValue = arrAttributeValue[j];"
|
|
|
|
" for (var i=0; i<arrElements.length; i++) {"
|
|
|
|
" var oCurrent = arrElements[i];"
|
|
|
|
" var oAttribute = oCurrent.getAttribute && oCurrent.getAttribute (strAttributeName);"
|
|
|
|
" if (oAttribute && oAttribute.length > 0 && strAttributeValue.indexOf (oAttribute) != -1)"
|
|
|
|
" arrReturnElements.push (oCurrent);"
|
|
|
|
" }"
|
2010-01-14 01:19:38 +00:00
|
|
|
" }"
|
|
|
|
" return arrReturnElements;"
|
2010-01-17 17:28:11 +00:00
|
|
|
"};"
|
|
|
|
"function hideElementBySrc (uris) {"
|
|
|
|
" var oElements = getElementsByAttribute('img', 'src', uris);"
|
2010-01-14 01:19:38 +00:00
|
|
|
" if (oElements.length == 0)"
|
2010-01-17 17:28:11 +00:00
|
|
|
" oElements = getElementsByAttribute ('iframe', 'src', uris);"
|
2010-01-14 01:19:38 +00:00
|
|
|
" for (var i=0; i<oElements.length; i++) {"
|
|
|
|
" oElements[i].style.visibility = 'hidden !important';"
|
|
|
|
" oElements[i].style.width = '0';"
|
|
|
|
" oElements[i].style.height = '0';"
|
|
|
|
" }"
|
2010-01-17 17:28:11 +00:00
|
|
|
"};"
|
2011-06-06 16:47:19 +00:00
|
|
|
"var uris=new Array ();");
|
2010-01-17 17:28:11 +00:00
|
|
|
|
|
|
|
for (li = uris; li != NULL; li = g_list_next (li))
|
2011-06-06 16:47:19 +00:00
|
|
|
g_string_append_printf (js, "uris.push ('%s');", (gchar*)li->data);
|
|
|
|
|
|
|
|
g_string_append (js, "hideElementBySrc (uris);})();");
|
2010-01-17 17:28:11 +00:00
|
|
|
|
2011-06-06 16:47:19 +00:00
|
|
|
return g_string_free (js, FALSE);
|
2010-01-14 01:19:38 +00:00
|
|
|
}
|
2009-12-18 18:56:10 +00:00
|
|
|
|
2012-04-12 21:17:02 +00:00
|
|
|
static gboolean
|
|
|
|
adblock_navigation_policy_decision_requested_cb (WebKitWebView* web_view,
|
|
|
|
WebKitWebFrame* web_frame,
|
|
|
|
WebKitNetworkRequest* request,
|
|
|
|
WebKitWebNavigationAction* action,
|
|
|
|
WebKitWebPolicyDecision* decision,
|
2012-06-27 21:58:07 +00:00
|
|
|
MidoriExtension* extension)
|
2012-04-12 21:17:02 +00:00
|
|
|
{
|
2012-06-27 21:58:07 +00:00
|
|
|
const gchar* uri = webkit_network_request_get_uri (request);
|
|
|
|
if (g_str_has_prefix (uri, "abp:"))
|
|
|
|
{
|
|
|
|
gchar** parts;
|
|
|
|
gchar* filter;
|
|
|
|
if (g_str_has_prefix (uri, "abp:subscribe?location="))
|
|
|
|
uri = &uri[23];
|
|
|
|
else if (g_str_has_prefix (uri, "abp://subscribe?location="))
|
|
|
|
uri = &uri[25];
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
parts = g_strsplit (uri, "&", 2);
|
|
|
|
filter = soup_uri_decode (parts[0]);
|
|
|
|
webkit_web_policy_decision_ignore (decision);
|
|
|
|
adblock_show_preferences_dialog (extension, filter);
|
|
|
|
g_free (filter);
|
|
|
|
g_strfreev (parts);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-04-12 21:17:02 +00:00
|
|
|
if (web_frame == webkit_web_view_get_main_frame (web_view))
|
|
|
|
{
|
|
|
|
const gchar* req_uri = webkit_network_request_get_uri (request);
|
|
|
|
g_hash_table_replace (navigationwhitelist, web_view, g_strdup (req_uri));
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-19 19:01:09 +00:00
|
|
|
static void
|
2009-09-09 21:53:30 +00:00
|
|
|
adblock_resource_request_starting_cb (WebKitWebView* web_view,
|
|
|
|
WebKitWebFrame* web_frame,
|
|
|
|
WebKitWebResource* web_resource,
|
|
|
|
WebKitNetworkRequest* request,
|
|
|
|
WebKitNetworkResponse* response,
|
2012-04-12 20:56:35 +00:00
|
|
|
MidoriView* view)
|
2009-05-19 19:01:09 +00:00
|
|
|
{
|
2009-11-20 00:00:48 +00:00
|
|
|
SoupMessage* msg;
|
2010-01-17 17:28:11 +00:00
|
|
|
GList* blocked_uris;
|
2009-12-18 18:56:10 +00:00
|
|
|
const gchar* req_uri;
|
|
|
|
const char *page_uri;
|
2009-11-20 00:00:48 +00:00
|
|
|
|
2011-08-31 16:45:05 +00:00
|
|
|
page_uri = webkit_web_view_get_uri (web_view);
|
|
|
|
/* Skip checks on about: pages */
|
2011-10-28 20:47:17 +00:00
|
|
|
if (midori_uri_is_blank (page_uri))
|
2011-08-31 16:45:05 +00:00
|
|
|
return;
|
|
|
|
|
2009-12-18 18:56:10 +00:00
|
|
|
req_uri = webkit_network_request_get_uri (request);
|
2012-04-12 21:17:02 +00:00
|
|
|
|
|
|
|
if (!g_strcmp0 (req_uri, g_hash_table_lookup (navigationwhitelist, web_view)))
|
|
|
|
return;
|
|
|
|
|
2011-10-29 01:32:16 +00:00
|
|
|
if (!midori_uri_is_http (req_uri)
|
|
|
|
|| g_str_has_suffix (req_uri, "favicon.ico"))
|
2011-08-27 14:24:43 +00:00
|
|
|
return;
|
2009-11-20 00:00:48 +00:00
|
|
|
|
|
|
|
msg = webkit_network_request_get_message (request);
|
2011-10-29 01:32:16 +00:00
|
|
|
if (!(msg && !g_strcmp0 (msg->method, "GET")))
|
2009-11-20 00:00:48 +00:00
|
|
|
return;
|
|
|
|
|
2012-04-13 01:02:22 +00:00
|
|
|
if (response != NULL) /* request is caused by redirect */
|
|
|
|
{
|
|
|
|
if (web_frame == webkit_web_view_get_main_frame (web_view))
|
|
|
|
{
|
|
|
|
g_hash_table_replace (navigationwhitelist, web_view, g_strdup (req_uri));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-08 19:39:05 +00:00
|
|
|
#ifdef G_ENABLE_DEBUG
|
2012-07-19 18:04:25 +00:00
|
|
|
if (midori_debug ("adblock:2"))
|
2010-04-08 19:39:05 +00:00
|
|
|
g_test_timer_start ();
|
|
|
|
#endif
|
2011-07-02 18:11:24 +00:00
|
|
|
if (adblock_is_matched (req_uri, page_uri))
|
2010-01-14 01:19:38 +00:00
|
|
|
{
|
2010-01-17 17:28:11 +00:00
|
|
|
blocked_uris = g_object_get_data (G_OBJECT (web_view), "blocked-uris");
|
|
|
|
blocked_uris = g_list_prepend (blocked_uris, g_strdup (req_uri));
|
2009-09-09 21:53:30 +00:00
|
|
|
webkit_network_request_set_uri (request, "about:blank");
|
2010-01-17 17:28:11 +00:00
|
|
|
g_object_set_data (G_OBJECT (web_view), "blocked-uris", blocked_uris);
|
2010-01-14 01:19:38 +00:00
|
|
|
}
|
2010-04-09 20:57:05 +00:00
|
|
|
#ifdef G_ENABLE_DEBUG
|
2012-07-19 18:04:25 +00:00
|
|
|
if (midori_debug ("adblock:2"))
|
2010-04-09 20:57:05 +00:00
|
|
|
g_debug ("match: %f%s", g_test_timer_elapsed (), "seconds");
|
|
|
|
#endif
|
2009-12-18 18:56:10 +00:00
|
|
|
|
2009-09-09 21:53:30 +00:00
|
|
|
}
|
2009-11-20 00:00:48 +00:00
|
|
|
|
2010-01-02 02:47:56 +00:00
|
|
|
static void
|
|
|
|
adblock_custom_block_image_cb (GtkWidget* widget,
|
|
|
|
MidoriExtension* extension)
|
|
|
|
{
|
|
|
|
gchar* custom_list;
|
2010-01-02 20:15:58 +00:00
|
|
|
FILE* list;
|
2010-02-03 17:58:07 +00:00
|
|
|
MidoriApp* app;
|
|
|
|
GtkWidget* browser;
|
|
|
|
GtkWidget* dialog;
|
2011-10-26 17:46:50 +00:00
|
|
|
GtkWidget* content_area;
|
2010-02-03 17:58:07 +00:00
|
|
|
GtkSizeGroup* sizegroup;
|
|
|
|
GtkWidget* hbox;
|
|
|
|
GtkWidget* label;
|
|
|
|
GtkWidget* entry;
|
|
|
|
gchar* title;
|
|
|
|
|
|
|
|
app = midori_extension_get_app (extension);
|
|
|
|
browser = katze_object_get_object (app, "browser");
|
|
|
|
|
|
|
|
title = _("Edit rule");
|
|
|
|
dialog = gtk_dialog_new_with_buttons (title, GTK_WINDOW (browser),
|
|
|
|
GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
|
|
|
|
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
|
|
|
|
GTK_STOCK_ADD, GTK_RESPONSE_ACCEPT,
|
|
|
|
NULL);
|
2011-10-26 17:46:50 +00:00
|
|
|
content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
|
2010-02-03 17:58:07 +00:00
|
|
|
gtk_window_set_icon_name (GTK_WINDOW (dialog), GTK_STOCK_ADD);
|
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
|
2011-10-26 17:46:50 +00:00
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (content_area), 5);
|
2010-02-03 17:58:07 +00:00
|
|
|
sizegroup = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
|
|
|
|
|
|
|
|
hbox = gtk_hbox_new (FALSE, 8);
|
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
|
|
|
|
label = gtk_label_new_with_mnemonic (_("_Rule:"));
|
|
|
|
gtk_size_group_add_widget (sizegroup, label);
|
|
|
|
gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
|
|
|
|
entry = gtk_entry_new ();
|
|
|
|
gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
|
|
|
|
gtk_entry_set_text (GTK_ENTRY (entry),
|
|
|
|
g_object_get_data (G_OBJECT (widget), "uri"));
|
|
|
|
gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
|
2011-10-26 17:46:50 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (content_area), hbox);
|
2010-02-03 17:58:07 +00:00
|
|
|
gtk_widget_show_all (hbox);
|
|
|
|
|
|
|
|
gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
|
|
|
|
if (gtk_dialog_run (GTK_DIALOG (dialog)) != GTK_RESPONSE_ACCEPT)
|
|
|
|
{
|
|
|
|
gtk_widget_destroy (dialog);
|
|
|
|
return;
|
|
|
|
}
|
2010-01-02 02:47:56 +00:00
|
|
|
|
|
|
|
custom_list = g_build_filename (midori_extension_get_config_dir (extension),
|
|
|
|
CUSTOM_LIST_NAME, NULL);
|
2012-04-05 20:29:49 +00:00
|
|
|
katze_mkdir_with_parents (midori_extension_get_config_dir (extension), 0700);
|
|
|
|
if ((list = g_fopen (custom_list, "a+")))
|
2010-01-02 02:47:56 +00:00
|
|
|
{
|
2012-04-05 20:29:49 +00:00
|
|
|
g_fprintf (list, "%s\n", gtk_entry_get_text (GTK_ENTRY (entry)));
|
|
|
|
fclose (list);
|
|
|
|
adblock_reload_rules (extension, TRUE);
|
|
|
|
g_debug ("%s: Updated custom list\n", G_STRFUNC);
|
2010-01-02 02:47:56 +00:00
|
|
|
}
|
2012-04-05 20:29:49 +00:00
|
|
|
else
|
|
|
|
g_debug ("%s: Failed to open custom list %s\n", G_STRFUNC, custom_list);
|
2010-01-02 02:47:56 +00:00
|
|
|
g_free (custom_list);
|
2010-02-03 17:58:07 +00:00
|
|
|
gtk_widget_destroy (dialog);
|
2010-01-02 02:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
adblock_populate_popup_cb (WebKitWebView* web_view,
|
|
|
|
GtkWidget* menu,
|
|
|
|
MidoriExtension* extension)
|
|
|
|
{
|
|
|
|
GtkWidget* menuitem;
|
|
|
|
gchar *uri;
|
|
|
|
gint x, y;
|
|
|
|
GdkEventButton event;
|
|
|
|
WebKitHitTestResultContext context;
|
|
|
|
WebKitHitTestResult* hit_test;
|
|
|
|
|
2011-10-01 13:12:04 +00:00
|
|
|
gdk_window_get_pointer (gtk_widget_get_window(GTK_WIDGET (web_view)), &x, &y, NULL);
|
2010-01-02 02:47:56 +00:00
|
|
|
event.x = x;
|
|
|
|
event.y = y;
|
|
|
|
hit_test = webkit_web_view_get_hit_test_result (web_view, &event);
|
|
|
|
context = katze_object_get_int (hit_test, "context");
|
2010-01-17 17:28:11 +00:00
|
|
|
if (context & WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE)
|
|
|
|
{
|
|
|
|
uri = katze_object_get_string (hit_test, "image-uri");
|
2010-02-06 16:28:37 +00:00
|
|
|
menuitem = gtk_menu_item_new_with_mnemonic (_("Bl_ock image"));
|
2010-01-17 17:28:11 +00:00
|
|
|
}
|
|
|
|
else if (context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK)
|
|
|
|
{
|
|
|
|
uri = katze_object_get_string (hit_test, "link-uri");
|
2010-02-06 16:28:37 +00:00
|
|
|
menuitem = gtk_menu_item_new_with_mnemonic (_("Bl_ock link"));
|
2010-01-17 17:28:11 +00:00
|
|
|
}
|
|
|
|
else
|
2010-01-02 02:47:56 +00:00
|
|
|
return;
|
|
|
|
gtk_widget_show (menuitem);
|
|
|
|
gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
|
|
|
|
g_object_set_data_full (G_OBJECT (menuitem), "uri", uri, (GDestroyNotify)g_free);
|
|
|
|
g_signal_connect (menuitem, "activate",
|
|
|
|
G_CALLBACK (adblock_custom_block_image_cb), extension);
|
|
|
|
}
|
|
|
|
|
2010-01-17 17:28:11 +00:00
|
|
|
static void
|
|
|
|
adblock_load_finished_cb (WebKitWebView *web_view,
|
|
|
|
WebKitWebFrame *web_frame,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GList* uris = g_object_get_data (G_OBJECT (web_view), "blocked-uris");
|
2010-04-08 19:39:05 +00:00
|
|
|
gchar* script;
|
|
|
|
GList* li;
|
|
|
|
|
2010-01-17 17:28:11 +00:00
|
|
|
if (g_list_nth_data (uris, 0) == NULL)
|
|
|
|
return;
|
|
|
|
|
2010-04-08 19:39:05 +00:00
|
|
|
script = adblock_prepare_urihider_js (uris);
|
2010-01-17 17:28:11 +00:00
|
|
|
webkit_web_view_execute_script (web_view, script);
|
2010-04-08 19:39:05 +00:00
|
|
|
li = NULL;
|
2010-01-17 17:28:11 +00:00
|
|
|
for (li = uris; li != NULL; li = g_list_next (li))
|
|
|
|
uris = g_list_remove (uris, li->data);
|
|
|
|
g_free (script);
|
|
|
|
g_object_set_data (G_OBJECT (web_view), "blocked-uris", uris);
|
|
|
|
}
|
|
|
|
|
2009-09-14 16:25:08 +00:00
|
|
|
static void
|
2010-01-17 17:28:11 +00:00
|
|
|
adblock_window_object_cleared_cb (WebKitWebView* web_view,
|
2009-09-14 16:25:08 +00:00
|
|
|
WebKitWebFrame* web_frame,
|
|
|
|
JSContextRef js_context,
|
|
|
|
JSObjectRef js_window)
|
|
|
|
{
|
2011-08-31 16:45:05 +00:00
|
|
|
const char *page_uri;
|
2011-12-18 01:07:55 +00:00
|
|
|
gchar* script;
|
2011-08-31 16:45:05 +00:00
|
|
|
|
2011-12-02 23:53:30 +00:00
|
|
|
page_uri = webkit_web_frame_get_uri (web_frame);
|
2011-08-31 16:45:05 +00:00
|
|
|
/* Don't add adblock css into speeddial and about: pages */
|
2011-12-02 23:53:30 +00:00
|
|
|
if (!midori_uri_is_http (page_uri))
|
2011-08-31 16:45:05 +00:00
|
|
|
return;
|
|
|
|
|
2011-12-18 01:07:55 +00:00
|
|
|
script = adblock_build_js (page_uri);
|
|
|
|
if (!script)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_free (sokoke_js_script_eval (js_context, script, NULL));
|
|
|
|
g_free (script);
|
2009-09-14 16:25:08 +00:00
|
|
|
}
|
2009-09-09 21:53:30 +00:00
|
|
|
|
|
|
|
static void
|
2010-01-02 02:47:56 +00:00
|
|
|
adblock_add_tab_cb (MidoriBrowser* browser,
|
|
|
|
MidoriView* view,
|
|
|
|
MidoriExtension* extension)
|
2009-09-09 21:53:30 +00:00
|
|
|
{
|
2010-03-30 20:44:04 +00:00
|
|
|
GtkWidget* web_view = midori_view_get_web_view (view);
|
2010-01-02 02:47:56 +00:00
|
|
|
|
2009-10-11 21:28:37 +00:00
|
|
|
g_signal_connect (web_view, "window-object-cleared",
|
|
|
|
G_CALLBACK (adblock_window_object_cleared_cb), 0);
|
2011-06-24 19:37:12 +00:00
|
|
|
|
2010-01-02 02:47:56 +00:00
|
|
|
g_signal_connect_after (web_view, "populate-popup",
|
|
|
|
G_CALLBACK (adblock_populate_popup_cb), extension);
|
2012-04-12 21:17:02 +00:00
|
|
|
g_signal_connect (web_view, "navigation-policy-decision-requested",
|
2012-06-27 21:58:07 +00:00
|
|
|
G_CALLBACK (adblock_navigation_policy_decision_requested_cb), extension);
|
2009-10-09 15:05:07 +00:00
|
|
|
g_signal_connect (web_view, "resource-request-starting",
|
2012-04-12 20:56:35 +00:00
|
|
|
G_CALLBACK (adblock_resource_request_starting_cb), view);
|
2010-01-17 17:28:11 +00:00
|
|
|
g_signal_connect (web_view, "load-finished",
|
2012-04-12 20:56:35 +00:00
|
|
|
G_CALLBACK (adblock_load_finished_cb), view);
|
2009-09-14 16:25:08 +00:00
|
|
|
}
|
|
|
|
|
2012-04-12 21:17:02 +00:00
|
|
|
static void
|
|
|
|
adblock_remove_tab_cb (MidoriBrowser* browser,
|
|
|
|
MidoriView* view,
|
|
|
|
MidoriExtension* extension)
|
|
|
|
{
|
|
|
|
GtkWidget* web_view = midori_view_get_web_view (view);
|
|
|
|
g_hash_table_remove (navigationwhitelist, web_view);
|
|
|
|
}
|
|
|
|
|
2009-09-14 16:25:08 +00:00
|
|
|
static void
|
|
|
|
adblock_deactivate_cb (MidoriExtension* extension,
|
2010-01-02 02:47:56 +00:00
|
|
|
MidoriBrowser* browser);
|
2009-09-14 16:25:08 +00:00
|
|
|
|
2009-09-09 21:53:30 +00:00
|
|
|
static void
|
|
|
|
adblock_app_add_browser_cb (MidoriApp* app,
|
|
|
|
MidoriBrowser* browser,
|
|
|
|
MidoriExtension* extension)
|
|
|
|
{
|
2009-10-19 21:25:12 +00:00
|
|
|
GtkWidget* statusbar;
|
|
|
|
GtkWidget* image;
|
2012-04-13 17:26:30 +00:00
|
|
|
GtkWidget* view;
|
|
|
|
gint i;
|
2009-10-19 21:25:12 +00:00
|
|
|
|
|
|
|
statusbar = katze_object_get_object (browser, "statusbar");
|
2010-01-02 02:47:56 +00:00
|
|
|
image = NULL;
|
|
|
|
/* image = gtk_image_new_from_stock (STOCK_IMAGE, GTK_ICON_SIZE_MENU);
|
2009-10-19 21:25:12 +00:00
|
|
|
gtk_widget_show (image);
|
2010-01-02 02:47:56 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (statusbar), image, FALSE, FALSE, 3); */
|
|
|
|
g_object_set_data_full (G_OBJECT (browser), "status-image", image,
|
|
|
|
(GDestroyNotify)gtk_widget_destroy);
|
2009-10-19 21:25:12 +00:00
|
|
|
|
2012-04-13 17:26:30 +00:00
|
|
|
i = 0;
|
|
|
|
while((view = midori_browser_get_nth_tab(browser, i++)))
|
|
|
|
adblock_add_tab_cb (browser, MIDORI_VIEW (view), extension);
|
|
|
|
|
2009-10-19 21:25:12 +00:00
|
|
|
g_signal_connect (browser, "add-tab",
|
2010-01-02 02:47:56 +00:00
|
|
|
G_CALLBACK (adblock_add_tab_cb), extension);
|
2012-04-12 21:17:02 +00:00
|
|
|
g_signal_connect (browser, "remove-tab",
|
|
|
|
G_CALLBACK (adblock_remove_tab_cb), extension);
|
2011-06-24 21:55:08 +00:00
|
|
|
g_signal_connect (extension, "open-preferences",
|
|
|
|
G_CALLBACK (adblock_open_preferences_cb), extension);
|
2009-09-09 21:53:30 +00:00
|
|
|
g_signal_connect (extension, "deactivate",
|
2010-01-02 02:47:56 +00:00
|
|
|
G_CALLBACK (adblock_deactivate_cb), browser);
|
2009-10-19 21:25:12 +00:00
|
|
|
g_object_unref (statusbar);
|
2009-05-19 19:01:09 +00:00
|
|
|
}
|
|
|
|
|
2011-10-08 15:07:55 +00:00
|
|
|
static GString*
|
2011-06-06 14:30:30 +00:00
|
|
|
adblock_fixup_regexp (const gchar* prefix,
|
|
|
|
gchar* src)
|
2009-12-18 18:56:10 +00:00
|
|
|
{
|
|
|
|
GString* str;
|
2011-06-18 13:45:20 +00:00
|
|
|
int len = 0;
|
2009-12-18 18:56:10 +00:00
|
|
|
|
|
|
|
if (!src)
|
|
|
|
return NULL;
|
|
|
|
|
2011-06-06 14:30:30 +00:00
|
|
|
str = g_string_new (prefix);
|
2009-12-18 18:56:10 +00:00
|
|
|
|
|
|
|
/* lets strip first .* */
|
|
|
|
if (src[0] == '*')
|
|
|
|
{
|
|
|
|
(void)*src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
switch (*src)
|
|
|
|
{
|
|
|
|
case '*':
|
|
|
|
g_string_append (str, ".*");
|
|
|
|
break;
|
2010-01-10 21:52:06 +00:00
|
|
|
/*case '.':
|
2009-12-18 18:56:10 +00:00
|
|
|
g_string_append (str, "\\.");
|
2010-01-10 21:52:06 +00:00
|
|
|
break;*/
|
2009-12-18 18:56:10 +00:00
|
|
|
case '?':
|
|
|
|
g_string_append (str, "\\?");
|
|
|
|
break;
|
|
|
|
case '|':
|
|
|
|
/* FIXME: We actually need to match :[0-9]+ or '/'. Sign means
|
|
|
|
"here could be port number or nothing". So bla.com^ will match
|
|
|
|
bla.com/ or bla.com:8080/ but not bla.com.au/ */
|
|
|
|
case '^':
|
2010-06-30 15:48:37 +00:00
|
|
|
case '+':
|
|
|
|
break;
|
2009-12-18 18:56:10 +00:00
|
|
|
default:
|
|
|
|
g_string_append_printf (str,"%c", *src);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
while (*src);
|
|
|
|
|
2011-10-08 15:07:55 +00:00
|
|
|
len = str->len;
|
2009-12-18 18:56:10 +00:00
|
|
|
/* We dont need .* in the end of url. Thats stupid */
|
2011-10-08 15:07:55 +00:00
|
|
|
if (str->str && str->str[len-1] == '*' && str->str[len-2] == '.')
|
|
|
|
g_string_erase (str, len-2, 2);
|
|
|
|
|
|
|
|
return str;
|
2009-12-18 18:56:10 +00:00
|
|
|
}
|
|
|
|
|
2011-10-08 15:07:55 +00:00
|
|
|
static gboolean
|
|
|
|
adblock_compile_regexp (GString* gpatt,
|
|
|
|
gchar* opts)
|
2009-11-20 00:00:48 +00:00
|
|
|
{
|
|
|
|
GRegex* regex;
|
|
|
|
GError* error = NULL;
|
2009-12-18 18:56:10 +00:00
|
|
|
int pos = 0;
|
|
|
|
gchar *sig;
|
2011-10-08 15:07:55 +00:00
|
|
|
gchar *patt;
|
|
|
|
int len;
|
2009-11-20 00:00:48 +00:00
|
|
|
|
2011-10-08 15:07:55 +00:00
|
|
|
if (!gpatt)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
patt = gpatt->str;
|
|
|
|
len = gpatt->len;
|
2011-06-18 13:45:20 +00:00
|
|
|
|
2009-11-20 00:00:48 +00:00
|
|
|
/* TODO: Play with optimization flags */
|
|
|
|
regex = g_regex_new (patt, G_REGEX_OPTIMIZE,
|
|
|
|
G_REGEX_MATCH_NOTEMPTY, &error);
|
2009-12-18 18:56:10 +00:00
|
|
|
if (error)
|
2009-11-20 00:00:48 +00:00
|
|
|
{
|
|
|
|
g_warning ("%s: %s", G_STRFUNC, error->message);
|
|
|
|
g_error_free (error);
|
2011-10-08 15:07:55 +00:00
|
|
|
return TRUE;
|
2009-12-18 18:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!g_regex_match_simple ("^/.*[\\^\\$\\*].*/$", patt, G_REGEX_UNGREEDY, G_REGEX_MATCH_NOTEMPTY))
|
|
|
|
{
|
2010-01-14 00:52:43 +00:00
|
|
|
int signature_count = 0;
|
2011-07-02 18:11:24 +00:00
|
|
|
|
2009-12-18 18:56:10 +00:00
|
|
|
for (pos = len - SIGNATURE_SIZE; pos >= 0; pos--) {
|
2009-12-28 22:57:47 +00:00
|
|
|
sig = g_strndup (patt + pos, SIGNATURE_SIZE);
|
2009-12-18 18:56:10 +00:00
|
|
|
if (!g_regex_match_simple ("[\\*]", sig, G_REGEX_UNGREEDY, G_REGEX_MATCH_NOTEMPTY) &&
|
2011-07-02 18:11:24 +00:00
|
|
|
!g_hash_table_lookup (keys, sig))
|
2009-12-18 18:56:10 +00:00
|
|
|
{
|
2011-12-18 01:07:55 +00:00
|
|
|
#ifdef G_ENABLE_DEBUG
|
2010-04-08 19:39:05 +00:00
|
|
|
adblock_debug ("sig: %s %s", sig, patt);
|
2011-12-18 01:07:55 +00:00
|
|
|
#endif
|
2011-07-02 18:11:24 +00:00
|
|
|
g_hash_table_insert (keys, sig, regex);
|
|
|
|
g_hash_table_insert (optslist, sig, g_strdup (opts));
|
2010-01-14 00:52:43 +00:00
|
|
|
signature_count++;
|
2009-12-18 18:56:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (g_regex_match_simple ("^\\*", sig, G_REGEX_UNGREEDY, G_REGEX_MATCH_NOTEMPTY) &&
|
2011-07-02 18:11:24 +00:00
|
|
|
!g_hash_table_lookup (pattern, patt))
|
2009-12-18 18:56:10 +00:00
|
|
|
{
|
2011-12-18 01:07:55 +00:00
|
|
|
#ifdef G_ENABLE_DEBUG
|
2010-04-08 19:39:05 +00:00
|
|
|
adblock_debug ("patt2: %s %s", sig, patt);
|
2011-12-18 01:07:55 +00:00
|
|
|
#endif
|
2011-07-02 18:11:24 +00:00
|
|
|
g_hash_table_insert (pattern, patt, regex);
|
|
|
|
g_hash_table_insert (optslist, patt, g_strdup (opts));
|
2009-12-18 18:56:10 +00:00
|
|
|
}
|
|
|
|
g_free (sig);
|
|
|
|
}
|
|
|
|
}
|
2011-07-02 18:11:24 +00:00
|
|
|
if (signature_count > 1 && g_hash_table_lookup (pattern, patt))
|
2011-10-08 15:07:55 +00:00
|
|
|
{
|
2011-07-02 18:11:24 +00:00
|
|
|
g_hash_table_steal (pattern, patt);
|
2011-10-08 15:07:55 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
2009-12-18 18:56:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-18 01:07:55 +00:00
|
|
|
#ifdef G_ENABLE_DEBUG
|
2010-04-08 19:39:05 +00:00
|
|
|
adblock_debug ("patt: %s%s", patt, "");
|
2011-12-18 01:07:55 +00:00
|
|
|
#endif
|
2009-12-18 18:56:10 +00:00
|
|
|
/* Pattern is a regexp chars */
|
2011-07-02 18:11:24 +00:00
|
|
|
g_hash_table_insert (pattern, patt, regex);
|
|
|
|
g_hash_table_insert (optslist, patt, g_strdup (opts));
|
2011-10-08 15:07:55 +00:00
|
|
|
return FALSE;
|
2009-11-20 00:00:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-06 16:52:07 +00:00
|
|
|
static inline gchar*
|
2011-06-06 14:30:30 +00:00
|
|
|
adblock_add_url_pattern (gchar* prefix,
|
2009-11-26 22:45:38 +00:00
|
|
|
gchar* type,
|
|
|
|
gchar* line)
|
|
|
|
{
|
|
|
|
gchar** data;
|
|
|
|
gchar* patt;
|
2011-10-08 15:07:55 +00:00
|
|
|
GString* format_patt;
|
2009-11-26 22:45:38 +00:00
|
|
|
gchar* opts;
|
2011-10-08 15:07:55 +00:00
|
|
|
gboolean should_free;
|
2009-11-26 22:45:38 +00:00
|
|
|
|
|
|
|
data = g_strsplit (line, "$", -1);
|
2011-06-18 13:45:20 +00:00
|
|
|
if (!data || !data[0])
|
2011-11-28 20:21:29 +00:00
|
|
|
{
|
|
|
|
g_strfreev (data);
|
2011-06-18 13:45:20 +00:00
|
|
|
return NULL;
|
2011-11-28 20:21:29 +00:00
|
|
|
}
|
2011-06-18 13:45:20 +00:00
|
|
|
|
|
|
|
if (data[1] && data[2])
|
2009-11-26 22:45:38 +00:00
|
|
|
{
|
2011-06-06 14:30:30 +00:00
|
|
|
patt = g_strconcat (data[0], data[1], NULL);
|
2011-07-02 18:11:24 +00:00
|
|
|
opts = g_strconcat (type, ",", data[2], NULL);
|
2009-11-26 22:45:38 +00:00
|
|
|
}
|
2011-06-18 13:45:20 +00:00
|
|
|
else if (data[1])
|
2009-11-26 22:45:38 +00:00
|
|
|
{
|
2011-06-06 14:30:30 +00:00
|
|
|
patt = data[0];
|
2011-07-02 18:11:24 +00:00
|
|
|
opts = g_strconcat (type, ",", data[1], NULL);
|
2009-11-26 22:45:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-06-06 14:30:30 +00:00
|
|
|
patt = data[0];
|
2011-11-28 20:21:29 +00:00
|
|
|
opts = type;
|
2009-11-26 22:45:38 +00:00
|
|
|
}
|
|
|
|
|
2011-06-23 02:27:39 +00:00
|
|
|
if (g_regex_match_simple ("subdocument", opts,
|
|
|
|
G_REGEX_CASELESS, G_REGEX_MATCH_NOTEMPTY))
|
|
|
|
{
|
2011-11-28 20:21:29 +00:00
|
|
|
if (data[1] && data[2])
|
|
|
|
g_free (patt);
|
|
|
|
if (data[1])
|
|
|
|
g_free (opts);
|
2011-11-28 22:31:17 +00:00
|
|
|
g_strfreev (data);
|
2011-06-23 02:27:39 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-06-06 14:30:30 +00:00
|
|
|
format_patt = adblock_fixup_regexp (prefix, patt);
|
2009-11-26 22:45:38 +00:00
|
|
|
|
2011-12-18 01:07:55 +00:00
|
|
|
#ifdef G_ENABLE_DEBUG
|
2011-10-08 15:07:55 +00:00
|
|
|
adblock_debug ("got: %s opts %s", format_patt->str, opts);
|
2011-12-18 01:07:55 +00:00
|
|
|
#endif
|
2011-10-08 15:07:55 +00:00
|
|
|
should_free = adblock_compile_regexp (format_patt, opts);
|
2009-11-26 22:45:38 +00:00
|
|
|
|
2011-11-28 20:21:29 +00:00
|
|
|
if (data[1] && data[2])
|
|
|
|
g_free (patt);
|
|
|
|
if (data[1])
|
|
|
|
g_free (opts);
|
2011-11-28 22:31:17 +00:00
|
|
|
g_strfreev (data);
|
2011-10-08 15:07:55 +00:00
|
|
|
|
|
|
|
return g_string_free (format_patt, should_free);
|
2009-11-26 22:45:38 +00:00
|
|
|
}
|
|
|
|
|
2011-06-06 16:52:07 +00:00
|
|
|
static inline void
|
2009-09-14 16:25:08 +00:00
|
|
|
adblock_frame_add (gchar* line)
|
|
|
|
{
|
2011-06-05 15:44:52 +00:00
|
|
|
const gchar* separator = " , ";
|
2009-10-09 15:17:26 +00:00
|
|
|
|
2009-09-14 16:25:08 +00:00
|
|
|
(void)*line++;
|
|
|
|
(void)*line++;
|
2010-01-26 21:03:47 +00:00
|
|
|
if (strchr (line, '\'')
|
|
|
|
|| (strchr (line, ':')
|
2010-01-03 22:37:44 +00:00
|
|
|
&& !g_regex_match_simple (".*\\[.*:.*\\].*", line,
|
2010-01-26 21:03:47 +00:00
|
|
|
G_REGEX_CASELESS, G_REGEX_MATCH_NOTEMPTY)))
|
2010-01-03 22:37:44 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2011-06-05 15:44:52 +00:00
|
|
|
g_string_append (blockcss, separator);
|
|
|
|
g_string_append (blockcss, line);
|
2009-09-14 16:25:08 +00:00
|
|
|
}
|
|
|
|
|
2012-09-02 19:20:35 +00:00
|
|
|
static void
|
2011-12-18 01:07:55 +00:00
|
|
|
adblock_update_css_hash (gchar* domain,
|
|
|
|
gchar* value)
|
|
|
|
{
|
|
|
|
const gchar* olddata;
|
|
|
|
gchar* newdata;
|
|
|
|
|
|
|
|
if ((olddata = g_hash_table_lookup (blockcssprivate, domain)))
|
|
|
|
{
|
|
|
|
newdata = g_strconcat (olddata, " , ", value, NULL);
|
|
|
|
g_hash_table_replace (blockcssprivate, g_strdup (domain), newdata);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
g_hash_table_insert (blockcssprivate, g_strdup (domain), g_strdup (value));
|
|
|
|
}
|
|
|
|
|
2011-06-06 16:52:07 +00:00
|
|
|
static inline void
|
2009-11-26 22:45:38 +00:00
|
|
|
adblock_frame_add_private (const gchar* line,
|
|
|
|
const gchar* sep)
|
2009-10-19 21:16:05 +00:00
|
|
|
{
|
|
|
|
gchar** data;
|
2009-11-26 22:45:38 +00:00
|
|
|
data = g_strsplit (line, sep, 2);
|
2009-10-19 21:16:05 +00:00
|
|
|
|
2010-01-03 22:37:44 +00:00
|
|
|
if (!(data[1] && *data[1])
|
2010-01-26 21:03:47 +00:00
|
|
|
|| strchr (data[1], '\'')
|
2010-01-03 22:37:44 +00:00
|
|
|
|| (strchr (data[1], ':')
|
|
|
|
&& !g_regex_match_simple (".*\\[.*:.*\\].*", data[1],
|
|
|
|
G_REGEX_CASELESS, G_REGEX_MATCH_NOTEMPTY)))
|
|
|
|
{
|
|
|
|
g_strfreev (data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strchr (data[0], ','))
|
2009-10-19 21:16:05 +00:00
|
|
|
{
|
|
|
|
gchar** domains;
|
2011-04-29 01:41:04 +00:00
|
|
|
gint i;
|
2009-11-20 00:00:48 +00:00
|
|
|
|
2009-10-19 21:16:05 +00:00
|
|
|
domains = g_strsplit (data[0], ",", -1);
|
2011-04-29 01:41:04 +00:00
|
|
|
for (i = 0; domains[i]; i++)
|
2009-10-19 21:16:05 +00:00
|
|
|
{
|
2011-12-06 00:24:08 +00:00
|
|
|
gchar* domain;
|
|
|
|
|
|
|
|
domain = domains[i];
|
|
|
|
/* Ignore Firefox-specific option */
|
|
|
|
if (!g_strcmp0 (domain, "~pregecko2"))
|
|
|
|
continue;
|
2012-03-11 19:43:24 +00:00
|
|
|
/* FIXME: ~ should negate match */
|
2011-12-06 00:24:08 +00:00
|
|
|
if (domain[0] == '~')
|
|
|
|
domain++;
|
2011-12-18 01:07:55 +00:00
|
|
|
adblock_update_css_hash (g_strstrip (domain), data[1]);
|
2009-10-19 21:16:05 +00:00
|
|
|
}
|
|
|
|
g_strfreev (domains);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-18 01:07:55 +00:00
|
|
|
adblock_update_css_hash (data[0], data[1]);
|
2009-10-19 21:16:05 +00:00
|
|
|
}
|
|
|
|
g_strfreev (data);
|
|
|
|
}
|
|
|
|
|
2009-07-18 01:05:40 +00:00
|
|
|
static gchar*
|
|
|
|
adblock_parse_line (gchar* line)
|
|
|
|
{
|
2012-03-11 19:43:24 +00:00
|
|
|
/*
|
|
|
|
* AdblockPlus rule reference based on http://adblockplus.org/en/filters
|
|
|
|
* Block URL:
|
|
|
|
* http://example.com/ads/banner123.gif
|
|
|
|
* http://example.com/ads/banner*.gif
|
|
|
|
* http://example.com/ads/*
|
|
|
|
* Partial match for "ad":
|
|
|
|
* *ad*
|
|
|
|
* ad
|
|
|
|
* Block example.com/annoyingflash.swf but not example.com/swf/:
|
|
|
|
* swf|
|
|
|
|
* Block bad.example/banner.gif but not good.example/analyze?http://bad.example:
|
|
|
|
* |http://baddomain.example/
|
|
|
|
* Block http(s) example.com but not badexample.com or good.example/analyze?http://bad.example:
|
|
|
|
* ||example.com/banner.gif
|
|
|
|
* Block example.com/ and example.com:8000/ but not example.com.ar/:
|
|
|
|
* http://example.com^
|
|
|
|
* A ^ matches anything that isn't A-Za-z0-0_-.%
|
|
|
|
* Block example.com:8000/foo.bar?a=12&b=%D1%82%D0%B5:
|
|
|
|
* ^example.com^
|
|
|
|
* ^%D1%82%D0%B5^
|
|
|
|
* ^foo.bar^
|
|
|
|
* TODO: ^ is partially supported by Midori
|
|
|
|
* Block banner123 and banner321 with a regex:
|
|
|
|
* /banner\d+/
|
|
|
|
* Never block URIs with "advice":
|
|
|
|
* @@advice
|
|
|
|
* No blocking at all:
|
|
|
|
* @@http://example.com
|
|
|
|
* @@|http://example.com
|
|
|
|
* TODO: @@ is currently ignored by Midori.
|
|
|
|
* Element hiding by class:
|
|
|
|
* ##textad
|
|
|
|
* ##div.textad
|
|
|
|
* Element hiding by id:
|
|
|
|
* ##div#sponsorad
|
|
|
|
* ##*#sponsorad
|
|
|
|
* Match example.com/ and something.example.com/ but not example.org/
|
|
|
|
* example.com##*.sponsor
|
|
|
|
* Match multiple domains:
|
|
|
|
* domain1.example,domain2.example,domain3.example##*.sponsor
|
|
|
|
* Match on any domain but "example.com":
|
|
|
|
* ~example.com##*.sponsor
|
|
|
|
* Match on "example.com" except "foo.example.com":
|
|
|
|
* example.com,~foo.example.com##*.sponsor
|
|
|
|
* By design rules only apply to full domain names:
|
|
|
|
* "domain" is NOT equal to "domain.example,domain.test."
|
|
|
|
* In Firefox rules can apply to browser UI:
|
|
|
|
* browser##menuitem#javascriptConsole will hide the Console menuitem
|
|
|
|
* Hide tables with width attribute 80%:
|
|
|
|
* ##table[width="80%"]
|
|
|
|
* Hide all div with title attribute containing "adv":
|
|
|
|
* ##div[title*="adv"]
|
|
|
|
* Hide div with title starting with "adv" and ending with "ert":
|
|
|
|
* ##div[title^="adv"][title$="ert"]
|
|
|
|
* Match tables with width attribute 80% and bgcolor attribute white:
|
|
|
|
* table[width="80%"][bgcolor="white"]
|
|
|
|
* TODO: [] is currently ignored by Midori
|
|
|
|
* Hide anything following div with class "adheader":
|
|
|
|
* ##div.adheader + *
|
|
|
|
* Old CSS element hiding syntax, officially deprecated:
|
|
|
|
* #div(id=foo)
|
|
|
|
* Match anything but "example.com"
|
|
|
|
* ~example.com##*.sponsor
|
|
|
|
* TODO: ~ is currently ignored by Midori
|
|
|
|
* Match "example.com" domain except "foo.example.com":
|
|
|
|
* example.com,~foo.example.com##*.sponsor
|
|
|
|
* ! Comment
|
|
|
|
* Supported options after a trailing $:
|
|
|
|
* domain,third-party,~pregecko2
|
|
|
|
* Official options (not all supported by Midori):
|
|
|
|
* script,image,stylesheet,object,xmlhttprequest,object-subrequest,
|
|
|
|
* subdocument,document,elemhide,popup,third-party,sitekey,match-case
|
|
|
|
* collapse,donottrack,pregecko2
|
|
|
|
* Deprecated:
|
|
|
|
* background,xbl,ping,dtd
|
|
|
|
* Inverse options:
|
|
|
|
* ~script,~image,~stylesheet,~object,~xmlhttprequest,~collapse,
|
|
|
|
* ~object-subrequest,~subdocument,~document,~elemhide,~third-party,
|
|
|
|
* ~pregecko2
|
|
|
|
**/
|
|
|
|
|
2012-03-06 19:14:15 +00:00
|
|
|
/* Skip invalid, empty and comment lines */
|
|
|
|
if (!(line && line[0] != ' ' && line[0] != '!' && line[0]))
|
2009-07-18 01:05:40 +00:00
|
|
|
return NULL;
|
2012-03-06 19:14:15 +00:00
|
|
|
|
2009-07-18 01:05:40 +00:00
|
|
|
/* FIXME: No support for whitelisting */
|
|
|
|
if (line[0] == '@' && line[1] == '@')
|
|
|
|
return NULL;
|
2009-11-20 00:00:48 +00:00
|
|
|
/* FIXME: No support for [include] and [exclude] tags */
|
|
|
|
if (line[0] == '[')
|
2009-09-15 19:01:04 +00:00
|
|
|
return NULL;
|
2009-11-20 00:00:48 +00:00
|
|
|
|
2012-03-06 19:14:15 +00:00
|
|
|
g_strchomp (line);
|
2011-06-18 13:45:20 +00:00
|
|
|
|
2009-11-20 00:00:48 +00:00
|
|
|
/* Got CSS block hider */
|
|
|
|
if (line[0] == '#' && line[1] == '#' )
|
2009-09-14 16:25:08 +00:00
|
|
|
{
|
|
|
|
adblock_frame_add (line);
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-11-26 22:45:38 +00:00
|
|
|
/* Got CSS block hider. Workaround */
|
2009-11-20 00:00:48 +00:00
|
|
|
if (line[0] == '#')
|
2009-07-18 01:05:40 +00:00
|
|
|
return NULL;
|
2009-10-19 21:16:05 +00:00
|
|
|
|
2009-11-20 00:00:48 +00:00
|
|
|
/* Got per domain CSS hider rule */
|
2010-01-03 22:37:44 +00:00
|
|
|
if (strstr (line, "##"))
|
2009-10-19 21:16:05 +00:00
|
|
|
{
|
2010-01-03 22:37:44 +00:00
|
|
|
adblock_frame_add_private (line, "##");
|
2009-09-14 16:25:08 +00:00
|
|
|
return NULL;
|
2009-10-19 21:16:05 +00:00
|
|
|
}
|
2009-11-26 22:45:38 +00:00
|
|
|
/* Got per domain CSS hider rule. Workaround */
|
2010-01-03 22:37:44 +00:00
|
|
|
if (strchr (line, '#'))
|
2009-11-26 22:45:38 +00:00
|
|
|
{
|
2010-01-03 22:37:44 +00:00
|
|
|
adblock_frame_add_private (line, "#");
|
2009-11-26 22:45:38 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-03-06 19:14:15 +00:00
|
|
|
|
2009-11-20 00:00:48 +00:00
|
|
|
/* Got URL blocker rule */
|
2009-11-26 22:45:38 +00:00
|
|
|
if (line[0] == '|' && line[1] == '|' )
|
|
|
|
{
|
|
|
|
(void)*line++;
|
|
|
|
(void)*line++;
|
2011-06-06 14:30:30 +00:00
|
|
|
return adblock_add_url_pattern ("", "fulluri", line);
|
2009-11-26 22:45:38 +00:00
|
|
|
}
|
|
|
|
if (line[0] == '|')
|
|
|
|
{
|
|
|
|
(void)*line++;
|
2011-06-06 14:30:30 +00:00
|
|
|
return adblock_add_url_pattern ("^", "fulluri", line);
|
2009-11-26 22:45:38 +00:00
|
|
|
}
|
2011-06-06 14:30:30 +00:00
|
|
|
return adblock_add_url_pattern ("", "uri", line);
|
2009-07-18 01:05:40 +00:00
|
|
|
}
|
|
|
|
|
2012-08-31 23:57:17 +00:00
|
|
|
static GDateMonth
|
|
|
|
str_month_name_to_gdate (const gchar* month)
|
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
const gchar* months[] = {
|
|
|
|
"", "January", "February", "March", "April", "May", "June",
|
|
|
|
"July", "August", "September", "October", "November", "December"
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i = 0; i < G_N_ELEMENTS (months); i++)
|
|
|
|
{
|
|
|
|
if (strncmp (month, months[i], 3) == 0)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
adblock_file_is_up_to_date (gchar* path)
|
|
|
|
{
|
|
|
|
FILE* file;
|
|
|
|
gchar line[2000];
|
|
|
|
|
|
|
|
/* Check a chunk of header for update info */
|
|
|
|
if ((file = g_fopen (path, "r")))
|
|
|
|
{
|
2012-09-05 22:00:46 +00:00
|
|
|
gint days_to_expire = 0;
|
2012-09-05 21:58:21 +00:00
|
|
|
gchar* timestamp = NULL;
|
2012-08-31 23:57:17 +00:00
|
|
|
guint i;
|
2012-09-05 21:58:21 +00:00
|
|
|
gboolean found_meta = FALSE;
|
2012-09-07 17:59:37 +00:00
|
|
|
gint fs_days_elapsed, days_elapsed = 0, least_days;
|
2012-09-05 21:58:21 +00:00
|
|
|
|
2012-08-31 23:57:17 +00:00
|
|
|
for (i = 0; i <= 10; i++)
|
|
|
|
{
|
|
|
|
fgets (line, 2000, file);
|
|
|
|
if (strncmp ("! Expires", line, 9) == 0)
|
|
|
|
{
|
|
|
|
gchar** parts = g_strsplit (line, " ", 4);
|
|
|
|
days_to_expire = atoi (parts[2]);
|
|
|
|
g_strfreev (parts);
|
2012-09-05 21:58:21 +00:00
|
|
|
found_meta = TRUE;
|
2012-08-31 23:57:17 +00:00
|
|
|
}
|
2012-09-02 19:25:56 +00:00
|
|
|
if (strncmp ("! This list expires after", line, 25) == 0)
|
|
|
|
{
|
|
|
|
gchar** parts = g_strsplit (line, " ", 7);
|
|
|
|
|
|
|
|
if (strncmp (parts[6], "days", 4) == 0)
|
|
|
|
days_to_expire = atoi (parts[5]);
|
|
|
|
if (strncmp (parts[6], "hours", 5) == 0)
|
|
|
|
days_to_expire = (atoi (parts[5])) / 24;
|
|
|
|
|
|
|
|
g_strfreev (parts);
|
2012-09-05 21:58:21 +00:00
|
|
|
found_meta = TRUE;
|
2012-09-02 19:25:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strncmp ("! Last modified", line, 15) == 0
|
|
|
|
|| strncmp ("! Updated", line, 9) == 0)
|
2012-08-31 23:57:17 +00:00
|
|
|
{
|
|
|
|
gchar** parts = g_strsplit (line, ":", 2);
|
|
|
|
timestamp = g_strdup (parts[1] + 1);
|
|
|
|
g_strchomp (timestamp);
|
|
|
|
g_strfreev (parts);
|
2012-09-05 21:58:21 +00:00
|
|
|
found_meta = TRUE;
|
2012-08-31 23:57:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-05 21:58:21 +00:00
|
|
|
if (!found_meta)
|
|
|
|
{
|
|
|
|
g_print ("Adblock: no metadata found in %s (broken download?)\n", path);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-09-07 17:59:37 +00:00
|
|
|
/* query filesystem about file change, maybe there is no update yet
|
|
|
|
* or there is no "modified" metadata to check, otherwise we will repeatedly
|
|
|
|
* download files that have no new updates */
|
2012-08-31 23:57:17 +00:00
|
|
|
{
|
2012-09-07 17:59:37 +00:00
|
|
|
GDate* current = g_date_new ();
|
|
|
|
GDate* fs_mod_date = g_date_new ();
|
|
|
|
GTimeVal mod_time;
|
|
|
|
GFile* filter_file = g_file_new_for_path (path);
|
|
|
|
GFileInfo* info = g_file_query_info (filter_file, "time:modified", 0, NULL, NULL);
|
|
|
|
|
|
|
|
g_file_info_get_modification_time (info, &mod_time);
|
|
|
|
g_date_set_time_t (current, time (NULL));
|
|
|
|
g_date_set_time_val (fs_mod_date, &mod_time);
|
|
|
|
|
|
|
|
fs_days_elapsed = g_date_days_between (fs_mod_date, current);
|
|
|
|
|
|
|
|
g_date_free (current);
|
|
|
|
g_date_free (fs_mod_date);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If there is no update metadata but file is valid, assume one week */
|
|
|
|
if ((!days_to_expire && !timestamp) && fs_days_elapsed < 7)
|
|
|
|
return TRUE;
|
2012-08-31 23:57:17 +00:00
|
|
|
|
2012-09-07 17:59:37 +00:00
|
|
|
if (days_to_expire && timestamp != NULL)
|
|
|
|
{
|
2012-08-31 23:57:17 +00:00
|
|
|
GDate* current = g_date_new ();
|
|
|
|
GDate* mod_date = g_date_new ();
|
2012-09-02 19:25:56 +00:00
|
|
|
gchar** parts;
|
|
|
|
gboolean use_dots = FALSE;
|
|
|
|
|
|
|
|
/* Common dates are 20 Mar 2012, 20.08.2012 */
|
|
|
|
if (g_strrstr (timestamp, "."))
|
|
|
|
{
|
|
|
|
use_dots = TRUE;
|
|
|
|
parts = g_strsplit (timestamp, ".", 4);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
parts = g_strsplit (timestamp, " ", 4);
|
2012-08-31 23:57:17 +00:00
|
|
|
|
|
|
|
g_date_set_day (mod_date, atoi (parts[0]));
|
2012-09-02 19:25:56 +00:00
|
|
|
|
|
|
|
if (use_dots)
|
|
|
|
g_date_set_month (mod_date, atoi (parts[1]));
|
|
|
|
else
|
|
|
|
g_date_set_month (mod_date, str_month_name_to_gdate (parts[1]));
|
|
|
|
|
2012-08-31 23:57:17 +00:00
|
|
|
g_date_set_year (mod_date, atoi (parts[2]));
|
|
|
|
g_strfreev (parts);
|
|
|
|
|
|
|
|
g_date_set_time_t (current, time (NULL));
|
|
|
|
days_elapsed = g_date_days_between (mod_date, current);
|
|
|
|
|
|
|
|
g_date_free (current);
|
|
|
|
g_date_free (mod_date);
|
|
|
|
g_free (timestamp);
|
2012-09-07 17:59:37 +00:00
|
|
|
}
|
2012-08-31 23:57:17 +00:00
|
|
|
|
2012-09-07 17:59:37 +00:00
|
|
|
/* File from the future? Assume up to date */
|
|
|
|
if (days_elapsed < 0)
|
|
|
|
{
|
|
|
|
g_print ("Adblock: file %s appears to be from the future,"
|
|
|
|
"check your system clock!\n", path);
|
|
|
|
return TRUE;
|
2012-08-31 23:57:17 +00:00
|
|
|
}
|
2012-09-07 17:59:37 +00:00
|
|
|
|
|
|
|
least_days = days_elapsed < fs_days_elapsed ? days_elapsed : fs_days_elapsed;
|
|
|
|
if (least_days < days_to_expire)
|
|
|
|
return TRUE;
|
2012-08-31 23:57:17 +00:00
|
|
|
else
|
2012-09-07 17:59:37 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2012-08-31 23:57:17 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-09-01 15:54:30 +00:00
|
|
|
static void
|
2009-07-18 01:05:40 +00:00
|
|
|
adblock_parse_file (gchar* path)
|
2009-05-19 19:01:09 +00:00
|
|
|
{
|
|
|
|
FILE* file;
|
2010-01-03 22:37:44 +00:00
|
|
|
gchar line[2000];
|
2009-11-20 00:00:48 +00:00
|
|
|
|
2009-05-19 19:01:09 +00:00
|
|
|
if ((file = g_fopen (path, "r")))
|
|
|
|
{
|
2010-01-03 22:37:44 +00:00
|
|
|
while (fgets (line, 2000, file))
|
2011-07-02 18:11:24 +00:00
|
|
|
adblock_parse_line (line);
|
2009-07-16 12:13:27 +00:00
|
|
|
fclose (file);
|
2009-05-19 19:01:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-14 16:25:08 +00:00
|
|
|
static void
|
2010-01-02 02:47:56 +00:00
|
|
|
adblock_deactivate_tabs (MidoriView* view,
|
|
|
|
MidoriBrowser* browser,
|
|
|
|
MidoriExtension* extension)
|
2009-09-14 16:25:08 +00:00
|
|
|
{
|
2010-03-30 20:44:04 +00:00
|
|
|
GtkWidget* web_view = midori_view_get_web_view (view);
|
2009-10-19 21:25:12 +00:00
|
|
|
|
2009-09-14 16:25:08 +00:00
|
|
|
g_signal_handlers_disconnect_by_func (
|
|
|
|
web_view, adblock_window_object_cleared_cb, 0);
|
2010-01-02 02:47:56 +00:00
|
|
|
g_signal_handlers_disconnect_by_func (
|
|
|
|
web_view, adblock_populate_popup_cb, extension);
|
2009-09-14 16:25:08 +00:00
|
|
|
g_signal_handlers_disconnect_by_func (
|
2012-04-12 20:56:35 +00:00
|
|
|
web_view, adblock_resource_request_starting_cb, view);
|
2010-01-17 17:28:11 +00:00
|
|
|
g_signal_handlers_disconnect_by_func (
|
2012-04-12 20:56:35 +00:00
|
|
|
web_view, adblock_load_finished_cb, view);
|
2012-04-12 21:17:02 +00:00
|
|
|
g_signal_handlers_disconnect_by_func (
|
2012-06-27 21:58:07 +00:00
|
|
|
web_view, adblock_navigation_policy_decision_requested_cb, extension);
|
2009-09-14 16:25:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
adblock_deactivate_cb (MidoriExtension* extension,
|
2010-01-02 02:47:56 +00:00
|
|
|
MidoriBrowser* browser)
|
2009-09-14 16:25:08 +00:00
|
|
|
{
|
2012-04-13 17:26:30 +00:00
|
|
|
gint i;
|
|
|
|
GtkWidget* view;
|
2009-09-14 16:25:08 +00:00
|
|
|
MidoriApp* app = midori_extension_get_app (extension);
|
2011-11-09 20:41:54 +00:00
|
|
|
MidoriWebSettings* settings = katze_object_get_object (app, "settings");
|
2009-09-14 16:25:08 +00:00
|
|
|
|
|
|
|
g_signal_handlers_disconnect_by_func (
|
2011-06-24 21:55:08 +00:00
|
|
|
browser, adblock_open_preferences_cb, extension);
|
2009-09-14 16:25:08 +00:00
|
|
|
g_signal_handlers_disconnect_by_func (
|
|
|
|
extension, adblock_deactivate_cb, browser);
|
|
|
|
g_signal_handlers_disconnect_by_func (
|
|
|
|
app, adblock_app_add_browser_cb, extension);
|
2010-06-30 20:31:10 +00:00
|
|
|
g_signal_handlers_disconnect_by_func (
|
|
|
|
browser, adblock_add_tab_cb, extension);
|
2012-04-12 21:17:02 +00:00
|
|
|
g_signal_handlers_disconnect_by_func (
|
|
|
|
browser, adblock_remove_tab_cb, extension);
|
2012-04-13 17:26:30 +00:00
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while((view = midori_browser_get_nth_tab(browser, i++)))
|
|
|
|
adblock_deactivate_tabs (MIDORI_VIEW (view), browser, extension);
|
2009-09-14 16:25:08 +00:00
|
|
|
|
2011-12-18 14:52:31 +00:00
|
|
|
adblock_destroy_db ();
|
2011-11-09 20:41:54 +00:00
|
|
|
midori_web_settings_remove_style (settings, "adblock-blockcss");
|
|
|
|
g_object_unref (settings);
|
2009-09-14 16:25:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-19 19:01:09 +00:00
|
|
|
static void
|
|
|
|
adblock_activate_cb (MidoriExtension* extension,
|
|
|
|
MidoriApp* app)
|
|
|
|
{
|
|
|
|
KatzeArray* browsers;
|
|
|
|
MidoriBrowser* browser;
|
2009-10-14 19:51:32 +00:00
|
|
|
|
2010-01-02 20:34:17 +00:00
|
|
|
adblock_reload_rules (extension, FALSE);
|
2009-09-14 16:25:08 +00:00
|
|
|
|
|
|
|
browsers = katze_object_get_object (app, "browsers");
|
2010-09-11 21:39:51 +00:00
|
|
|
KATZE_ARRAY_FOREACH_ITEM (browser, browsers)
|
2009-09-14 16:25:08 +00:00
|
|
|
adblock_app_add_browser_cb (app, browser, extension);
|
|
|
|
g_signal_connect (app, "add-browser",
|
|
|
|
G_CALLBACK (adblock_app_add_browser_cb), extension);
|
|
|
|
|
|
|
|
g_object_unref (browsers);
|
2009-05-19 19:01:09 +00:00
|
|
|
}
|
|
|
|
|
2009-07-18 01:05:40 +00:00
|
|
|
#if G_ENABLE_DEBUG
|
|
|
|
static void
|
|
|
|
test_adblock_parse (void)
|
|
|
|
{
|
2009-12-18 18:56:10 +00:00
|
|
|
adblock_init_db ();
|
2009-07-18 01:05:40 +00:00
|
|
|
g_assert (!adblock_parse_line (NULL));
|
|
|
|
g_assert (!adblock_parse_line ("!"));
|
|
|
|
g_assert (!adblock_parse_line ("@@"));
|
|
|
|
g_assert (!adblock_parse_line ("##"));
|
|
|
|
g_assert (!adblock_parse_line ("["));
|
|
|
|
|
2010-06-30 15:48:37 +00:00
|
|
|
g_assert_cmpstr (adblock_parse_line ("+advert/"), ==, "advert/");
|
2009-12-18 18:56:10 +00:00
|
|
|
g_assert_cmpstr (adblock_parse_line ("*foo"), ==, "foo");
|
|
|
|
g_assert_cmpstr (adblock_parse_line ("f*oo"), ==, "f.*oo");
|
2009-07-18 01:05:40 +00:00
|
|
|
g_assert_cmpstr (adblock_parse_line ("?foo"), ==, "\\?foo");
|
2009-07-22 19:42:33 +00:00
|
|
|
g_assert_cmpstr (adblock_parse_line ("foo?"), ==, "foo\\?");
|
2009-07-18 01:05:40 +00:00
|
|
|
|
2010-01-10 21:52:06 +00:00
|
|
|
g_assert_cmpstr (adblock_parse_line (".*foo/bar"), ==, "..*foo/bar");
|
|
|
|
g_assert_cmpstr (adblock_parse_line ("http://bla.blub/*"), ==, "http://bla.blub/");
|
2009-07-18 01:05:40 +00:00
|
|
|
}
|
|
|
|
|
2012-09-05 21:52:04 +00:00
|
|
|
static void
|
|
|
|
test_subscription_update (void)
|
|
|
|
{
|
|
|
|
gint temp;
|
|
|
|
gchar* filename;
|
|
|
|
|
|
|
|
temp = g_file_open_tmp ("midori_adblock_update_test_XXXXXX", &filename, NULL);
|
|
|
|
close (temp);
|
|
|
|
|
|
|
|
g_file_set_contents (filename, "", -1, NULL);
|
|
|
|
g_assert (!adblock_file_is_up_to_date (filename));
|
|
|
|
|
|
|
|
g_file_set_contents (filename,
|
|
|
|
"[Adblock Plus 1.1]\n"
|
|
|
|
"! Checksum: 48f6Qdo4PsNogsurLvQ71w\n"
|
|
|
|
"! Title: EasyList\n"
|
|
|
|
"! Last modified: 05 Sep 2010 11:00 UTC\n"
|
|
|
|
"! This list expires after 48 hours\n",
|
|
|
|
-1, NULL);
|
|
|
|
g_assert (!adblock_file_is_up_to_date (filename));
|
|
|
|
|
|
|
|
g_file_set_contents (filename,
|
|
|
|
"[Adblock Plus 1.1]\n"
|
|
|
|
"! Checksum: 48f6Qdo4PsNogsurLvQ71w\n"
|
|
|
|
"! Title: EasyList\n"
|
|
|
|
"! Last modified: 05.09.2010 11:00 UTC\n"
|
|
|
|
"! Expires: 2 days (update frequency)\n",
|
|
|
|
-1, NULL);
|
|
|
|
g_assert (!adblock_file_is_up_to_date (filename));
|
|
|
|
|
|
|
|
g_file_set_contents (filename,
|
|
|
|
"[Adblock Plus 1.1]\n"
|
|
|
|
"! Checksum: 48f6Qdo4PsNogsurLvQ71w\n"
|
|
|
|
"! Title: EasyList\n"
|
|
|
|
"! Updated: 05 Nov 2014 11:00 UTC\n"
|
|
|
|
"! Expires: 5 days (update frequency)\n",
|
|
|
|
-1, NULL);
|
|
|
|
g_assert (adblock_file_is_up_to_date (filename));
|
|
|
|
|
|
|
|
g_file_set_contents (filename,
|
|
|
|
"[Adblock]\n"
|
|
|
|
"! dutchblock v3\n"
|
|
|
|
"! This list expires after 14 days\n"
|
|
|
|
"|http://b*.mookie1.com/\n",
|
|
|
|
-1, NULL);
|
|
|
|
g_assert (adblock_file_is_up_to_date (filename));
|
|
|
|
|
|
|
|
g_unlink (filename);
|
|
|
|
g_free (filename);
|
|
|
|
}
|
|
|
|
|
2009-07-18 01:05:40 +00:00
|
|
|
static void
|
|
|
|
test_adblock_pattern (void)
|
|
|
|
{
|
|
|
|
gint temp;
|
|
|
|
gchar* filename;
|
|
|
|
|
|
|
|
temp = g_file_open_tmp ("midori_adblock_match_test_XXXXXX", &filename, NULL);
|
|
|
|
|
2009-11-20 00:00:48 +00:00
|
|
|
/* TODO: Update some tests and add new ones. */
|
2009-07-18 01:05:40 +00:00
|
|
|
g_file_set_contents (filename,
|
2009-07-22 19:42:33 +00:00
|
|
|
"*ads.foo.bar*\n"
|
|
|
|
"*ads.bogus.name*\n"
|
2009-12-18 18:56:10 +00:00
|
|
|
"||^http://ads.bla.blub/*\n"
|
|
|
|
"|http://ads.blub.boing/*$domain=xxx.com\n"
|
|
|
|
"engine.adct.ru/*?\n"
|
|
|
|
"/addyn|*|adtech;\n"
|
|
|
|
"doubleclick.net/pfadx/*.mtvi\n"
|
|
|
|
"objects.tremormedia.com/embed/xml/*.xml?r=\n"
|
|
|
|
"videostrip.com^*/admatcherclient.\n"
|
|
|
|
"test.dom/test?var\n"
|
2010-01-17 17:28:11 +00:00
|
|
|
"/adpage.\n"
|
2009-12-18 18:56:10 +00:00
|
|
|
"br.gcl.ru/cgi-bin/br/",
|
2009-07-18 01:05:40 +00:00
|
|
|
-1, NULL);
|
2009-12-18 18:56:10 +00:00
|
|
|
|
2009-10-09 15:05:07 +00:00
|
|
|
adblock_parse_file (filename);
|
2009-07-18 01:05:40 +00:00
|
|
|
|
2010-01-10 21:52:06 +00:00
|
|
|
g_test_timer_start ();
|
2011-07-02 18:11:24 +00:00
|
|
|
g_assert (adblock_is_matched ("http://www.engadget.com/_uac/adpage.html", ""));
|
|
|
|
g_assert (adblock_is_matched ("http://test.dom/test?var=1", ""));
|
|
|
|
g_assert (adblock_is_matched ("http://ads.foo.bar/teddy", ""));
|
|
|
|
g_assert (!adblock_is_matched ("http://ads.fuu.bar/teddy", ""));
|
|
|
|
g_assert (adblock_is_matched ("https://ads.bogus.name/blub", ""));
|
|
|
|
g_assert (adblock_is_matched ("http://ads.bla.blub/kitty", ""));
|
|
|
|
g_assert (adblock_is_matched ("http://ads.blub.boing/soda", ""));
|
|
|
|
g_assert (!adblock_is_matched ("http://ads.foo.boing/beer", ""));
|
|
|
|
g_assert (adblock_is_matched ("https://testsub.engine.adct.ru/test?id=1", ""));
|
2009-12-18 18:56:10 +00:00
|
|
|
if (USE_PATTERN_MATCHING)
|
2011-07-02 18:11:24 +00:00
|
|
|
g_assert (adblock_is_matched ("http://test.ltd/addyn/test/test?var=adtech;&var2=1", ""));
|
|
|
|
g_assert (adblock_is_matched ("http://add.doubleclick.net/pfadx/aaaa.mtvi", ""));
|
|
|
|
g_assert (!adblock_is_matched ("http://add.doubleclick.net/pfadx/aaaa.mtv", ""));
|
|
|
|
g_assert (adblock_is_matched ("http://objects.tremormedia.com/embed/xml/list.xml?r=", ""));
|
|
|
|
g_assert (!adblock_is_matched ("http://qq.videostrip.c/sub/admatcherclient.php", ""));
|
|
|
|
g_assert (adblock_is_matched ("http://qq.videostrip.com/sub/admatcherclient.php", ""));
|
|
|
|
g_assert (adblock_is_matched ("http://qq.videostrip.com/sub/admatcherclient.php", ""));
|
|
|
|
g_assert (adblock_is_matched ("http://br.gcl.ru/cgi-bin/br/test", ""));
|
|
|
|
g_assert (!adblock_is_matched ("https://bugs.webkit.org/buglist.cgi?query_format=advanced&short_desc_type=allwordssubstr&short_desc=&long_desc_type=substring&long_desc=&bug_file_loc_type=allwordssubstr&bug_file_loc=&keywords_type=allwords&keywords=&bug_status=UNCONFIRMED&bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&emailassigned_to1=1&emailtype1=substring&email1=&emailassigned_to2=1&emailreporter2=1&emailcc2=1&emailtype2=substring&email2=&bugidtype=include&bug_id=&votes=&chfieldfrom=&chfieldto=Now&chfieldvalue=&query_based_on=gtkport&field0-0-0=keywords&type0-0-0=anywordssubstr&value0-0-0=Gtk%20Cairo%20soup&field0-0-1=short_desc&type0-0-1=anywordssubstr&value0-0-1=Gtk%20Cairo%20soup%20autoconf%20automake%20autotool&field0-0-2=component&type0-0-2=equals&value0-0-2=WebKit%20Gtk", ""));
|
|
|
|
g_assert (!adblock_is_matched ("http://www.engadget.com/2009/09/24/google-hits-android-rom-modder-with-a-cease-and-desist-letter/", ""));
|
|
|
|
g_assert (!adblock_is_matched ("http://karibik-invest.com/es/bienes_raices/search.php?sqT=19&sqN=&sqMp=&sqL=0&qR=1&sqMb=&searchMode=1&action=B%FAsqueda", ""));
|
|
|
|
g_assert (!adblock_is_matched ("http://google.com", ""));
|
2010-01-10 21:52:06 +00:00
|
|
|
|
|
|
|
g_print ("Search took %f seconds\n", g_test_timer_elapsed ());
|
2009-07-18 01:05:40 +00:00
|
|
|
|
|
|
|
close (temp);
|
|
|
|
g_unlink (filename);
|
2009-10-10 09:08:29 +00:00
|
|
|
|
|
|
|
g_hash_table_destroy (pattern);
|
2009-07-18 01:05:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
extension_test (void)
|
|
|
|
{
|
|
|
|
g_test_add_func ("/extensions/adblock/parse", test_adblock_parse);
|
|
|
|
g_test_add_func ("/extensions/adblock/pattern", test_adblock_pattern);
|
2012-09-05 21:52:04 +00:00
|
|
|
g_test_add_func ("/extensions/adblock/update", test_subscription_update);
|
2009-07-18 01:05:40 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-05-19 19:01:09 +00:00
|
|
|
MidoriExtension*
|
|
|
|
extension_init (void)
|
|
|
|
{
|
|
|
|
MidoriExtension* extension = g_object_new (MIDORI_TYPE_EXTENSION,
|
|
|
|
"name", _("Advertisement blocker"),
|
2009-09-14 15:59:43 +00:00
|
|
|
"description", _("Block advertisements according to a filter list"),
|
2012-01-08 00:28:42 +00:00
|
|
|
"version", "0.6" MIDORI_VERSION_SUFFIX,
|
2009-05-19 19:01:09 +00:00
|
|
|
"authors", "Christian Dywan <christian@twotoasts.de>",
|
|
|
|
NULL);
|
|
|
|
midori_extension_install_string_list (extension, "filters", NULL, G_MAXSIZE);
|
|
|
|
|
|
|
|
g_signal_connect (extension, "activate",
|
|
|
|
G_CALLBACK (adblock_activate_cb), NULL);
|
|
|
|
|
|
|
|
return extension;
|
|
|
|
}
|