diff options
author | portix <portix@gmx.net> | 2011-11-29 00:08:19 +0100 |
---|---|---|
committer | portix <portix@gmx.net> | 2011-11-29 00:08:19 +0100 |
commit | 5edce0b55d1564c8ed4e8ac891c9e5e2296fca2c (patch) | |
tree | 3f7893b35d4478430eef9fbc6b38e01118a8a361 | |
parent | 7f33ebca008f5f3ce58a7bdd88d3600deaa56ccf (diff) | |
download | dwb-5edce0b55d1564c8ed4e8ac891c9e5e2296fca2c.zip |
Reintroducing simple filter rules
--HG--
branch : experimental
-rw-r--r-- | doc/dwb.1 | 10 | ||||
-rw-r--r-- | src/adblock.c | 100 | ||||
-rw-r--r-- | src/adblock.h | 3 | ||||
-rw-r--r-- | src/commands.c | 17 | ||||
-rw-r--r-- | src/commands.h | 3 | ||||
-rw-r--r-- | src/config.h | 5 | ||||
-rw-r--r-- | src/domain.c | 3 | ||||
-rw-r--r-- | src/dwb.c | 5 | ||||
-rw-r--r-- | src/dwb.h | 4 |
9 files changed, 130 insertions, 20 deletions
@@ -550,12 +550,14 @@ If a string value is set to the default value will be used. The settings in detail are: .TP .BR adblocker -Block advertisements using a filterlist. The filter can be specified in -.IR $XDG_CONFIG_HOME/dwb/adblock . -Regular expressions can be used by putting a '@' at the beginning of the line. -default value: +Block advertisements using a filterlist, see also +.IR adblocker-filterlist .IR false . .TP +.BR adblocker-filterlist +A path to a adblock plus compatible filterlist for the adblocker. +.IR NULL . +.TP .BR auto-load-images Load images automatically. Possible values: true/false, default value: diff --git a/src/adblock.c b/src/adblock.c index 69f9d19a..03d967d0 100644 --- a/src/adblock.c +++ b/src/adblock.c @@ -97,6 +97,8 @@ typedef struct _AdblockElementHider { /* Static variables {{{*/ +static GPtrArray *_simple_rules; +static GPtrArray *_simple_exceptions; static GPtrArray *_rules; static GPtrArray *_exceptions; static GHashTable *_hider_rules; @@ -167,9 +169,18 @@ adblock_do_match(AdblockRule *rule, const char *uri) { return false; }/*}}}*/ -/* adblock_match(GPtrArray *, SoupURI *, const char *base_domain, * AdblockAttribute, gboolean thirdparty) {{{*/ +/* adblock_match(GPtrArray *, SoupURI *, const char *base_domain, * AdblockAttribute, gboolean thirdparty) + * Params: + * array - the filtes array + * uri - the uri to check + * uri_host - the hostname of the request + * uri_base - the domainname of the request + * host - the hostname of the page + * domain - the domainname of the page + * thirdparty - the domainname of the page + * {{{*/ gboolean -adblock_match(GPtrArray *array, const char *uri, const char *uri_host, const char *uri_base, const char *host, const char *base_domain, AdblockAttribute attributes, gboolean thirdparty) { +adblock_match(GPtrArray *array, const char *uri, const char *uri_host, const char *uri_base, const char *host, const char *domain, AdblockAttribute attributes, gboolean thirdparty) { gboolean match = false; const char *base_start = strstr(uri, uri_base); const char *uri_start = strstr(uri, uri_host); @@ -199,7 +210,7 @@ adblock_match(GPtrArray *array, const char *uri, const char *uri_host, const cha if (rule->attributes & ADBLOCK_CLEAR_UPPER && ! (rule->attributes & attributes) ) continue; } - if (rule->domains && !domain_match(rule->domains, host, base_domain)) { + if (rule->domains && !domain_match(rule->domains, host, domain)) { continue; } if ( (rule->options & AO_THIRDPARTY && !thirdparty) @@ -291,6 +302,47 @@ adblock_frame_created_cb(WebKitWebView *wv, WebKitWebFrame *frame, GList *gl) { g_signal_connect(frame, "load-committed", G_CALLBACK(adblock_frame_load_committed_cb), gl); }/*}}}*/ +static void +adblock_resource_request_cb(WebKitWebView *wv, WebKitWebFrame *frame, + WebKitWebResource *resource, WebKitNetworkRequest *request, + WebKitNetworkResponse *response, GList *gl) { + if (request == NULL) + return; + AdblockAttribute attribute = webkit_web_view_get_main_frame(wv) == frame ? AA_DOCUMENT : AA_SUBDOCUMENT; + + const char *uri = webkit_network_request_get_uri(request); + if (uri == NULL) + return; + SoupMessage *msg = webkit_network_request_get_message(request); + if (msg == NULL) + return; + SoupURI *suri = soup_message_get_uri(msg); + const char *host = soup_uri_get_host(suri); + if (host == NULL) + return; + const char *domain = domain_get_base_for_host(host); + if (domain == NULL) + return; + + SoupURI *sfirst_party = soup_message_get_first_party(msg); + if (sfirst_party == NULL) + return; + const char *firsthost = soup_uri_get_host(sfirst_party); + if (firsthost == NULL) + return; + const char *firstdomain = domain_get_base_for_host(firsthost); + if (firstdomain == NULL) + return; + gboolean thirdparty = strcmp(domain, firstdomain); + + if (!adblock_match(_simple_exceptions, uri, host, domain, firsthost, firstdomain, attribute, thirdparty)) { + if (adblock_match(_simple_rules, uri, host, domain, firsthost, firstdomain, attribute, thirdparty)) { + webkit_network_request_set_uri(request, "about:blank"); + } + } + + +} /* adblock_load_status_cb(WebKitWebView *, GParamSpec *, GList *) {{{*/ static void adblock_load_status_cb(WebKitWebView *wv, GParamSpec *p, GList *gl) { @@ -310,10 +362,16 @@ adblock_load_status_cb(WebKitWebView *wv, GParamSpec *p, GList *gl) { AdblockElementHider *hider; WebKitWebDataSource *datasource = webkit_web_frame_get_data_source(frame); WebKitNetworkRequest *request = webkit_web_data_source_get_request(datasource); + SoupMessage *msg = webkit_network_request_get_message(request); + g_return_if_fail(msg != NULL); + SoupURI *suri = soup_message_get_uri(msg); + g_return_if_fail(suri != NULL); + const char *host = soup_uri_get_host(suri); const char *base_domain = domain_get_base_for_host(host); + g_return_if_fail(host != NULL); g_return_if_fail(base_domain != NULL); GString *css_rule = NULL; @@ -389,6 +447,11 @@ adblock_load_status_cb(WebKitWebView *wv, GParamSpec *p, GList *gl) { /* START AND END {{{*/ + +gboolean +adblock_running() { + return _init && GET_BOOL("adblocker"); +} /* adblock_disconnect(GList *) {{{*/ void adblock_disconnect(GList *gl) { @@ -401,10 +464,13 @@ adblock_disconnect(GList *gl) { g_signal_handler_disconnect(WEBVIEW(gl), (VIEW(gl)->status->signals[SIG_AD_FRAME_CREATED])); v->status->signals[SIG_AD_FRAME_CREATED] = 0; } + if (v->status->signals[SIG_AD_RESOURCE_REQUEST] > 0) { + g_signal_handler_disconnect(WEBVIEW(gl), (VIEW(gl)->status->signals[SIG_AD_RESOURCE_REQUEST])); + v->status->signals[SIG_AD_RESOURCE_REQUEST] = 0; + } }/*}}}*/ - /* adblock_connect() {{{*/ void adblock_connect(GList *gl) { @@ -414,6 +480,9 @@ adblock_connect(GList *gl) { VIEW(gl)->status->signals[SIG_AD_LOAD_STATUS] = g_signal_connect(WEBVIEW(gl), "notify::load-status", G_CALLBACK(adblock_load_status_cb), gl); VIEW(gl)->status->signals[SIG_AD_FRAME_CREATED] = g_signal_connect(WEBVIEW(gl), "frame-created", G_CALLBACK(adblock_frame_created_cb), gl); } + if (_simple_rules->len > 0) { + VIEW(gl)->status->signals[SIG_AD_RESOURCE_REQUEST] = g_signal_connect(WEBVIEW(gl), "resource-request-starting", G_CALLBACK(adblock_resource_request_cb), gl); + } }/*}}}*/ /* adblock_warn_ignored(const char *message, const char *rule){{{*/ @@ -438,7 +507,6 @@ adblock_rule_parse(char *pattern) { if (pattern[0] == '!' || pattern[0] == '[') { return STATUS_IGNORE; } - //AdblockRule *rule = adblock_rule_new(); char *tmp = NULL; char *tmp_a = NULL, *tmp_b = NULL, *tmp_c = NULL; /* Element hiding rules */ @@ -662,10 +730,18 @@ adblock_rule_parse(char *pattern) { adrule->pattern = rule; adrule->options = option; adrule->domains = domains; - if (exception) - g_ptr_array_add(_exceptions, adrule); - else - g_ptr_array_add(_rules, adrule); + if (!(attributes & ~(AA_DOCUMENT | AA_FRAME))) { + if (exception) + g_ptr_array_add(_simple_exceptions, adrule); + else + g_ptr_array_add(_simple_rules, adrule); + } + else { + if (exception) + g_ptr_array_add(_exceptions, adrule); + else + g_ptr_array_add(_rules, adrule); + } } error_out: FREE(tmp_a); @@ -682,6 +758,10 @@ adblock_end() { g_string_free(_css_exceptions, true); if (_rules != NULL) g_ptr_array_free(_rules, true); + if (_simple_rules != NULL) + g_ptr_array_free(_simple_rules, true); + if (_simple_exceptions != NULL) + g_ptr_array_free(_simple_exceptions, true); if(_exceptions != NULL) g_ptr_array_free(_exceptions, true); if (_hider_rules != NULL) @@ -709,6 +789,8 @@ adblock_init() { } _rules = g_ptr_array_new_with_free_func((GDestroyNotify)adblock_rule_free); _exceptions = g_ptr_array_new_with_free_func((GDestroyNotify)adblock_rule_free); + _simple_rules = g_ptr_array_new_with_free_func((GDestroyNotify)adblock_rule_free); + _simple_exceptions = g_ptr_array_new_with_free_func((GDestroyNotify)adblock_rule_free); _hider_rules = g_hash_table_new_full((GHashFunc)g_str_hash, (GEqualFunc)g_str_equal, (GDestroyNotify)g_free, NULL); _css_exceptions = g_string_new(NULL); _css_rules = g_string_new(NULL); diff --git a/src/adblock.h b/src/adblock.h index 0015372d..2b138836 100644 --- a/src/adblock.h +++ b/src/adblock.h @@ -23,9 +23,8 @@ #include "dwb.h" gboolean adblock_init(); +gboolean adblock_running(); void adblock_end(); -void adblock_resource_request_cb(WebKitWebView *, WebKitWebFrame *, WebKitWebResource *, - WebKitNetworkRequest *, WebKitNetworkResponse *, GList *gl); void adblock_connect(GList *gl); void adblock_disconnect(GList *gl); void adblock_set_user_stylesheet(const char *file); diff --git a/src/commands.c b/src/commands.c index 336ac532..e170f1f4 100644 --- a/src/commands.c +++ b/src/commands.c @@ -18,6 +18,9 @@ #include "commands.h" #include "local.h" +#ifdef DWB_ADBLOCKER +#include "adblock.h" +#endif static int inline dwb_floor(double x) { return x >= 0 ? (int) x : (int) x - 1; @@ -695,6 +698,20 @@ commands_toggle_scripts(KeyMap *km, Arg *arg) { return STATUS_OK; }/*}}}*/ +#ifdef DWB_ADBLOCKER +/* commands_toggle_adblocker {{{ */ +DwbStatus +commands_toggle_adblocker(KeyMap *km, Arg *arg) { + gboolean running = adblock_running(); + WebSettings *s = g_hash_table_lookup(dwb.settings, "adblocker"); + s->arg.b = !running; + dwb_set_adblock(NULL, s); + + dwb_set_normal_message(dwb.state.fview, true, "Adblocker %s", running ? "disabled" : "enabled"); + return STATUS_OK; +}/*}}}*/ +#endif + /* commands_new_window_or_view{{{*/ DwbStatus commands_new_window_or_view(KeyMap *km, Arg *arg) { diff --git a/src/commands.h b/src/commands.h index 3400a3db..b8e3a4a9 100644 --- a/src/commands.h +++ b/src/commands.h @@ -93,5 +93,8 @@ DwbStatus commands_open_editor(KeyMap *, Arg *); DwbStatus commands_insert_mode(KeyMap *, Arg *); DwbStatus commands_command_mode(KeyMap *, Arg *); DwbStatus commands_only(KeyMap *, Arg *); +#ifdef DWB_ADBLOCKER +DwbStatus commands_toggle_adblocker(KeyMap *, Arg *); +#endif #endif diff --git a/src/config.h b/src/config.h index 8d10c6d6..197bcdcc 100644 --- a/src/config.h +++ b/src/config.h @@ -139,6 +139,7 @@ static KeyValue KEYS[] = { { "toggle_plugins_host", { "ph", 0 }, }, { "toggle_plugins_uri_tmp", { "ptu", 0 }, }, { "toggle_plugins_host_tmp", { "pth", 0 }, }, + { "toggle_adblocker", { "a", GDK_CONTROL_MASK }, }, { "new_view", { "V", 0 }, }, { "new_window", { "W", 0 }, }, { "save", { "sf", 0 }, }, @@ -432,6 +433,10 @@ static FunctionMap FMAP [] = { (Func) commands_toggle_plugin_blocker, NULL, POST_SM, { .n = ALLOW_HOST | ALLOW_TMP } }, { { "toggle_plugins_uri_tmp", "Toggle block content for current url for this session" }, 1, (Func) commands_toggle_plugin_blocker, NULL, POST_SM, { .n = ALLOW_URI | ALLOW_TMP } }, +#ifdef DWB_ADBLOCKER + { { "toggle_adblocker", "Toggle adblocker" }, 1, + (Func) commands_toggle_adblocker, NULL, POST_SM, { 0 } }, +#endif { { "toggle_hidden_files", "Toggle hidden files in directory listing" }, 1, (Func) commands_toggle_hidden_files, NULL, ALWAYS_SM, { 0 } }, { { "print", "Print current page" }, 1, diff --git a/src/domain.c b/src/domain.c index 125ee6fd..072501fb 100644 --- a/src/domain.c +++ b/src/domain.c @@ -76,7 +76,8 @@ domain_match(char **domains, const char *host, const char *base_domain) { const char * domain_get_base_for_host(const char *host) { - g_return_val_if_fail(host != NULL, NULL); + if (host == NULL) + return NULL; g_return_val_if_fail(_tld_table != NULL, NULL); const char *cur_domain = host; @@ -45,9 +45,6 @@ static void dwb_set_startpage(GList *, WebSettings *); static void dwb_set_message_delay(GList *, WebSettings *); static void dwb_set_history_length(GList *, WebSettings *); static void dwb_set_plugin_blocker(GList *, WebSettings *); -#ifdef DWB_ADBLOCKER -static void dwb_set_adblock(GList *, WebSettings *); -#endif static void dwb_set_hide_tabbar(GList *, WebSettings *); static void dwb_set_sync_interval(GList *, WebSettings *); static void dwb_set_private_browsing(GList *, WebSettings *); @@ -123,7 +120,7 @@ dwb_set_plugin_blocker(GList *gl, WebSettings *s) { #ifdef DWB_ADBLOCKER /* dwb_set_adblock {{{*/ -static void +void dwb_set_adblock(GList *gl, WebSettings *s) { if (s->arg.b) { for (GList *l = dwb.state.views; l; l=l->next) @@ -374,6 +374,7 @@ enum Signal { #ifdef DWB_ADBLOCKER SIG_AD_LOAD_STATUS, SIG_AD_FRAME_CREATED, + SIG_AD_RESOURCE_REQUEST, #endif SIG_PLUGINS_LOAD, @@ -796,5 +797,8 @@ void dwb_set_open_mode(Open); DwbStatus dwb_set_clipboard(const char *text, GdkAtom atom); DwbStatus dwb_open_in_editor(void); +#ifdef DWB_ADBLOCKER +void dwb_set_adblock(GList *, WebSettings *); +#endif #endif |