static GHashTable* keys;
static GHashTable* optslist;
static GHashTable* urlcache;
+static GHashTable* blockcssprivate;
static GString* blockcss;
-static GString* blockcssprivate;
-static gchar* blockscript = NULL;
#ifdef G_ENABLE_DEBUG
static guint debug;
#endif
gboolean custom_only);
static gchar*
-adblock_build_js (const gchar* private)
+adblock_build_js (const gchar* uri)
{
- return g_strdup_printf (
+ const gchar* domain;
+ const gchar* block;
+ GString* subdomain;
+ GString* code;
+ int cnt = 0, blockscnt = 0;
+ gchar** subdomains;
+ SoupURI* suri;
+
+ code = g_string_new (
"window.addEventListener ('DOMContentLoaded',"
"function () {"
" if (document.getElementById('madblock'))"
" return;"
- // Get just domain name from URL
- " var URL = location.href.match(/:\\/\\/(.[^/]+)/)[1];"
- " var sites = new Array(); %s;"
- " var public = '.madblockplaceholder ';"
- // Split domain into subdomain parts
- " var subdomains = URL.split ('.');"
- " var hostname = subdomains [subdomains.length - 1];"
- " var i = subdomains.length - 2;"
- // Check if any of subdomains do have blocking rules
- " while (i >= 0) {"
- " hostname = subdomains [i] + '.' + hostname;"
- " if (sites [hostname])"
- " public += ', ' + sites [hostname];"
- " i--;"
- " }"
- " public += ' {display: none !important}';"
+ " public = '");
+
+ suri = soup_uri_new (uri);
+ if (!suri)
+ return NULL;
+
+ domain = soup_uri_get_host (suri);
+ subdomains = g_strsplit (domain, ".", -1);
+ if (!subdomains)
+ {
+ soup_uri_free (suri);
+ g_string_free (code, TRUE);
+ return NULL;
+ }
+
+ while (subdomains[cnt] != NULL)
+ cnt++;
+ cnt--;
+
+ subdomain = g_string_new (subdomains [cnt]);
+ g_string_prepend_c (subdomain, '.');
+ cnt--;
+ while (cnt >= 0)
+ {
+ g_string_prepend (subdomain, subdomains[cnt]);
+ /* g_debug ("checking %s", subdomain->str); */
+ if ((block = g_hash_table_lookup (blockcssprivate, subdomain->str)))
+ {
+ /* g_debug ("found"); */
+ g_string_append (code, block);
+ g_string_append_c (code, ',');
+ blockscnt++;
+ }
+ g_string_prepend_c (subdomain, '.');
+ cnt--;
+ }
+ g_string_free (subdomain, TRUE);
+ g_strfreev (subdomains);
+ soup_uri_free (suri);
+
+ if (blockscnt == 0)
+ {
+ g_string_free (code, TRUE);
+ return NULL;
+ }
+
+ g_string_append (code,
+ " zz-non-existent {display: none !important}';"
" var mystyle = document.createElement('style');"
" mystyle.setAttribute('type', 'text/css');"
" mystyle.setAttribute('id', 'madblock');"
" mystyle.appendChild(document.createTextNode(public));"
" var head = document.getElementsByTagName('head')[0];"
" if (head) head.appendChild(mystyle);"
- "}, true);",
- private);
+ "}, true);");
+ return g_string_free (code, FALSE);
}
static GString*
urlcache = g_hash_table_new_full (g_str_hash, g_str_equal,
(GDestroyNotify)g_free,
(GDestroyNotify)g_free);
+ blockcssprivate = g_hash_table_new_full (g_str_hash, g_str_equal,
+ (GDestroyNotify)g_free,
+ (GDestroyNotify)g_free);
+
if (blockcss && blockcss->len > 0)
g_string_free (blockcss, TRUE);
- if (blockcssprivate && blockcssprivate->len > 0)
- g_string_free (blockcssprivate, TRUE);
blockcss = g_string_new ("z-non-exist");
- blockcssprivate = g_string_new ("");
}
static void
settings = katze_object_get_object (app, "settings");
g_string_append (blockcss, " {display: none !important}\n");
midori_web_settings_add_style (settings, "adblock-blockcss", blockcss->str);
- katze_assign (blockscript, adblock_build_js (blockcssprivate->str));
g_object_unref (settings);
}
MidoriApp* app = midori_extension_get_app (extension);
MidoriWebSettings* settings = katze_object_get_object (app, "settings");
+ /* g_test_timer_start (); */
adblock_init_db ();
+ /* g_debug ("match: %f%s", g_test_timer_elapsed (), "seconds"); */
custom_list = g_build_filename (midori_extension_get_config_dir (extension),
CUSTOM_LIST_NAME, NULL);
g_strfreev (filters);
g_string_append (blockcss, " {display: none !important}\n");
- katze_assign (blockscript, adblock_build_js (blockcssprivate->str));
midori_web_settings_add_style (settings, "adblock-blockcss", blockcss->str);
g_object_unref (settings);
}
return FALSE;
}
/* TODO: Domain opt check */
+ #ifdef G_ENABLE_DEBUG
adblock_debug ("blocked by pattern regexp=%s -- %s", g_regex_get_pattern (regex), req_uri);
+ #endif
return TRUE;
}
JSObjectRef js_window)
{
const char *page_uri;
+ gchar* script;
page_uri = webkit_web_frame_get_uri (web_frame);
/* Don't add adblock css into speeddial and about: pages */
if (!midori_uri_is_http (page_uri))
return;
- g_free (sokoke_js_script_eval (js_context, blockscript, NULL));
+ script = adblock_build_js (page_uri);
+ if (!script)
+ return;
+
+ g_free (sokoke_js_script_eval (js_context, script, NULL));
+ g_free (script);
}
static void
if (!g_regex_match_simple ("[\\*]", sig, G_REGEX_UNGREEDY, G_REGEX_MATCH_NOTEMPTY) &&
!g_hash_table_lookup (keys, sig))
{
+ #ifdef G_ENABLE_DEBUG
adblock_debug ("sig: %s %s", sig, patt);
+ #endif
g_hash_table_insert (keys, sig, regex);
g_hash_table_insert (optslist, sig, g_strdup (opts));
signature_count++;
if (g_regex_match_simple ("^\\*", sig, G_REGEX_UNGREEDY, G_REGEX_MATCH_NOTEMPTY) &&
!g_hash_table_lookup (pattern, patt))
{
+ #ifdef G_ENABLE_DEBUG
adblock_debug ("patt2: %s %s", sig, patt);
+ #endif
g_hash_table_insert (pattern, patt, regex);
g_hash_table_insert (optslist, patt, g_strdup (opts));
}
}
else
{
+ #ifdef G_ENABLE_DEBUG
adblock_debug ("patt: %s%s", patt, "");
+ #endif
/* Pattern is a regexp chars */
g_hash_table_insert (pattern, patt, regex);
g_hash_table_insert (optslist, patt, g_strdup (opts));
format_patt = adblock_fixup_regexp (prefix, patt);
+ #ifdef G_ENABLE_DEBUG
adblock_debug ("got: %s opts %s", format_patt->str, opts);
+ #endif
should_free = adblock_compile_regexp (format_patt, opts);
if (data[1] && data[2])
g_string_append (blockcss, line);
}
+static inline void
+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));
+}
+
static inline void
adblock_frame_add_private (const gchar* line,
const gchar* sep)
/* strip ~ from domain */
if (domain[0] == '~')
domain++;
- g_string_append_printf (blockcssprivate, ";sites['%s']+=',%s'",
- g_strstrip (domain), data[1]);
+ adblock_update_css_hash (g_strstrip (domain), data[1]);
}
g_strfreev (domains);
}
else
{
- g_string_append_printf (blockcssprivate, ";sites['%s']+=',%s'",
- data[0], data[1]);
+ adblock_update_css_hash (data[0], data[1]);
}
g_strfreev (data);
}
FILE* file;
gchar line[2000];
+ /* G_ENABLE_DEBUG g_test_timer_start (); */
if ((file = g_fopen (path, "r")))
{
while (fgets (line, 2000, file))
adblock_parse_line (line);
fclose (file);
+ /* g_debug ("match: %f%s %s", g_test_timer_elapsed (), "seconds", path); */
return TRUE;
}
+ /* g_debug ("match: %f%s %s", g_test_timer_elapsed (), "seconds", path); */
return FALSE;
}
if (blockcss)
g_string_free (blockcss, TRUE);
- if (blockcssprivate)
- g_string_free (blockcssprivate, TRUE);
midori_web_settings_remove_style (settings, "adblock-blockcss");
- blockcssprivate = blockcss = NULL;
+ blockcss = NULL;
g_hash_table_destroy (pattern);
g_hash_table_destroy (optslist);
g_hash_table_destroy (urlcache);
+ g_hash_table_destroy (blockcssprivate);
g_object_unref (settings);
}