{"id":805,"date":"2011-11-23T11:45:44","date_gmt":"2011-11-23T03:45:44","guid":{"rendered":"http:\/\/www.awaysoft.com\/taor\/?p=805"},"modified":"2014-12-11T09:15:26","modified_gmt":"2014-12-11T01:15:26","slug":"%e5%9c%a8webkitgtk-%e4%b8%ad%e6%89%a9%e5%b1%95js","status":"publish","type":"post","link":"https:\/\/actom.me\/blog\/%e5%9c%a8webkitgtk-%e4%b8%ad%e6%89%a9%e5%b1%95js.html","title":{"rendered":"\u5728WebKitGTK \u4e2d\u6269\u5c55JS"},"content":{"rendered":"<p>\u6700\u8fd1\u60f3\u505a\u4e00\u4e2a\u4e1c\u897f\uff0c\u9700\u8981\u4f7f\u7528webkit\uff0c\u4f46\u662f\uff0c\u5f88\u591a\u529f\u80fd\u5355\u7eaf\u7684js\u662f\u65e0\u6cd5\u5b8c\u6210\u7684\uff0c\u8fd9\u65f6\u5019\u5c31\u60f3\u5230\u4e86\u8981\u6269\u5c55js\u3002<\/p>\n<p>\u67e5\u627e\u4e86\u90e8\u5206\u8d44\u6599\uff0c\u627e\u5230\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>1\u3001\u5728\u6587\u6863\u4e2d\uff0c\u53d1\u73b0&#8221;<a title=\"The &quot;window-object-cleared&quot; signal\" href=\"http:\/\/webkitgtk.org\/reference\/webkitgtk\/unstable\/webkitgtk-webkitwebview.html#WebKitWebView-window-object-cleared\">window-object-cleared<\/a>&#8220;\u8fd9\u4e2a\u4fe1\u53f7\u4e2d\u6709\u8fd9\u4e48\u4e00\u53e5\u8bdd\uff0c&#8221;This is the preferred place to set custom properties on the window object using the JavaScriptCore API&#8221;\uff0c\u7136\u540e\uff0c\u5bf9\u6b64\u67e5\u627e\u5230\u4ee3\u7801\uff0c\u4fee\u6539\u540e\u5f97\u5230\u60f3\u8981\u7684\u6548\u679c\u3002<\/p>\n<p>2\u3001\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u9700\u8981\u6269\u5c55\u7684\u5bf9\u8c61Away\uff0c\u8fd9\u91cc\u6700\u4e3a\u5173\u952e\u5c31\u662fAway_ClassCreate\u4e86\uff1a<\/p>\n<pre class=\"lang:c decode:true \">JSClassRef Away_ClassCreate(JSContextRef ctx)\r\n{\r\nstatic JSClassRef awayClass = NULL;\r\nif (awayClass)\r\n{\r\n\/\/ already created, just return\r\nreturn awayClass;\r\n}\r\n\r\nJSStaticFunction awayStaticFunctions[] =\r\n{\r\n{ \"show\", away_Show, kJSPropertyAttributeNone },\r\n{ \"print\", away_Print, kJSPropertyAttributeNone },\r\n{ NULL, 0, 0 },\r\n};\r\n\r\nJSStaticValue awayStaticValues[] =\r\n{\r\n{ \"Version\", away_GetVersion, NULL, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },\r\n{ \"Verbose\", away_GetVerbose, NULL, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },\r\n{ NULL, 0, 0, 0},\r\n};\r\n\r\nJSClassDefinition classdef = kJSClassDefinitionEmpty;\r\nclassdef.className = \"Away\";\r\nclassdef.initialize = away_Initialize;\r\nclassdef.finalize = away_Finalize;\r\nclassdef.staticValues = awayStaticValues;\r\nclassdef.staticFunctions = awayStaticFunctions;\r\n\r\nreturn awayClass = JSClassCreate(&amp;classdef);\r\n}\r\n\r\nvoid away_Initialize(JSContextRef ctx, JSObjectRef object)\r\n{\r\n}\r\n\r\nvoid away_Finalize(JSObjectRef object)\r\n{\r\n}\r\n\r\nJSValueRef away_GetVerbose(\r\nJSContextRef ctx,\r\nJSObjectRef object,\r\nJSStringRef propertyName,\r\nJSValueRef *exception)\r\n{\r\n\/\/ verbose is false\r\nreturn JSValueMakeBoolean(ctx, false);\r\n}\r\n\r\nJSValueRef away_GetVersion(\r\nJSContextRef ctx,\r\nJSObjectRef object,\r\nJSStringRef propertyName,\r\nJSValueRef *exception)\r\n{\r\n\/\/ verbose is false\r\nstatic JSStringRef version = NULL;\r\nif (version == NULL)\r\n{\r\nversion = JSStringCreateWithUTF8CString(\"0.1\");\r\n}\r\nreturn JSValueMakeString(ctx, version);\r\n}\r\n\r\nJSValueRef away_Show(JSContextRef ctx,\r\nJSObjectRef function,\r\nJSObjectRef thisObject,\r\nsize_t argumentCount,\r\nconst JSValueRef arguments[],\r\nJSValueRef *exception)\r\n{\r\nJSStringRef str = JSValueToStringCopy(ctx, arguments[0], exception);\r\nsize_t size = JSStringGetMaximumUTF8CStringSize(str);\r\nchar* utf8 = NEW(char, size);\r\n\r\nJSStringGetUTF8CString(str, utf8, size);\r\n\r\nGtkWidget *dia = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, utf8);\r\n\r\ngtk_dialog_run(dia);\r\n\r\ngtk_widget_destroy(dia);\r\n\r\nreturn JSValueMakeNumber(ctx, size);\r\n}\r\n\r\nJSValueRef away_Print(JSContextRef ctx,\r\nJSObjectRef function,\r\nJSObjectRef thisObject,\r\nsize_t argumentCount,\r\nconst JSValueRef arguments[],\r\nJSValueRef *exception)\r\n{\r\nJSStringRef str = JSValueToStringCopy(ctx, arguments[0], exception);\r\nsize_t size = JSStringGetMaximumUTF8CStringSize(str);\r\nchar* utf8 = NEW(char, size);\r\n\r\nJSStringGetUTF8CString(str, utf8, size);\r\n\r\nfprintf(stderr, \"%s\\n\", utf8);\r\n\r\nreturn JSValueMakeNumber(ctx, size);\r\n}<\/pre>\n<p>3\u3001\u521b\u5efa<code>window-object-cleared\u4fe1\u53f7<\/code>\u51fd\u6570\uff1a<\/p>\n<pre class=\"lang:c decode:true \">void away_WindowObjectClearedCB(\r\nWebKitWebView *wv,\r\nWebKitWebFrame *wf,\r\ngpointer ctx,\r\ngpointer arg3,\r\ngpointer user_data)\r\n{\r\nJSStringRef name = JSStringCreateWithUTF8CString(\"Away\");\r\n\/\/ Make the javascript object\r\nJSObjectRef obj = JSObjectMake(ctx, Away_ClassCreate(ctx), NULL);\r\n\/\/ Set the property\r\nJSObjectSetProperty(ctx, JSContextGetGlobalObject(ctx), name, obj,kJSPropertyAttributeNone, NULL);\r\n\r\nJSObjectRef obj2 = JSObjectMake(ctx, Away_ClassCreate(ctx), NULL);\r\n\/\/ Set the property\r\nJSObjectSetProperty(ctx, obj, name, obj2,kJSPropertyAttributeNone, NULL);\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>4\u3001<code>webview\u7684\u76f8\u5173\u4ee3\u7801\uff1a<\/code><\/p>\n<blockquote>\n<pre class=\"lang:c decode:true \">webview = webkit_web_view_new();\r\nwebkit_web_view_set_custom_encoding(WEBKIT_WEB_VIEW(webview), \"UTF-8\"); \/*\u8bbe\u7f6e\u7f16\u7801\u4e3aUTF-8 *\/\r\ng_signal_connect(G_OBJECT (webview), \"window-object-cleared\", G_CALLBACK(away_WindowObjectClearedCB), webview);\r\nwebkit_web_view_load_uri(WEBKIT_WEB_VIEW(webview), \"file:\/\/\/home\/Tom\/Projects\/GtkWebKitTest\/test.html\"); \/*\u8f7d\u5165\u6d4b\u8bd5\u6587\u4ef6*\/<\/pre>\n<p>5\u3001html\u6587\u4ef6\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:xhtml decode:true \">&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;title&gt;TestGTKWebKit JS&lt;\/title&gt;\r\n&lt;script type=\"text\/javascript\"&gt;\r\nfunction show(){\r\nvar str = document.getElementById(\"text\").value;\r\ndocument.getElementById(\"size\").innerHTML = \"Sizeof Str:\" + Away.Away.show(str);\r\nAway.print(str);\r\n}\r\n&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\r\n&lt;input id=\"text\" type=\"text\" value=\"\u4f60\u597d,Webkit!\"\/&gt;&amp;nbsp;&lt;input type=\"button\" onclick=\"show()\" value=\"Show\"\/&gt;&amp;nbsp;&lt;span id=\"size\"&gt;&lt;\/span&gt;&lt;br \/&gt;\r\n&lt;h2&gt;Away Info:&lt;\/h2&gt;\r\n&lt;div id=\"AInfo\"&gt;\r\n&lt;\/div&gt;\r\n\r\n&lt;script type=\"text\/javascript\"&gt;\r\ndocument.getElementById(\"AInfo\").innerHTML = Away.Version + \"&lt;br \/&gt;\";\r\n&lt;\/script&gt;\r\n\r\n&lt;\/body&gt;<\/pre>\n<p>\u6548\u679c\u622a\u56fe\uff1a<\/p>\n<p><a href=\"http:\/\/www.awaysoft.com\/taor\/wp-content\/uploads\/2011\/11\/Screenshot-at-2011-11-23-114343.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-806\" title=\"GTKWebkit JS\" src=\"http:\/\/www.awaysoft.com\/taor\/wp-content\/uploads\/2011\/11\/Screenshot-at-2011-11-23-114343.png\" alt=\"\" width=\"1013\" height=\"475\" srcset=\"https:\/\/actom.me\/blog\/wp-content\/uploads\/2011\/11\/Screenshot-at-2011-11-23-114343.png 1013w, https:\/\/actom.me\/blog\/wp-content\/uploads\/2011\/11\/Screenshot-at-2011-11-23-114343-300x140.png 300w\" sizes=\"(max-width: 1013px) 100vw, 1013px\" \/><\/a><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u60f3\u505a\u4e00\u4e2a\u4e1c\u897f\uff0c\u9700\u8981\u4f7f\u7528webkit\uff0c\u4f46\u662f\uff0c\u5f88\u591a\u529f\u80fd\u5355\u7eaf\u7684js\u662f\u65e0\u6cd5\u5b8c\u6210\u7684\uff0c\u8fd9 &#8230; <a class=\"more-link\" href=\"https:\/\/actom.me\/blog\/%e5%9c%a8webkitgtk-%e4%b8%ad%e6%89%a9%e5%b1%95js.html\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/posts\/805"}],"collection":[{"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/comments?post=805"}],"version-history":[{"count":4,"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/posts\/805\/revisions"}],"predecessor-version":[{"id":1133,"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/posts\/805\/revisions\/1133"}],"wp:attachment":[{"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/media?parent=805"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/categories?post=805"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/tags?post=805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}