@@ -12,6 +12,7 @@ import Cocoa
1212import WebKit
1313import Foundation
1414import AppKit
15+ import AudioToolbox
1516
1617class ViewController: NSViewController , WebFrameLoadDelegate , WebUIDelegate {
1718
@@ -220,6 +221,17 @@ class ViewController: NSViewController, WebFrameLoadDelegate, WebUIDelegate {
220221
221222 // _blank internal
222223 jsContext. objectForKeyedSubscript ( " app " ) . setObject ( unsafeBitCast ( openNow, AnyObject . self) , forKeyedSubscript: " openInternal " )
224+
225+ // Add Battery!
226+ // Not fully working right now.
227+ jsContext. evaluateScript ( " navigator.battery = {charging: true, chargingTime:0, dischargingTime:999, level:1, addEventListener:function(val, cal){}} " )
228+ jsContext. evaluateScript ( " navigator.getBattery = function() { return {charging: true, chargingTime:0, dischargingTime:999, level:1, addEventListener:function(val, cal){}, then:function(call){return call(navigator.battery)}}} " )
229+
230+ //navigator.vibrate
231+ let vibrateNow : @convention ( block) ( NSString ! ) -> Void = { ( data: NSString!) in
232+ self . flashScreen ( data)
233+ }
234+ jsContext. objectForKeyedSubscript ( " navigator " ) . setObject ( unsafeBitCast ( vibrateNow, AnyObject . self) , forKeyedSubscript: " vibrate " )
223235 }
224236
225237
@@ -258,4 +270,23 @@ class ViewController: NSViewController, WebFrameLoadDelegate, WebUIDelegate {
258270
259271 NSApplication . sharedApplication ( ) . dockTile. badgeLabel = String ( notificationCount)
260272 }
273+
274+ func flashScreen ( data: NSString ) {
275+ print ( data)
276+ if ( ( Int ( data as String ) ) != nil || data. isEqualToString ( " undefined " ) ) {
277+ AudioServicesPlaySystemSound ( kSystemSoundID_FlashScreen) ;
278+ } else {
279+ // Flash multiple times..
280+ let time : NSArray = ( data as String ) . componentsSeparatedByString ( " , " )
281+ for( var i = 0 ; i < time . count ; i++ ) {
282+ var timeAsInt = NSNumberFormatter( ) . numberFromString ( time[ i ] as! String)
283+ timeAsInt = Int( timeAsInt ! ) / 100
284+ NSTimer. scheduledTimerWithTimeInterval( NSTimeInterval ( timeAsInt!) , target: self , selector: Selector ( " flashScreenNow " ) , userInfo: nil , repeats: false )
285+ }
286+ }
287+ }
288+
289+ func flashScreenNow( ) {
290+ AudioServicesPlaySystemSound ( kSystemSoundID_FlashScreen) ;
291+ }
261292}
0 commit comments