summaryrefslogtreecommitdiff
path: root/src/js.c
diff options
context:
space:
mode:
authorportix <portix@gmx.net>2011-11-26 01:04:50 +0100
committerportix <portix@gmx.net>2011-11-26 01:04:50 +0100
commitd37a80ca051283fc1291de92dd49478080ac468c (patch)
treeeaeaef753e84dc7f32156362f11b170c03d7cd9b /src/js.c
parent5374ef2365cc5398a8df20726771220febfa6257 (diff)
downloaddwb-d37a80ca051283fc1291de92dd49478080ac468c.zip
Adblocker: get html-elements with javascript function
--HG-- branch : experimental
Diffstat (limited to 'src/js.c')
-rw-r--r--src/js.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/js.c b/src/js.c
new file mode 100644
index 00000000..34cf39dc
--- /dev/null
+++ b/src/js.c
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2010-2011 Stefan Bolte <portix@gmx.net>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <JavaScriptCore/JavaScript.h>
+#include <webkit/webkit.h>
+#include <glib-2.0/glib.h>
+
+
+void
+js_create_callback(WebKitWebFrame *frame, const char *name, JSObjectCallAsFunctionCallback function) {
+ JSContextRef ctx = webkit_web_frame_get_global_context(frame);
+ JSStringRef jsname = JSStringCreateWithUTF8CString(name);
+ JSObjectRef jsfunction = JSObjectMakeFunctionWithCallback(ctx, jsname, function);
+ JSObjectSetProperty(ctx, JSContextGetGlobalObject(ctx), jsname, jsfunction,
+ kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly, NULL);
+ JSStringRelease(jsname);
+}
+
+/* js_string_to_char
+ * Converts a JSStringRef, return a newly allocated char.
+ * {{{*/
+char *
+js_string_to_char(JSContextRef ctx, JSStringRef jsstring) {
+ size_t length = JSStringGetLength(jsstring) + 1;
+
+ char *ret = g_new(char, length);
+ size_t written = JSStringGetUTF8CString(jsstring, ret, length);
+ /* TODO: handle length error */
+ if (written != length)
+ return NULL;
+ return ret;
+}/*}}}*/
+
+/*{{{*/
+char *
+js_value_to_char(JSContextRef ctx, JSValueRef value) {
+ JSValueRef exc = NULL;
+ if (! JSValueIsString(ctx, value))
+ return NULL;
+ JSStringRef jsstring = JSValueToStringCopy(ctx, value, &exc);
+ if (exc != NULL)
+ return NULL;
+
+ char *ret = js_string_to_char(ctx, jsstring);
+ JSStringRelease(jsstring);
+ return ret;
+}/*}}}*/