Move adblock element hiding logic from JS to C

This commit is contained in:
Alexander Butenko 2011-12-18 02:07:55 +01:00 committed by Christian Dywan
parent ee055334ce
commit a87b4cd7d8

View file

@ -39,9 +39,8 @@ static GHashTable* pattern;
static GHashTable* keys; static GHashTable* keys;
static GHashTable* optslist; static GHashTable* optslist;
static GHashTable* urlcache; static GHashTable* urlcache;
static GHashTable* blockcssprivate;
static GString* blockcss; static GString* blockcss;
static GString* blockcssprivate;
static gchar* blockscript = NULL;
#ifdef G_ENABLE_DEBUG #ifdef G_ENABLE_DEBUG
static guint debug; static guint debug;
#endif #endif
@ -54,37 +53,77 @@ adblock_reload_rules (MidoriExtension* extension,
gboolean custom_only); gboolean custom_only);
static gchar* 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'," "window.addEventListener ('DOMContentLoaded',"
"function () {" "function () {"
" if (document.getElementById('madblock'))" " if (document.getElementById('madblock'))"
" return;" " return;"
// Get just domain name from URL " public = '");
" var URL = location.href.match(/:\\/\\/(.[^/]+)/)[1];"
" var sites = new Array(); %s;" suri = soup_uri_new (uri);
" var public = '.madblockplaceholder ';" if (!suri)
// Split domain into subdomain parts return NULL;
" var subdomains = URL.split ('.');"
" var hostname = subdomains [subdomains.length - 1];" domain = soup_uri_get_host (suri);
" var i = subdomains.length - 2;" subdomains = g_strsplit (domain, ".", -1);
// Check if any of subdomains do have blocking rules if (!subdomains)
" while (i >= 0) {" {
" hostname = subdomains [i] + '.' + hostname;" soup_uri_free (suri);
" if (sites [hostname])" g_string_free (code, TRUE);
" public += ', ' + sites [hostname];" return NULL;
" i--;" }
" }"
" public += ' {display: none !important}';" 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');" " var mystyle = document.createElement('style');"
" mystyle.setAttribute('type', 'text/css');" " mystyle.setAttribute('type', 'text/css');"
" mystyle.setAttribute('id', 'madblock');" " mystyle.setAttribute('id', 'madblock');"
" mystyle.appendChild(document.createTextNode(public));" " mystyle.appendChild(document.createTextNode(public));"
" var head = document.getElementsByTagName('head')[0];" " var head = document.getElementsByTagName('head')[0];"
" if (head) head.appendChild(mystyle);" " if (head) head.appendChild(mystyle);"
"}, true);", "}, true);");
private); return g_string_free (code, FALSE);
} }
static GString* static GString*
@ -106,12 +145,13 @@ adblock_init_db ()
urlcache = g_hash_table_new_full (g_str_hash, g_str_equal, urlcache = g_hash_table_new_full (g_str_hash, g_str_equal,
(GDestroyNotify)g_free, (GDestroyNotify)g_free,
(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) if (blockcss && blockcss->len > 0)
g_string_free (blockcss, TRUE); g_string_free (blockcss, TRUE);
if (blockcssprivate && blockcssprivate->len > 0)
g_string_free (blockcssprivate, TRUE);
blockcss = g_string_new ("z-non-exist"); blockcss = g_string_new ("z-non-exist");
blockcssprivate = g_string_new ("");
} }
static void static void
@ -134,7 +174,6 @@ adblock_download_notify_status_cb (WebKitDownload* download,
settings = katze_object_get_object (app, "settings"); settings = katze_object_get_object (app, "settings");
g_string_append (blockcss, " {display: none !important}\n"); g_string_append (blockcss, " {display: none !important}\n");
midori_web_settings_add_style (settings, "adblock-blockcss", blockcss->str); midori_web_settings_add_style (settings, "adblock-blockcss", blockcss->str);
katze_assign (blockscript, adblock_build_js (blockcssprivate->str));
g_object_unref (settings); g_object_unref (settings);
} }
@ -174,7 +213,9 @@ adblock_reload_rules (MidoriExtension* extension,
MidoriApp* app = midori_extension_get_app (extension); MidoriApp* app = midori_extension_get_app (extension);
MidoriWebSettings* settings = katze_object_get_object (app, "settings"); MidoriWebSettings* settings = katze_object_get_object (app, "settings");
/* g_test_timer_start (); */
adblock_init_db (); 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 = g_build_filename (midori_extension_get_config_dir (extension),
CUSTOM_LIST_NAME, NULL); CUSTOM_LIST_NAME, NULL);
@ -215,7 +256,6 @@ adblock_reload_rules (MidoriExtension* extension,
g_strfreev (filters); g_strfreev (filters);
g_string_append (blockcss, " {display: none !important}\n"); 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); midori_web_settings_add_style (settings, "adblock-blockcss", blockcss->str);
g_object_unref (settings); g_object_unref (settings);
} }
@ -622,7 +662,9 @@ adblock_check_rule (GRegex* regex,
return FALSE; return FALSE;
} }
/* TODO: Domain opt check */ /* TODO: Domain opt check */
#ifdef G_ENABLE_DEBUG
adblock_debug ("blocked by pattern regexp=%s -- %s", g_regex_get_pattern (regex), req_uri); adblock_debug ("blocked by pattern regexp=%s -- %s", g_regex_get_pattern (regex), req_uri);
#endif
return TRUE; return TRUE;
} }
@ -932,13 +974,19 @@ adblock_window_object_cleared_cb (WebKitWebView* web_view,
JSObjectRef js_window) JSObjectRef js_window)
{ {
const char *page_uri; const char *page_uri;
gchar* script;
page_uri = webkit_web_frame_get_uri (web_frame); page_uri = webkit_web_frame_get_uri (web_frame);
/* Don't add adblock css into speeddial and about: pages */ /* Don't add adblock css into speeddial and about: pages */
if (!midori_uri_is_http (page_uri)) if (!midori_uri_is_http (page_uri))
return; 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 static void
@ -1089,7 +1137,9 @@ adblock_compile_regexp (GString* gpatt,
if (!g_regex_match_simple ("[\\*]", sig, G_REGEX_UNGREEDY, G_REGEX_MATCH_NOTEMPTY) && if (!g_regex_match_simple ("[\\*]", sig, G_REGEX_UNGREEDY, G_REGEX_MATCH_NOTEMPTY) &&
!g_hash_table_lookup (keys, sig)) !g_hash_table_lookup (keys, sig))
{ {
#ifdef G_ENABLE_DEBUG
adblock_debug ("sig: %s %s", sig, patt); adblock_debug ("sig: %s %s", sig, patt);
#endif
g_hash_table_insert (keys, sig, regex); g_hash_table_insert (keys, sig, regex);
g_hash_table_insert (optslist, sig, g_strdup (opts)); g_hash_table_insert (optslist, sig, g_strdup (opts));
signature_count++; signature_count++;
@ -1099,7 +1149,9 @@ adblock_compile_regexp (GString* gpatt,
if (g_regex_match_simple ("^\\*", sig, G_REGEX_UNGREEDY, G_REGEX_MATCH_NOTEMPTY) && if (g_regex_match_simple ("^\\*", sig, G_REGEX_UNGREEDY, G_REGEX_MATCH_NOTEMPTY) &&
!g_hash_table_lookup (pattern, patt)) !g_hash_table_lookup (pattern, patt))
{ {
#ifdef G_ENABLE_DEBUG
adblock_debug ("patt2: %s %s", sig, patt); adblock_debug ("patt2: %s %s", sig, patt);
#endif
g_hash_table_insert (pattern, patt, regex); g_hash_table_insert (pattern, patt, regex);
g_hash_table_insert (optslist, patt, g_strdup (opts)); g_hash_table_insert (optslist, patt, g_strdup (opts));
} }
@ -1115,7 +1167,9 @@ adblock_compile_regexp (GString* gpatt,
} }
else else
{ {
#ifdef G_ENABLE_DEBUG
adblock_debug ("patt: %s%s", patt, ""); adblock_debug ("patt: %s%s", patt, "");
#endif
/* Pattern is a regexp chars */ /* Pattern is a regexp chars */
g_hash_table_insert (pattern, patt, regex); g_hash_table_insert (pattern, patt, regex);
g_hash_table_insert (optslist, patt, g_strdup (opts)); g_hash_table_insert (optslist, patt, g_strdup (opts));
@ -1170,7 +1224,9 @@ adblock_add_url_pattern (gchar* prefix,
format_patt = adblock_fixup_regexp (prefix, patt); format_patt = adblock_fixup_regexp (prefix, patt);
#ifdef G_ENABLE_DEBUG
adblock_debug ("got: %s opts %s", format_patt->str, opts); adblock_debug ("got: %s opts %s", format_patt->str, opts);
#endif
should_free = adblock_compile_regexp (format_patt, opts); should_free = adblock_compile_regexp (format_patt, opts);
if (data[1] && data[2]) if (data[1] && data[2])
@ -1200,6 +1256,22 @@ adblock_frame_add (gchar* line)
g_string_append (blockcss, line); 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 static inline void
adblock_frame_add_private (const gchar* line, adblock_frame_add_private (const gchar* line,
const gchar* sep) const gchar* sep)
@ -1234,15 +1306,13 @@ adblock_frame_add_private (const gchar* line,
/* strip ~ from domain */ /* strip ~ from domain */
if (domain[0] == '~') if (domain[0] == '~')
domain++; domain++;
g_string_append_printf (blockcssprivate, ";sites['%s']+=',%s'", adblock_update_css_hash (g_strstrip (domain), data[1]);
g_strstrip (domain), data[1]);
} }
g_strfreev (domains); g_strfreev (domains);
} }
else else
{ {
g_string_append_printf (blockcssprivate, ";sites['%s']+=',%s'", adblock_update_css_hash (data[0], data[1]);
data[0], data[1]);
} }
g_strfreev (data); g_strfreev (data);
} }
@ -1311,13 +1381,16 @@ adblock_parse_file (gchar* path)
FILE* file; FILE* file;
gchar line[2000]; gchar line[2000];
/* G_ENABLE_DEBUG g_test_timer_start (); */
if ((file = g_fopen (path, "r"))) if ((file = g_fopen (path, "r")))
{ {
while (fgets (line, 2000, file)) while (fgets (line, 2000, file))
adblock_parse_line (line); adblock_parse_line (line);
fclose (file); fclose (file);
/* g_debug ("match: %f%s %s", g_test_timer_elapsed (), "seconds", path); */
return TRUE; return TRUE;
} }
/* g_debug ("match: %f%s %s", g_test_timer_elapsed (), "seconds", path); */
return FALSE; return FALSE;
} }
@ -1360,14 +1433,13 @@ adblock_deactivate_cb (MidoriExtension* extension,
if (blockcss) if (blockcss)
g_string_free (blockcss, TRUE); g_string_free (blockcss, TRUE);
if (blockcssprivate)
g_string_free (blockcssprivate, TRUE);
midori_web_settings_remove_style (settings, "adblock-blockcss"); midori_web_settings_remove_style (settings, "adblock-blockcss");
blockcssprivate = blockcss = NULL; blockcss = NULL;
g_hash_table_destroy (pattern); g_hash_table_destroy (pattern);
g_hash_table_destroy (optslist); g_hash_table_destroy (optslist);
g_hash_table_destroy (urlcache); g_hash_table_destroy (urlcache);
g_hash_table_destroy (blockcssprivate);
g_object_unref (settings); g_object_unref (settings);
} }