summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorportix <none@none>2012-09-25 11:28:42 +0200
committerportix <none@none>2012-09-25 11:28:42 +0200
commit3766f55ac54b228782f82e981416cc06f9b30bb8 (patch)
tree59a9556d6e3c24176f7525747bc6ed4bde772340 /src
parentf0a3b9d7f5c3ed6cfc067ae7da8c35af334a7b47 (diff)
downloaddwb-3766f55ac54b228782f82e981416cc06f9b30bb8.zip
Optional argument for inject function
Diffstat (limited to 'src')
-rw-r--r--src/js.c17
-rw-r--r--src/js.h1
-rw-r--r--src/scripts.c12
3 files changed, 27 insertions, 3 deletions
diff --git a/src/js.c b/src/js.c
index ce566175..548d1f13 100644
--- a/src/js.c
+++ b/src/js.c
@@ -114,6 +114,23 @@ js_string_to_char(JSContextRef ctx, JSStringRef jsstring, size_t size) {
return ret;
}/*}}}*/
+JSValueRef
+js_context_change(JSContextRef source_ctx, JSContextRef dest_ctx, JSValueRef val, JSValueRef *exc) {
+ char *c_val = js_value_to_json(source_ctx, val, -1, exc);
+ if (c_val == NULL)
+ return JSValueMakeNull(dest_ctx);
+
+ JSStringRef json = JSStringCreateWithUTF8CString(c_val);
+ JSValueRef ret = JSValueMakeFromJSONString(dest_ctx, json);
+
+ g_free(c_val);
+ JSStringRelease(json);
+
+ if (ret == NULL)
+ return JSValueMakeNull(dest_ctx);
+ return ret;
+}
+
/* js_create_object(WebKitWebFrame *frame, const char *)
*
* Executes a script in a function scope, should return an object with
diff --git a/src/js.h b/src/js.h
index a51cb911..07a4cf58 100644
--- a/src/js.h
+++ b/src/js.h
@@ -35,6 +35,7 @@ JSValueRef js_execute(JSContextRef ctx, const char *, JSValueRef *exc);
gboolean js_print_exception(JSContextRef ctx, JSValueRef exception);
JSObjectRef js_make_function(JSContextRef ctx, const char *script);
JSValueRef js_json_to_value(JSContextRef ctx, const char *text);
+JSValueRef js_context_change(JSContextRef, JSContextRef, JSValueRef, JSValueRef *);
typedef struct _js_array_iterator {
JSContextRef ctx;
diff --git a/src/scripts.c b/src/scripts.c
index e3ede6bd..8851701b 100644
--- a/src/scripts.c
+++ b/src/scripts.c
@@ -194,12 +194,18 @@ static JSValueRef
inject(JSContextRef ctx, JSContextRef wctx, JSObjectRef function, JSObjectRef this, size_t argc, const JSValueRef argv[], JSValueRef* exc) {
JSValueRef ret = NULL;
gboolean global = false;
+ JSValueRef args[1];
+ int count = 0;
if (argc < 1) {
js_make_exception(ctx, exc, EXCEPTION("webview.inject: missing argument"));
return JSValueMakeBoolean(ctx, false);
}
- if (argc > 1 && JSValueIsBoolean(ctx, argv[1]))
- global = JSValueToBoolean(ctx, argv[1]);
+ if (argc > 1 && !JSValueIsNull(ctx, argv[1])) {
+ args[0] = js_context_change(ctx, wctx, argv[1], exc);
+ count = 1;
+ }
+ if (argc > 2 && JSValueIsBoolean(ctx, argv[2]))
+ global = JSValueToBoolean(ctx, argv[2]);
JSStringRef script = JSValueToStringCopy(ctx, argv[0], exc);
if (script == NULL) {
@@ -212,7 +218,7 @@ inject(JSContextRef ctx, JSContextRef wctx, JSObjectRef function, JSObjectRef th
else {
JSObjectRef func = JSObjectMakeFunction(wctx, NULL, 0, NULL, script, NULL, 0, NULL);
if (func != NULL && JSObjectIsFunction(ctx, func)) {
- JSValueRef wret = JSObjectCallAsFunction(wctx, func, NULL, 0, NULL, NULL);
+ JSValueRef wret = JSObjectCallAsFunction(wctx, func, NULL, count, count == 1 ? args : NULL, NULL) ;
char *retx = js_value_to_json(wctx, wret, -1, NULL);
if (retx) {
ret = js_char_to_value(ctx, retx);