GTK+中文社区(gtk.awaysoft.com)

 找回密码
 马上加入

QQ登录

只需一步,快速开始

查看: 4747|回复: 3

在WebKitGTK 中扩展JS

[复制链接]
  • TA的每日心情
    奋斗
    2016-10-11 09:20
  • 签到天数: 271 天

    连续签到: 1 天

    [LV.8]以坛为家I

    发表于 2011-11-23 20:14:30 | 显示全部楼层 |阅读模式
    最近想做一个东西,需要使用webkit,但是,很多功能单纯的js是无法完成的,这时候就想到了要扩展js。
    查找了部分资料,找到方法如下:
    1、在文档中,发现”window-object-cleared“这个信号中有这么一句话,”This is the preferred place to set custom properties on thewindow object using the JavaScriptCore API”,然后,对此查找到代码,修改后得到想要的效果。
    2、首先创建一个需要扩展的对象Away,这里最为关键就是Away_ClassCreate了:[blockquote]
    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);
    }
    [/blockquote]
    3、创建window-object-cleared信号函数:[blockquote]
    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);
    }[/blockquote]
    4、webview的相关代码:[blockquote]
    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”); /*载入测试文件*/[/blockquote]
    5、html文件如下:[blockquote]
    <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”/> <spanid=”size”></span><br />
    <h2>Away Info:</h2>
    <div id=”AInfo”>
    </div>
    <script type=”text/javascript”>
    document.getElementById(“AInfo”).innerHTML = Away.Version + “<br />”;
    </script>
    </body>[/blockquote]
    效果截图:


    原文地址:http://www.awaysoft.com/taor/%E5 ... %A9%E5%B1%95js.html

    该用户从未签到

    发表于 2011-11-29 17:17:53 | 显示全部楼层
    你这个是Linux的kde吗?感觉好像kde的界面~请问webkitgtk+在Windows下怎么装到gtk+里去?好像找不到Windows的webkitgtk+啊~

    该用户从未签到

    发表于 2012-8-11 10:40:13 | 显示全部楼层
    同求webkitgtk+的Windows版
  • TA的每日心情
    奋斗
    2016-10-11 09:20
  • 签到天数: 271 天

    连续签到: 1 天

    [LV.8]以坛为家I

     楼主| 发表于 2012-8-11 23:06:56 | 显示全部楼层

    回 chfm 的帖子

    chfm:同求webkitgtk+的Windows版 (2012-08-11 10:40) 
    先回答楼上的问题,这是Fedora的gnome3界面。

    如果你想要windows版本,建议安装一个Fedora,使用mingw32来编译,安装mingw32-webkitgtk3包,使用i686-mingw32-gcc命令来编译(我记得大概是这样,不是很记得命令了)。然后把/usr/mingw32/....下面的一些dll复制过来就好。。。
    *滑块验证:
    您需要登录后才可以回帖 登录 | 马上加入

    本版积分规则

    申请友链|Archiver|小黑屋|手机版|GTK+中文社区 ( 粤ICP备13080851号 )

    我要啦免费统计

    GMT+8, 2024-4-29 02:58 , Processed in 0.767709 second(s), 7 queries , Redis On.

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表