surf-adblock.c - surf-adblock - Surf adblock web extension | |
git clone git://git.codemadness.org/surf-adblock | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
surf-adblock.c (2486B) | |
--- | |
1 #include <errno.h> | |
2 #include <stdio.h> | |
3 #include <stdlib.h> | |
4 #include <string.h> | |
5 | |
6 #include <webkit2/webkit-web-extension.h> | |
7 #include <webkitdom/webkitdom.h> | |
8 | |
9 #include "adblock.h" | |
10 | |
11 typedef struct Page { | |
12 guint64 id; | |
13 WebKitWebPage *webpage; | |
14 struct Page *next; | |
15 } Page; | |
16 | |
17 static Page *pages; | |
18 | |
19 static Page * | |
20 newpage(WebKitWebPage *page) | |
21 { | |
22 Page *p; | |
23 | |
24 if (!(p = calloc(1, sizeof(Page)))) { | |
25 fprintf(stderr, "surf-adblock: calloc: %s\n", strerror(e… | |
26 return NULL; | |
27 } | |
28 p->id = webkit_web_page_get_id(page); | |
29 p->webpage = page; | |
30 p->next = pages; | |
31 | |
32 pages = p; | |
33 | |
34 return p; | |
35 } | |
36 | |
37 static void | |
38 documentloaded(WebKitWebPage *wp, Page *p) | |
39 { | |
40 WebKitDOMDocument *doc = webkit_web_page_get_dom_document(wp); | |
41 WebKitDOMHTMLElement *head = webkit_dom_document_get_body(doc); | |
42 WebKitDOMElement *el; | |
43 const char *uri = webkit_web_page_get_uri(p->webpage); | |
44 char *css, *globalcss; | |
45 | |
46 if ((globalcss = getglobalcss())) { | |
47 el = webkit_dom_document_create_element(doc, "style", NU… | |
48 webkit_dom_element_set_attribute(el, "type", "text/css",… | |
49 webkit_dom_element_set_inner_html(el, globalcss, NULL); | |
50 webkit_dom_node_append_child(WEBKIT_DOM_NODE(head), | |
51 WEBKIT_DOM_NODE(el), NULL); | |
52 } | |
53 | |
54 if ((css = getdocumentcss(uri))) { | |
55 el = webkit_dom_document_create_element(doc, "style", NU… | |
56 webkit_dom_element_set_attribute(el, "type", "text/css",… | |
57 webkit_dom_element_set_inner_html(el, css, NULL); | |
58 webkit_dom_node_append_child(WEBKIT_DOM_NODE(head), | |
59 WEBKIT_DOM_NODE(el), NULL); | |
60 } | |
61 | |
62 free(css); | |
63 /* NOTE: globalcss should not be free'd */ | |
64 } | |
65 | |
66 static gboolean | |
67 sendrequest(WebKitWebPage *wp, WebKitURIRequest *req, | |
68 WebKitURIResponse *res, Page *p) | |
69 { | |
70 const char *fromuri, *requri; | |
71 | |
72 if (!webkit_uri_request_get_http_method(req)) | |
73 return TRUE; /* TRUE = don't handle any more events */ | |
74 fromuri = webkit_web_page_get_uri(p->webpage); | |
75 requri = webkit_uri_request_get_uri(req); | |
76 | |
77 return allowrequest(fromuri, requri) ? FALSE : TRUE; | |
78 } | |
79 | |
80 static void | |
81 webpagecreated(WebKitWebExtension *e, WebKitWebPage *p, gpointer unused) | |
82 { | |
83 Page *np; | |
84 | |
85 if (!(np = newpage(p))) { | |
86 fprintf(stderr, "surf-adblock: cannot associate webext w… | |
87 return; | |
88 } | |
89 | |
90 g_signal_connect(p, "document-loaded", G_CALLBACK(documentloaded… | |
91 g_signal_connect(p, "send-request", G_CALLBACK(sendrequest), np); | |
92 } | |
93 | |
94 G_MODULE_EXPORT void | |
95 webkit_web_extension_initialize(WebKitWebExtension *ext) | |
96 { | |
97 init(); | |
98 g_signal_connect(ext, "page-created", G_CALLBACK(webpagecreated)… | |
99 } |