Skip to content

Instantly share code, notes, and snippets.

@geoffjay
Created July 18, 2016 21:33
Show Gist options
  • Save geoffjay/dc84e642d412f403b33ae666812761fa to your computer and use it in GitHub Desktop.
Save geoffjay/dc84e642d412f403b33ae666812761fa to your computer and use it in GitHub Desktop.
WebKit Javascript extension
[DBus (name = "org.opendcs.Dcs.Extension")]
public class Dcs.UI.Extension.DOM : GLib.Object {
private int count;
private WebKit.WebPage page;
private static const JSCore.StaticFunction[] class_functions = {{
"get", (JSCore.ObjectCallAsFunctionCallback) meas_get_cb, JSCore.PropertyAttribute.ReadOnly
},{
null, null, 0
}};
private static const JSCore.ClassDefinition class_definition = {
0, // version
JSCore.ClassAttribute.None, // attributes
"Measurement", // className
null, // parentClass
null, // staticValues
class_functions, // staticFunctions
null, // initialize
null, // finalize
null, // hasProperty
null, // getProperty
null, // setProperty
null, // deleteProperty
null, // getPropertyNames
null, // callAsFunction
class_constructor_cb, // callAsConstructor
null, // hasInstance
null // convertToType
};
public signal void div_clicked (string number);
[DBus (visible = false)]
public void bus_acquired_cb (DBusConnection connection) {
try {
connection.register_object ("/org/opendcs/dcs/extension", this);
} catch (IOError error) {
warning ("Could not register service: %s", error.message);
}
}
[DBus (visible = false)]
public void page_created_cb (WebKit.WebExtension extension,
WebKit.WebPage page) {
this.page = page;
WebKit.DOM.Document document = page.get_dom_document ();
debug ("Page %d created for %s with title %s",
(int) page.get_id (),
page.uri,
document.title);
}
[DBus (visible = false)]
public void window_object_cleared_cb (WebKit.ScriptWorld world,
WebKit.WebPage page,
WebKit.Frame frame) {
message ("Window object cleared");
var str = new JSCore.String.with_utf8_c_string ("Extension");
var class = new JSCore.Class (class_definition);
var ctx = frame.get_javascript_context_for_script_world (world);
var obj = ((JSCore.GlobalContext) ctx).get_global_object ();
obj.set_property ((JSCore.GlobalContext) ctx,
str,
obj,
JSCore.PropertyAttribute.None,
null);
((JSCore.Object) obj).dump ((JSCore.Context) ctx, stdout);
}
public void add_div (string color) {
int x = Random.int_range (0, 300),
y = Random.int_range (0, 300);
count += 1;
WebKit.DOM.Document document = page.get_dom_document ();
try {
WebKit.DOM.Element el = document.create_element ("div");
el.append_child (document.create_text_node (@"$count"));
el.set_attribute ("style", @"background: $color; left: $x; top: $y;");
el.set_attribute ("id", @"$count");
((WebKit.DOM.EventTarget) el).add_event_listener_with_closure (
"click", div_clicked_cb, false);
document.body.insert_before (el, null);
} catch (Error error) {
warning ("DOM Error: %s", error.message);
}
}
[DBus (visible = false)]
public void div_clicked_cb (WebKit.DOM.EventTarget target, WebKit.DOM.Event event) {
div_clicked (((WebKit.DOM.Element) target).get_attribute ("id"));
}
[DBus (visible = false)]
private static JSCore.Object class_constructor_cb (JSCore.Context ctx,
JSCore.Object constructor,
JSCore.Value[] arguments,
out JSCore.Value exception) {
message ("Inititialize JS measurement class");
var c = new JSCore.Class (class_definition);
var o = new JSCore.Object (ctx, c, null);
// Do something with the object
return o;
}
[DBus (visible = false)]
private JSCore.Value meas_get_cb (JSCore.Context ctx,
JSCore.Object function,
JSCore.Object thisObject,
JSCore.ConstValue[] arguments,
out JSCore.Value exception) {
debug ("Get JS call");
var value = 1.0;
return (new JSCore.Value.number (ctx, value));
}
}
[DBus (name = "org.opendcs.Dcs.Extension")]
public errordomain DOMError {
ERROR
}
[CCode (cname = "G_MODULE_EXPORT webkit_web_extension_initialize", instance_pos = -1)]
public static void webkit_web_extension_initialize (WebKit.WebExtension extension) {
Dcs.UI.Extension.DOM dom = new Dcs.UI.Extension.DOM ();
WebKit.ScriptWorld world = WebKit.ScriptWorld.get_default ();
extension.page_created.connect (dom.page_created_cb);
world.window_object_cleared.connect (dom.window_object_cleared_cb);
Bus.own_name (BusType.SESSION, "org.opendcs.Dcs.Extension", BusNameOwnerFlags.NONE,
dom.bus_acquired_cb, null,
() => { warning ("Could not acquired bus name"); });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment