Refactor JS code generation for adblock
This commit is contained in:
parent
729e960cd6
commit
835f808115
1 changed files with 17 additions and 32 deletions
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
Copyright (C) 2009-2010 Christian Dywan <christian@twotoasts.de>
|
Copyright (C) 2009-2012 Christian Dywan <christian@twotoasts.de>
|
||||||
Copyright (C) 2009 Alexander Butenko <a.butenka@gmail.com>
|
Copyright (C) 2009-2012 Alexander Butenko <a.butenka@gmail.com>
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
This library is free software; you can redistribute it and/or
|
||||||
modify it under the terms of the GNU Lesser General Public
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
@ -9,7 +9,6 @@
|
||||||
|
|
||||||
See the file COPYING for the full license text.
|
See the file COPYING for the full license text.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <midori/midori.h>
|
#include <midori/midori.h>
|
||||||
#include <glib/gstdio.h>
|
#include <glib/gstdio.h>
|
||||||
|
|
||||||
|
@ -55,13 +54,18 @@ adblock_reload_rules (MidoriExtension* extension,
|
||||||
static gchar*
|
static gchar*
|
||||||
adblock_build_js (const gchar* uri)
|
adblock_build_js (const gchar* uri)
|
||||||
{
|
{
|
||||||
const gchar* domain;
|
gchar* domain;
|
||||||
const gchar* block;
|
const gchar* style;
|
||||||
GString* subdomain;
|
GString* subdomain;
|
||||||
GString* code;
|
GString* code;
|
||||||
int cnt = 0, blockscnt = 0;
|
int cnt = 0, blockscnt = 0;
|
||||||
gchar** subdomains;
|
gchar** subdomains;
|
||||||
SoupURI* suri;
|
|
||||||
|
domain = midori_uri_parse_hostname (uri, NULL);
|
||||||
|
subdomains = g_strsplit (domain, ".", -1);
|
||||||
|
g_free (domain);
|
||||||
|
if (!subdomains)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
code = g_string_new (
|
code = g_string_new (
|
||||||
"window.addEventListener ('DOMContentLoaded',"
|
"window.addEventListener ('DOMContentLoaded',"
|
||||||
|
@ -70,32 +74,17 @@ adblock_build_js (const gchar* uri)
|
||||||
" return;"
|
" return;"
|
||||||
" public = '");
|
" public = '");
|
||||||
|
|
||||||
suri = soup_uri_new (uri);
|
cnt = g_strv_length (subdomains) - 1;
|
||||||
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]);
|
subdomain = g_string_new (subdomains [cnt]);
|
||||||
g_string_prepend_c (subdomain, '.');
|
g_string_prepend_c (subdomain, '.');
|
||||||
cnt--;
|
cnt--;
|
||||||
while (cnt >= 0)
|
while (cnt >= 0)
|
||||||
{
|
{
|
||||||
g_string_prepend (subdomain, subdomains[cnt]);
|
g_string_prepend (subdomain, subdomains[cnt]);
|
||||||
if ((block = g_hash_table_lookup (blockcssprivate, subdomain->str)))
|
/* g_debug ("processing %s", subdomain->str); */
|
||||||
|
if ((style = g_hash_table_lookup (blockcssprivate, subdomain->str)))
|
||||||
{
|
{
|
||||||
g_string_append (code, block);
|
g_string_append (code, style);
|
||||||
g_string_append_c (code, ',');
|
g_string_append_c (code, ',');
|
||||||
blockscnt++;
|
blockscnt++;
|
||||||
}
|
}
|
||||||
|
@ -104,13 +93,9 @@ adblock_build_js (const gchar* uri)
|
||||||
}
|
}
|
||||||
g_string_free (subdomain, TRUE);
|
g_string_free (subdomain, TRUE);
|
||||||
g_strfreev (subdomains);
|
g_strfreev (subdomains);
|
||||||
soup_uri_free (suri);
|
|
||||||
|
|
||||||
if (blockscnt == 0)
|
if (blockscnt == 0)
|
||||||
{
|
return g_string_free (code, TRUE);
|
||||||
g_string_free (code, TRUE);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
g_string_append (code,
|
g_string_append (code,
|
||||||
" zz-non-existent {display: none !important}';"
|
" zz-non-existent {display: none !important}';"
|
||||||
|
@ -1562,7 +1547,7 @@ extension_init (void)
|
||||||
MidoriExtension* extension = g_object_new (MIDORI_TYPE_EXTENSION,
|
MidoriExtension* extension = g_object_new (MIDORI_TYPE_EXTENSION,
|
||||||
"name", _("Advertisement blocker"),
|
"name", _("Advertisement blocker"),
|
||||||
"description", _("Block advertisements according to a filter list"),
|
"description", _("Block advertisements according to a filter list"),
|
||||||
"version", "0.5" MIDORI_VERSION_SUFFIX,
|
"version", "0.6" MIDORI_VERSION_SUFFIX,
|
||||||
"authors", "Christian Dywan <christian@twotoasts.de>",
|
"authors", "Christian Dywan <christian@twotoasts.de>",
|
||||||
NULL);
|
NULL);
|
||||||
midori_extension_install_string_list (extension, "filters", NULL, G_MAXSIZE);
|
midori_extension_install_string_list (extension, "filters", NULL, G_MAXSIZE);
|
||||||
|
|
Loading…
Reference in a new issue