Skip to content

Instantly share code, notes, and snippets.

@AndrewO
Forked from mattetti/macruby_js_bridge.rb
Created August 1, 2011 21:02
Show Gist options
  • Save AndrewO/1118996 to your computer and use it in GitHub Desktop.
Save AndrewO/1118996 to your computer and use it in GitHub Desktop.
Fixed "kCGErrorInvalidConnection: CGSGetCurrentCursorLocation: Invalid connection"; turned off plugins
framework 'Cocoa'
framework 'WebKit'
class Cat
attr_accessor :name, :age
def initialize(name = 'kitty', age=42)
@name = name
@age = age
end
# Make all the Cat's methods available from JS
def self.isSelectorExcludedFromWebScript(sel); false end
end
class Browser
attr_accessor :view, :js_engine
def initialize
@kitty = Cat.new
@view = WebView.alloc.initWithFrame([0, 0, 520, 520])
@window = NSWindow.alloc.initWithContentRect([200, 200, 520, 520],
styleMask:NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask,
backing:NSBackingStoreBuffered,
defer:false)
@window.contentView = view
# Use the screen stylesheet, rather than the print one.
view.mediaStyle = 'screen'
view.customUserAgent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_2; en-us) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10'
# Make sure we don't save any of the prefs that we change.
view.preferences.autosaves = false
# Set some useful options.
view.preferences.shouldPrintBackgrounds = true
view.preferences.javaScriptCanOpenWindowsAutomatically = false
view.preferences.allowsAnimatedImages = false
view.preferences.plugInsEnabled = false
# Make sure we don't get a scroll bar.
view.mainFrame.frameView.allowsScrolling = false
view.frameLoadDelegate = self
end
def fetch
page_url = NSURL.URLWithString('http://jquery.com')
view.mainFrame.loadRequest(NSURLRequest.requestWithURL(page_url))
puts "fetching"
end
def webView(view, didFinishLoadForFrame:frame)
@window.display
@window.orderFrontRegardless
@js_engine = view.windowScriptObject # windowScriptObject
@js_engine.setValue(@kitty, forKey: "animal")
# JIT the method, no colon at the end of the method since the selector doesn't
# take arguments.
@kitty.respondsToSelector("age")
puts "js bridge test: "
# trigger JS from Ruby
@js_engine.evaluateWebScript("$('body').text('phase 1');")
# Evaluate JS from Ruby via the DOM
puts @js_engine.evaluateWebScript('animal.age()')
# Execute Ruby code from the DOM
@js_engine.evaluateWebScript("$('body').text('phase 2 ' + animal.age())")
end
end
NSApplication.sharedApplication
Browser.new.fetch
NSRunLoop.currentRunLoop.runUntilDate(NSDate.distantFuture)
@AndrewO
Copy link
Author

AndrewO commented Aug 1, 2011

Fork of the example code at: http://merbist.com/2010/10/19/macruby-webkit-and-js/

I was getting errors like "kCGErrorInvalidConnection: CGSGetCurrentCursorLocation: Invalid connection". According to this, I needed to call NSApplication.sharedApplication before trying to initialize a window:

http://comments.gmane.org/gmane.comp.lang.ruby.macintosh.devel/3354

Also, some Internet Plugins were trying to load and complaining about GC compatibility, so I added a preference to disable them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment