最近想做一个东西,需要使用webkit,但是,很多功能单纯的js是无法完成的,这时候就想到了要扩展js。
查找了部分资料,找到方法如下:
1、在文档中,发现”window-object-cleared“这个信号中有这么一句话,”This is the preferred place to set custom properties on the window object using the JavaScriptCore API”,然后,对此查找到代码,修改后得到想要的效果。
2、首先创建一个需要扩展的对象Away,这里最为关键就是Away_ClassCreate了:
JSClassRef Away_ClassCreate(JSContextRef ctx)
{
static JSClassRef awayClass = NULL;
if (awayClass)
{
// already created, just return
return awayClass;
}
JSStaticFunction awayStaticFunctions[] =
{
{ "show", away_Show, kJSPropertyAttributeNone },
{ "print", away_Print, kJSPropertyAttributeNone },
{ NULL, 0, 0 },
};
JSStaticValue awayStaticValues[] =
{
{ "Version", away_GetVersion, NULL, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
{ "Verbose", away_GetVerbose, NULL, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
{ NULL, 0, 0, 0},
};
JSClassDefinition classdef = kJSClassDefinitionEmpty;
classdef.className = "Away";
classdef.initialize = away_Initialize;
classdef.finalize = away_Finalize;
classdef.staticValues = awayStaticValues;
classdef.staticFunctions = awayStaticFunctions;
return awayClass = JSClassCreate(&classdef);
}
void away_Initialize(JSContextRef ctx, JSObjectRef object)
{
}
void away_Finalize(JSObjectRef object)
{
}
JSValueRef away_GetVerbose(
JSContextRef ctx,
JSObjectRef object,
JSStringRef propertyName,
JSValueRef *exception)
{
// verbose is false
return JSValueMakeBoolean(ctx, false);
}
JSValueRef away_GetVersion(
JSContextRef ctx,
JSObjectRef object,
JSStringRef propertyName,
JSValueRef *exception)
{
// verbose is false
static JSStringRef version = NULL;
if (version == NULL)
{
version = JSStringCreateWithUTF8CString("0.1");
}
return JSValueMakeString(ctx, version);
}
JSValueRef away_Show(JSContextRef ctx,
JSObjectRef function,
JSObjectRef thisObject,
size_t argumentCount,
const JSValueRef arguments[],
JSValueRef *exception)
{
JSStringRef str = JSValueToStringCopy(ctx, arguments[0], exception);
size_t size = JSStringGetMaximumUTF8CStringSize(str);
char* utf8 = NEW(char, size);
JSStringGetUTF8CString(str, utf8, size);
GtkWidget *dia = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, utf8);
gtk_dialog_run(dia);
gtk_widget_destroy(dia);
return JSValueMakeNumber(ctx, size);
}
JSValueRef away_Print(JSContextRef ctx,
JSObjectRef function,
JSObjectRef thisObject,
size_t argumentCount,
const JSValueRef arguments[],
JSValueRef *exception)
{
JSStringRef str = JSValueToStringCopy(ctx, arguments[0], exception);
size_t size = JSStringGetMaximumUTF8CStringSize(str);
char* utf8 = NEW(char, size);
JSStringGetUTF8CString(str, utf8, size);
fprintf(stderr, "%s\n", utf8);
return JSValueMakeNumber(ctx, size);
}
3、创建window-object-cleared信号函数:
void away_WindowObjectClearedCB(
WebKitWebView *wv,
WebKitWebFrame *wf,
gpointer ctx,
gpointer arg3,
gpointer user_data)
{
JSStringRef name = JSStringCreateWithUTF8CString("Away");
// Make the javascript object
JSObjectRef obj = JSObjectMake(ctx, Away_ClassCreate(ctx), NULL);
// Set the property
JSObjectSetProperty(ctx, JSContextGetGlobalObject(ctx), name, obj,kJSPropertyAttributeNone, NULL);
JSObjectRef obj2 = JSObjectMake(ctx, Away_ClassCreate(ctx), NULL);
// Set the property
JSObjectSetProperty(ctx, obj, name, obj2,kJSPropertyAttributeNone, NULL);
}
4、webview的相关代码:
webview = webkit_web_view_new(); webkit_web_view_set_custom_encoding(WEBKIT_WEB_VIEW(webview), "UTF-8"); /*设置编码为UTF-8 */ g_signal_connect(G_OBJECT (webview), "window-object-cleared", G_CALLBACK(away_WindowObjectClearedCB), webview); webkit_web_view_load_uri(WEBKIT_WEB_VIEW(webview), "file:///home/Tom/Projects/GtkWebKitTest/test.html"); /*载入测试文件*/5、html文件如下:
<html> <head> <title>TestGTKWebKit JS</title> <script type="text/javascript"> function show(){ var str = document.getElementById("text").value; document.getElementById("size").innerHTML = "Sizeof Str:" + Away.Away.show(str); Away.print(str); } </script> </head> <body> <input id="text" type="text" value="你好,Webkit!"/> <input type="button" onclick="show()" value="Show"/> <span id="size"></span><br /> <h2>Away Info:</h2> <div id="AInfo"> </div> <script type="text/javascript"> document.getElementById("AInfo").innerHTML = Away.Version + "<br />"; </script> </body>效果截图:

webkitgtk
怎么能模拟点击 谢谢