Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 397019ca4df693f176b939f79d5380f157d0466b @vrruiz committed Oct 25, 2011
Showing with 175 additions and 0 deletions.
  1. +8 −0 README
  2. +45 −0 webkit-01.c
  3. BIN webkit-02
  4. +111 −0 webkit-02.c
  5. +11 −0 webkit-02.html
8 README
@@ -0,0 +1,8 @@
+Extending JavaScript with WebKit
+================================
+
+Tutorial files to extend JavaScript functions using WebKit and JavaScriptCore.
+
+The tutorial is available at http://rvr.typepad.com/
+
+(C) 2011 Víctor R. Ruiz <rvr@linotipo.es>
45 webkit-01.c
@@ -0,0 +1,45 @@
+#include <gtk/gtk.h>
+#include <webkit/webkit.h>
+
+/* Destroy callback */
+static void destroy( GtkWidget *widget,
+ gpointer data )
+{
+ gtk_main_quit();
+}
+
+int
+main (int argc, char* argv[])
+{
+ /* Initialize the widget set */
+ gtk_init (&argc, &argv);
+
+ /* Create the window widgets */
+ GtkWidget *main_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+ GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL);
+
+ /* Create the WebKit Web View widget */
+ GtkWidget *web_view = webkit_web_view_new ();
+
+ /* Place the WebKitWebView in the GtkScrolledWindow */
+ gtk_container_add (GTK_CONTAINER (scrolled_window), web_view);
+ gtk_container_add (GTK_CONTAINER (main_window), scrolled_window);
+
+ /* Connect the destroy window event with destroy function */
+ g_signal_connect (G_OBJECT (main_window), "destroy", G_CALLBACK (destroy), NULL);
+
+ /* Open webpage */
+ webkit_web_view_load_uri (WEBKIT_WEB_VIEW (web_view), "http://rvr.typepad.com/");
+
+ /* Create the main window */
+ gtk_window_set_default_size (GTK_WINDOW (main_window), 800, 600);
+
+ /* Show the application window */
+ gtk_widget_show_all (main_window);
+
+ /* Enter the main event loop, and wait for user interaction */
+ gtk_main ();
+
+ /* The user lost interest */
+ return 0;
+}
BIN webkit-02
Binary file not shown.
111 webkit-02.c
@@ -0,0 +1,111 @@
+#include <gtk/gtk.h>
+#include <webkit/webkit.h>
+#include <JavaScriptCore/JavaScript.h>
+
+/* Class initialize */
+static void class_init_cb(JSContextRef ctx,
+ JSObjectRef object)
+{
+ g_message("Custom class initialize.");
+}
+
+/* Class finalize */
+static void class_finalize_cb(JSObjectRef object)
+{
+ g_message("Custom class finalize.");
+}
+
+
+/* Class constructor. Called at "new CustomClass()" */
+JSObjectRef class_constructor_cb(JSContextRef ctx,
+ JSObjectRef constructor,
+ size_t argumentCount,
+ const JSValueRef arguments[],
+ JSValueRef* exception)
+{
+ g_message("Custom class constructor");
+}
+
+static const JSClassDefinition class_def =
+{
+ 0, // version
+ kJSClassAttributeNone, // attributes
+ "CustomClass", // className
+ NULL, // parentClass
+ NULL, // staticValues
+ NULL, // staticFunctions
+ class_init_cb, // initialize
+ class_finalize_cb, // finalize
+ NULL, // hasProperty
+ NULL, // getProperty
+ NULL, // setProperty
+ NULL, // deleteProperty
+ NULL, // getPropertyNames
+ NULL, // callAsFunction
+ class_constructor_cb, // callAsConstructor
+ NULL, // hasInstance
+ NULL // convertToType
+};
+
+/* Callback - JavaScript window object has been cleared */
+static void window_object_cleared_cb(WebKitWebView *web_view,
+ WebKitWebFrame *frame,
+ gpointer context,
+ gpointer window_object,
+ gpointer user_data)
+
+{
+ /* Add classes to JavaScriptCore */
+ JSClassRef classDef = JSClassCreate(&class_def);
+ JSObjectRef classObj = JSObjectMake(context, classDef, context);
+ JSObjectRef globalObj = JSContextGetGlobalObject(context);
+ JSStringRef str = JSStringCreateWithUTF8CString("CustomClass");
+ JSObjectSetProperty(context, globalObj, str, classObj, kJSPropertyAttributeNone, NULL);
+}
+
+/* Destroy callback */
+static void destroy(GtkWidget *widget,
+ gpointer data )
+{
+ gtk_main_quit();
+}
+
+int
+main (int argc, char* argv[])
+{
+ /* Initialize the widget set */
+ gtk_init (&argc, &argv);
+
+ /* Create the window widgets */
+ GtkWidget *main_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+ GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL);
+
+ /* Create the WebKit Web View widget */
+ GtkWidget *web_view = webkit_web_view_new ();
+
+ /* Connect the window object cleared event with callback */
+ g_signal_connect (G_OBJECT (web_view), "window-object-cleared", G_CALLBACK(window_object_cleared_cb), web_view);
+
+
+ /* Place the WebKitWebView in the GtkScrolledWindow */
+ gtk_container_add (GTK_CONTAINER (scrolled_window), web_view);
+ gtk_container_add (GTK_CONTAINER (main_window), scrolled_window);
+
+ /* Connect the destroy window event with destroy function */
+ g_signal_connect (G_OBJECT (main_window), "destroy", G_CALLBACK (destroy), NULL);
+
+ /* Open webpage */
+ webkit_web_view_load_uri (WEBKIT_WEB_VIEW (web_view), "file:///home/rvr/Dropbox/devel/webkit/webkit-02.html");
+
+ /* Create the main window */
+ gtk_window_set_default_size (GTK_WINDOW (main_window), 800, 600);
+
+ /* Show the application window */
+ gtk_widget_show_all (main_window);
+
+ /* Enter the main event loop, and wait for user interaction */
+ gtk_main ();
+
+ /* The user lost interest */
+ return 0;
+}
11 webkit-02.html
@@ -0,0 +1,11 @@
+<html>
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+</head>
+<body>
+ <h1>Extending JavaScript with WebKit. Custom Class.</h1>
+ <script type="text/javascript">
+ custom = new CustomClass();
+ </script>
+</body>
+</html>

0 comments on commit 397019c

Please sign in to comment.