Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

A WebKit Plugin Written in MacRuby

By Peter Cooper / January 13, 2011

webkit.pngEloy Duran (of the Dutch Rails consultancy Fingertips) has put together an interesting side project: a WebKit plugin written in MacRuby. His 'MacRubyWebKitPluginExample' project on GitHub is a short, self contained example of how to pull it off, so it's worth checking out if you want to do something similar. Eloy's example simply allows Ruby code to be supplied by a text box in a WebView and then executed by MacRuby on the back end.

Here's a video showing it in action:

Before you get too excited, there are some significant provisos. Eloy explains:

Whoa, hold your horses, son. Because this brings us to the most important note, and will most probably be sad news to you, which is that none of the common WebKit based browsers support this... This means no Safari nor Chrome. [Due to their lack of support for garbage collection.]

The latter could possibly be compiled with garbage collection support, it’s completely open-source after all, but I was not able to do so in my quick attempts. In case you'd like to have a go, I could not get ‘Chromium Framework.framework’ to support it. Let me know if you are successful.

However, it's possible to use the plugin from a Web browser because, well, that's what a WebView is. You can build a simple Web browser

Eloy Duran

So it's early days, but these problems seem surmountable, and in the interim it could be a useful technique for those of you building MacRuby OS X apps with custom WebKit WebViews.

Thanks to Ilya Grigorik for pointing this project out on Twitter.

Comments

  1. Eloy Durán says:

    Thanks for the mention Peter!

    It's worth noting, that the example illustrates how to get custom NSView instances in a WebView. In this case I just used a simple NSTextField, which, in itself, might not look very interesting besides the point that you can do things like eval Ruby code ;)

  2. Peter Cooper says:

    Could that as easily have been a TEXTAREA rigged up through JavaScript to call through to your code? I'm assuming so and that's what smells particularly interesting to me (the idea of extending existing app views with this stuff).

  3. Eloy Durán says:

    Sure thing! See the code of Miner, the app we wrote for the RubyAndRails 2010 hackfest:

    * MacRuby side exposing methods to the JS runtime: https://github.com/Fingertips/miner/blob/master/app/MainWindowController.rb
    * JS side using the exposed MacRuby methods: https://github.com/Fingertips/miner/blob/master/site/public/javascripts/install_gem.js

Other Posts to Enjoy

Twitter Mentions