打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
涛儿的小窝 ? 在WebKitGTK 中扩展JS

 

最近想做一个东西,需要使用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>

效果截图:

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
[iOS] 在桌面應用程式中使用 WebKit (2) - JavaScriptCore
sqlite 数据库基本操作
c++ 读文件,读取到中文如果是乱码的处理方式。
OpenSSL: 消息摘要算法
使用Spring中的IoC功能来实现我们所开发项目系统的国际化
java中文乱码解决总结
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服