summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorportix <none@none>2012-10-23 01:00:42 +0200
committerportix <none@none>2012-10-23 01:00:42 +0200
commit1bf336e63e1c46a3005d1e4d555d294dd45a9af0 (patch)
treeb55d987a4a31a07701a5f52c6b9fb5ad0dd553ae /src
parent371935c5857ff7440f2fd1e29d28c1bd2918e7cf (diff)
downloaddwb-1bf336e63e1c46a3005d1e4d555d294dd45a9af0.zip
Fixing element hider issues with webkit-1.10
Diffstat (limited to 'src')
-rw-r--r--src/adblock.c89
-rw-r--r--src/commands.c4
-rw-r--r--src/dwb.c8
-rw-r--r--src/js.c17
-rw-r--r--src/js.h2
-rw-r--r--src/util.c4
-rw-r--r--src/view.c4
7 files changed, 62 insertions, 66 deletions
diff --git a/src/adblock.c b/src/adblock.c
index bf3c133d..6b59c4b5 100644
--- a/src/adblock.c
+++ b/src/adblock.c
@@ -83,12 +83,12 @@ static GHashTable *m_hider_rules;
gboolean m_has_hider_rules;
/* only used to freeing elementhider */
static GSList *m_hider_list;
-static GString *m_css_rules;
static GString *m_css_exceptions;
static gboolean m_init = false;
+static GSList *m_css_hider_list;
+#define HIDER_LIST_MAX 500
/*}}}*//*}}}*/
-
/* NEW AND FREE {{{*/
/* adblock_rule_new {{{*/
static AdblockRule *
@@ -115,10 +115,10 @@ adblock_rule_free(AdblockRule *rule) {
/* adblock_element_hider_new {{{*/
static AdblockElementHider *
-adblock_element_hider_new() {
+adblock_element_hider_new(const char *selector, char **domains) {
AdblockElementHider *hider = dwb_malloc(sizeof(AdblockElementHider));
- hider->selector = NULL;
- hider->domains = NULL;
+ hider->selector = g_strdup(selector);
+ hider->domains = domains;
return hider;
}/*}}}*/
@@ -265,7 +265,6 @@ error_out:
void
adblock_apply_element_hider(WebKitWebFrame *frame, GList *gl) {
GSList *list;
- WebKitWebView *wv = WEBVIEW(gl);
WebKitWebDataSource *datasource = webkit_web_frame_get_data_source(frame);
WebKitNetworkRequest *request = webkit_web_data_source_get_request(datasource);
@@ -320,46 +319,16 @@ adblock_apply_element_hider(WebKitWebFrame *frame, GList *gl) {
if (! has_exception) {
g_string_append(css_rule, m_css_exceptions->str);
}
- if (m_css_rules != NULL && m_css_rules->len > 1) {
- g_string_append(css_rule, m_css_rules->str);
- }
- if (css_rule != NULL && css_rule->len > 0) {
+ if (css_rule->len > 0)
+ {
if (css_rule->str[css_rule->len-1] == ',')
g_string_erase(css_rule, css_rule->len-1, 1);
g_string_append(css_rule, "{display:none!important;}");
- if (frame == webkit_web_view_get_main_frame(wv)) {
- WebKitDOMDocument *doc = webkit_web_view_get_dom_document(wv);
- if (VIEW(gl)->status->style == NULL) {
- WebKitDOMDocument *doc = webkit_web_view_get_dom_document(WEBVIEW(gl));
- VIEW(gl)->status->style = webkit_dom_document_create_element(doc, "style", NULL);
- }
- WebKitDOMHTMLHeadElement *head = webkit_dom_document_get_head(doc);
- if (G_IS_OBJECT(head)) {
- webkit_dom_html_element_set_inner_html(WEBKIT_DOM_HTML_ELEMENT(VIEW(gl)->status->style), css_rule->str, NULL);
- webkit_dom_node_append_child(WEBKIT_DOM_NODE(head), WEBKIT_DOM_NODE(VIEW(gl)->status->style), NULL);
- g_object_unref(head);
- }
- }
- else {
- const char *css = css_rule->str;
- /* Escape css_rule, at least ' and \ must be escaped, maybe some more? */
- for (int pos = 0; *css; pos++, css++) {
- if (*css == '\'' || *css == '\\') {
- g_string_insert_c(css_rule, pos, '\\');
- pos++;
- css++;
- }
- }
- char *script = g_strdup_printf(
- "(function() {var st=document.createElement('style');\
- document.head.appendChild(st);\
- document.styleSheets[document.styleSheets.length-1].insertRule('%s', 0);})();",
- css_rule->str);
- dwb_execute_script(frame, script, false);
- g_free(script);
- }
- g_string_free(css_rule, true);
+ js_call_as_function(frame, VIEW(gl)->hint_object, "insertAdblockRule", css_rule->str, kJSTypeString, NULL);
}
+ for (GSList *l = m_css_hider_list; l; l=l->next)
+ js_call_as_function(frame, VIEW(gl)->hint_object, "insertAdblockRule", l->data, kJSTypeString, NULL);
+ g_string_free(css_rule, true);
}/*}}}*/
/*}}}*/
@@ -526,7 +495,7 @@ void
adblock_connect(GList *gl) {
if (!m_init && !adblock_init())
return;
- if (m_rules->len > 0 || m_css_rules->len > 0 || m_has_hider_rules) {
+ if (m_rules->len > 0 || m_css_hider_list != NULL || m_has_hider_rules) {
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);
}
@@ -572,6 +541,8 @@ adblock_rule_parse(char *filterlist) {
GRegex *rule;
char **options_arr;
char warning[256];
+ int n_css_rules = 0;
+ GString *css_rule = g_string_new(NULL);
for (int i=0; lines[i] != NULL; i++) {
pattern = lines[i];
@@ -588,11 +559,9 @@ adblock_rule_parse(char *filterlist) {
/* Match domains */
if (*pattern != '#') {
domains = g_strndup(pattern, tmp-pattern);
- AdblockElementHider *hider = adblock_element_hider_new();
domain_arr = g_strsplit(domains, ",", -1);
- hider->domains = domain_arr;
- hider->selector = g_strdup(tmp+2);
+ AdblockElementHider *hider = adblock_element_hider_new(tmp+2, domain_arr);
GSList *list;
gboolean hider_exc = true;
for (; *domain_arr; domain_arr++) {
@@ -622,8 +591,19 @@ adblock_rule_parse(char *filterlist) {
}
/* general rules */
else {
- g_string_append(m_css_rules, tmp + 2);
- g_string_append_c(m_css_rules, ',');
+ g_string_append(css_rule, tmp+2);
+ n_css_rules++;
+ if (n_css_rules == HIDER_LIST_MAX)
+ {
+ g_string_append(css_rule, "{display:none!important;}");
+ m_css_hider_list = g_slist_prepend(m_css_hider_list, css_rule->str);
+ n_css_rules = 0;
+ g_string_free(css_rule, false);
+ css_rule = g_string_new(NULL);
+ }
+ else {
+ g_string_append_c(css_rule, ',');
+ }
}
}
/* Request patterns */
@@ -798,14 +778,22 @@ error_out:
g_free(tmp_a);
g_free(tmp_b);
}
+ if (css_rule->len > 0) {
+ g_string_erase(css_rule, css_rule->len-1, 1);
+ g_string_append(css_rule, "{display:none!important;}");
+ m_css_hider_list = g_slist_prepend(m_css_hider_list, css_rule->str);
+ }
+ g_string_free(css_rule, false);
g_strfreev(lines);
}/*}}}*/
/* adblock_end() {{{*/
void
adblock_end() {
- if (m_css_rules != NULL)
- g_string_free(m_css_rules, true);
+ for (GSList *l = m_css_hider_list; l; l=l->next) {
+ g_free(l->data);
+ }
+ g_slist_free(m_css_hider_list);
if (m_css_exceptions != NULL)
g_string_free(m_css_exceptions, true);
if (m_rules != NULL)
@@ -847,7 +835,6 @@ adblock_init() {
m_simple_exceptions = g_ptr_array_new_with_free_func((GDestroyNotify)adblock_rule_free);
m_hider_rules = g_hash_table_new_full((GHashFunc)g_str_hash, (GEqualFunc)g_str_equal, (GDestroyNotify)g_free, NULL);
m_css_exceptions = g_string_new(NULL);
- m_css_rules = g_string_new(NULL);
adblock_rule_parse(filterlist);
m_init = true;
return true;
diff --git a/src/commands.c b/src/commands.c
index 5e85035c..af99cc5b 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -119,7 +119,7 @@ commands_focus_input(KeyMap *km, Arg *a) {
char *value;
DwbStatus ret = STATUS_OK;
- if ( (value = js_call_as_function(MAIN_FRAME(), CURRENT_VIEW()->hint_object, "focusInput", NULL, &value)) ) {
+ if ( (value = js_call_as_function(MAIN_FRAME(), CURRENT_VIEW()->hint_object, "focusInput", NULL, kJSTypeUndefined, &value)) ) {
if (!g_strcmp0(value, "_dwb_no_input_"))
ret = STATUS_ERROR;
g_free(value);
@@ -132,7 +132,7 @@ commands_focus_input(KeyMap *km, Arg *a) {
DwbStatus
commands_add_search_field(KeyMap *km, Arg *a) {
char *value;
- if ( (value = js_call_as_function(MAIN_FRAME(), CURRENT_VIEW()->hint_object, "addSearchEngine", NULL, &value)) ) {
+ if ( (value = js_call_as_function(MAIN_FRAME(), CURRENT_VIEW()->hint_object, "addSearchEngine", NULL, kJSTypeUndefined, &value)) ) {
if (!g_strcmp0(value, "_dwb_no_hints_")) {
return STATUS_ERROR;
}
diff --git a/src/dwb.c b/src/dwb.c
index d33a222d..b46f140e 100644
--- a/src/dwb.c
+++ b/src/dwb.c
@@ -1692,7 +1692,7 @@ dwb_submit_searchengine(void) {
hint_search_submit = HINT_SEARCH_SUBMIT;
}
snprintf(buffer, sizeof(buffer), "{ \"searchString\" : \"%s\" }", hint_search_submit);
- if ( (value = js_call_as_function(MAIN_FRAME(), CURRENT_VIEW()->hint_object, "submitSearchEngine", buffer, &value)) ) {
+ if ( (value = js_call_as_function(MAIN_FRAME(), CURRENT_VIEW()->hint_object, "submitSearchEngine", buffer, kJSTypeUndefined, &value)) ) {
dwb.state.form_name = value;
}
}/*}}}*/
@@ -1841,7 +1841,7 @@ dwb_update_hints(GdkEventKey *e) {
g_free(val);
}
if (com) {
- buffer = js_call_as_function(MAIN_FRAME(), CURRENT_VIEW()->hint_object, com, *json ? json : NULL, &buffer);
+ buffer = js_call_as_function(MAIN_FRAME(), CURRENT_VIEW()->hint_object, com, *json ? json : NULL, kJSTypeObject, &buffer);
}
if (buffer != NULL) {
if (dwb_evaluate_hints(buffer) == STATUS_END)
@@ -1865,7 +1865,7 @@ dwb_show_hints(Arg *arg) {
(dwb.state.nv & (OPEN_NEW_WINDOW|OPEN_NEW_VIEW)),
hint_map[arg->i].arg);
char *jsret;
- js_call_as_function(MAIN_FRAME(), CURRENT_VIEW()->hint_object, "showHints", json, &jsret);
+ js_call_as_function(MAIN_FRAME(), CURRENT_VIEW()->hint_object, "showHints", json, kJSTypeObject, &jsret);
if (jsret != NULL) {
ret = dwb_evaluate_hints(jsret);
g_free(jsret);
@@ -2636,7 +2636,7 @@ dwb_normal_mode(gboolean clean) {
return STATUS_OK;
if (mode == HINT_MODE || mode == SEARCH_FIELD_MODE) {
- js_call_as_function(MAIN_FRAME(), CURRENT_VIEW()->hint_object, "clear", NULL, NULL);
+ js_call_as_function(MAIN_FRAME(), CURRENT_VIEW()->hint_object, "clear", NULL, kJSTypeUndefined, NULL);
}
else if (mode == DOWNLOAD_GET_PATH) {
completion_clean_path_completion();
diff --git a/src/js.c b/src/js.c
index 02a83618..1e5246d1 100644
--- a/src/js.c
+++ b/src/js.c
@@ -167,7 +167,7 @@ js_create_object(WebKitWebFrame *frame, const char *script) {
/* js_call_as_function(WebKitWebFrame, JSObjectRef, char *string, char *json, * char **ret) {{{*/
char *
-js_call_as_function(WebKitWebFrame *frame, JSObjectRef obj, const char *string, const char *json, char **char_ret) {
+js_call_as_function(WebKitWebFrame *frame, JSObjectRef obj, const char *string, const char *json, JSType arg_type, char **char_ret) {
char *ret = NULL;
JSValueRef js_ret, function, v = NULL;
JSObjectRef function_object;
@@ -187,9 +187,18 @@ js_call_as_function(WebKitWebFrame *frame, JSObjectRef obj, const char *string,
function = JSObjectGetProperty(ctx, obj, js_name, NULL);
function_object = JSValueToObject(ctx, function, NULL);
if (json != NULL) {
- js_json = JSStringCreateWithUTF8CString(json);
- v = JSValueMakeFromJSONString(ctx, js_json);
- JSStringRelease(js_json);
+ switch(arg_type) {
+ case kJSTypeObject :
+ js_json = JSStringCreateWithUTF8CString(json);
+ v = JSValueMakeFromJSONString(ctx, js_json);
+ JSStringRelease(js_json);
+ break;
+ case kJSTypeString :
+ v = js_char_to_value(ctx, json);
+ break;
+ default :
+ break;
+ }
}
if (v) {
JSValueRef vals[] = { v };
diff --git a/src/js.h b/src/js.h
index 40dbdd2e..9fe6fd7b 100644
--- a/src/js.h
+++ b/src/js.h
@@ -30,7 +30,7 @@ void js_set_object_number_property(JSContextRef ctx, JSObjectRef arg, const char
char * js_get_string_property(JSContextRef ctx, JSObjectRef arg, const char *name);
double js_get_double_property(JSContextRef ctx, JSObjectRef arg, const char *name);
JSObjectRef js_create_object(WebKitWebFrame *, const char *);
-char * js_call_as_function(WebKitWebFrame *, JSObjectRef, const char *string, const char *args, char **char_ret);
+char * js_call_as_function(WebKitWebFrame *, JSObjectRef, const char *string, const char *args, JSType, char **char_ret);
JSValueRef js_char_to_value(JSContextRef ctx, const char *text);
char * js_value_to_json(JSContextRef ctx, JSValueRef value, size_t limit, JSValueRef *exc);
JSValueRef js_execute(JSContextRef ctx, const char *, JSValueRef *exc);
diff --git a/src/util.c b/src/util.c
index f7db654e..23974ddf 100644
--- a/src/util.c
+++ b/src/util.c
@@ -612,8 +612,8 @@ util_domain_from_uri(const char *uri) {
if ( (p = strstr(uri, "://")) ) {
uri_p = p + 3;
}
- if ( (p = strchr(uri_p, '/')) ) {
- strncpy(domain, uri_p, MIN(p - uri_p, sizeof(domain)));
+ if ( (p = strchr(uri_p, '/')) != NULL ) {
+ strncpy(domain, uri_p, MIN((unsigned int)(p - uri_p), sizeof(domain)));
}
char *ret = domain[0] ? domain : uri_p;
return g_strdup(ret);
diff --git a/src/view.c b/src/view.c
index 7d953e17..4658ae0e 100644
--- a/src/view.c
+++ b/src/view.c
@@ -635,7 +635,7 @@ view_load_status_after_cb(WebKitWebView *web, GParamSpec *pspec, GList *gl) {
VIEW(gl)->hint_object = NULL;
}
VIEW(gl)->hint_object = js_create_object(webkit_web_view_get_main_frame(web), dwb.misc.hints);
- js_call_as_function(webkit_web_view_get_main_frame(web), VIEW(gl)->hint_object, "init", dwb.misc.hint_style, NULL);
+ js_call_as_function(webkit_web_view_get_main_frame(web), VIEW(gl)->hint_object, "init", dwb.misc.hint_style, kJSTypeObject, NULL);
}
}/*}}}*/
/* view_load_status_cb {{{*/
@@ -659,7 +659,7 @@ view_load_status_cb(WebKitWebView *web, GParamSpec *pspec, GList *gl) {
/* This is more or less a dummy call, to compile the script and speed up
* execution time
* */
- js_call_as_function(webkit_web_view_get_main_frame(web), v->hint_object, "createStyleSheet", NULL, NULL);
+ js_call_as_function(webkit_web_view_get_main_frame(web), v->hint_object, "createStyleSheet", NULL, kJSTypeUndefined, NULL);
break;
case WEBKIT_LOAD_COMMITTED:
if (v->status->scripts & SCRIPTS_ALLOWED_TEMPORARY) {