Skip to content

Commit c80fc75

Browse files
committed
Merge pull request #16 from wdg/master
Added support for navigator.vibrate and partial navigator.battery and navigator.getBattery
2 parents 86f290f + 53d60d9 commit c80fc75

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

WebShell/ViewController.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Cocoa
1212
import WebKit
1313
import Foundation
1414
import AppKit
15+
import AudioToolbox
1516

1617
class 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,22 @@ 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+
let time:NSArray = (data as String).componentsSeparatedByString(",")
280+
for(var i = 0; i < time.count; i++) {
281+
var timeAsInt = NSNumberFormatter().numberFromString(time[i] as! String)
282+
timeAsInt = Int(timeAsInt!)/100
283+
NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(timeAsInt!), target: self, selector: Selector("flashScreenNow"), userInfo: nil, repeats: false)
284+
}
285+
}
286+
}
287+
288+
func flashScreenNow() {
289+
AudioServicesPlaySystemSound(kSystemSoundID_FlashScreen);
290+
}
261291
}

0 commit comments

Comments
 (0)