ウェブビューを仕込んだメニューバーアプリのサンプルです。
YouTubeApp
ObjC.import('Cocoa')
ObjC.import('WebKit')
var webViewRect = $.NSMakeRect(0, 0, 1024, 560)
var webViewConf = $.WKWebViewConfiguration.alloc.init
webViewConf.preferences.plugInsEnabled = true
var webView = $.WKWebView.alloc.initWithFrameConfiguration(webViewRect, webViewConf)
var url = $.NSURL.URLWithString('http://www.youtube.com/')
var req = $.NSURLRequest.requestWithURL(url)
webView.loadRequest(req)
var webViewItem = $.NSMenuItem.alloc.init
webViewItem.view = webView
var reloadItem = $.NSMenuItem.alloc.init
reloadItem.title = 'Reload'
var quitItem = $.NSMenuItem.alloc.init
quitItem.title = 'Quit'
var statusMenu = $.NSMenu.alloc.init
statusMenu.addItem(webViewItem)
statusMenu.addItem($.NSMenuItem.separatorItem)
statusMenu.addItem(reloadItem)
statusMenu.addItem($.NSMenuItem.separatorItem)
statusMenu.addItem(quitItem)
ObjC.registerSubclass({
name: 'ActivateStatusItemAction',
methods: {
'popUpMenu:': {
types: ['void', ['id']],
implementation: function(sender) {
if ($.NSApplication.sharedApplication.active) {
statusItem.highlightMode = true
statusItem.popUpStatusItemMenu(statusMenu)
statusItem.highlightMode = false
} else {
$.NSApplication.sharedApplication.activateIgnoringOtherApps(true)
}
}
},
'becomeActive:': {
types: ['void', ['id']],
implementation: function(sender) {
statusItem.button.performClick(this)
}
},
}
})
var statusAction = $.ActivateStatusItemAction.alloc.init
var statusItem = $.NSStatusBar.systemStatusBar.statusItemWithLength($.NSVariableStatusItemLength)
statusItem.title = '📺'
statusItem.target = statusAction
statusItem.action = 'popUpMenu:'
statusItem.highlightMode = false
$.NSNotificationCenter.defaultCenter.addObserverSelectorNameObject(statusAction, 'becomeActive:', $.NSApplicationDidBecomeActiveNotification, $.NSApp)
ObjC.registerSubclass({
name: 'Action',
methods: {
'reload:': {
types: ['void', ['id']],
implementation: function(sender) {
webView.reloadFromOrigin
}
},
'quit:': {
types: ['void', ['id']],
implementation: function(sender) {
$.NSNotificationCenter.defaultCenter.removeObserver(statusAction)
$.NSApp.terminate($())
}
},
}
})
var action = $.Action.alloc.init
reloadItem.target = action
reloadItem.action = 'reload:'
quitItem.target = action
quitItem.action = 'quit:'
ポイントはオブザーバーを使ってクリックのタイミングでアプリを最前面に持ってくるところ。そうしないとウェブビューはキーボード入力を受け付けないので。