@@ -13,16 +13,17 @@ import Foundation
1313import AppKit
1414import AudioToolbox
1515import IOKit. ps
16+ import Darwin
1617
17- class ViewController : NSViewController , WebFrameLoadDelegate , WebUIDelegate {
18+ class ViewController : NSViewController , WebFrameLoadDelegate , WebUIDelegate , WebResourceLoadDelegate , WebPolicyDelegate {
1819
1920 @IBOutlet var mainWindow : NSView !
2021 @IBOutlet weak var mainWebview : WebView !
2122 @IBOutlet weak var loadingBar : NSProgressIndicator !
2223 @IBOutlet weak var launchingLabel : NSTextField !
2324
2425 // TODO: configure your app here
25- let SETTINGS : [ String : Any ] = [
26+ var SETTINGS : [ String : Any ] = [
2627
2728 // Url to browse to.
2829 " url " : " https://www.google.com " ,
@@ -45,7 +46,12 @@ class ViewController: NSViewController, WebFrameLoadDelegate, WebUIDelegate {
4546 // Do you want a loading bar?
4647 " showLoadingBar " : true ,
4748
48- " consoleSupport " : false
49+ // Add console.log support?
50+ " consoleSupport " : false ,
51+
52+ // run the app in debug mode?
53+ // will be overridden by xCode (runs with -NSDocumentRevisionsDebugMode YES)
54+ " debugmode " : false ,
4955 ]
5056
5157 func webView( sender: WebView ! , runJavaScriptAlertPanelWithMessage message: String ! , initiatedByFrame frame: WebFrame ! ) {
@@ -59,6 +65,7 @@ class ViewController: NSViewController, WebFrameLoadDelegate, WebUIDelegate {
5965
6066 var firstLoadingStarted = false
6167 var firstAppear = true
68+ var notificationCount = 0
6269
6370 override func viewDidAppear( ) {
6471 if ( firstAppear) {
@@ -70,15 +77,19 @@ class ViewController: NSViewController, WebFrameLoadDelegate, WebUIDelegate {
7077 super. viewDidLoad ( )
7178
7279 mainWebview. UIDelegate = self
80+ mainWebview. resourceLoadDelegate = self
7381
74- addObservers ( )
82+ checkSettings ( )
7583
84+ addObservers ( )
85+
7686 initSettings ( )
7787
7888 goHome ( )
89+
7990 }
8091
81- func addObservers( ) {
92+ func addObservers( ) {
8293 // add menu action observers
8394 let observers = [ " goHome " , " reload " , " copyUrl " , " clearNotificationCount " ]
8495
@@ -87,24 +98,24 @@ class ViewController: NSViewController, WebFrameLoadDelegate, WebUIDelegate {
8798 }
8899 }
89100
90- func goHome( ) {
101+ func goHome( ) {
91102 loadUrl ( ( SETTINGS [ " url " ] as? String ) !)
92103 }
93104
94- func reload( ) {
105+ func reload ( ) {
95106 let currentUrl : String = ( mainWebview. mainFrame. dataSource? . request. URL? . absoluteString) !
96107 loadUrl ( currentUrl)
97108 }
98109
99- func copyUrl( ) {
110+ func copyUrl( ) {
100111 let currentUrl : String = ( mainWebview. mainFrame. dataSource? . request. URL? . absoluteString) !
101112 let clipboard : NSPasteboard = NSPasteboard . generalPasteboard ( )
102113 clipboard. clearContents ( )
103114
104115 clipboard. setString ( currentUrl, forType: NSStringPboardType)
105116 }
106117
107- func initSettings( ) {
118+ func initSettings( ) {
108119 // controll the progress bar
109120 if ( !( SETTINGS [ " showLoadingBar " ] as? Bool ) !) {
110121 loadingBar. hidden = true
@@ -114,8 +125,7 @@ class ViewController: NSViewController, WebFrameLoadDelegate, WebUIDelegate {
114125 launchingLabel. stringValue = ( SETTINGS [ " launchingText " ] as? String ) !
115126 }
116127
117- func initWindow( ) {
118-
128+ func initWindow( ) {
119129 firstAppear = false
120130
121131 // set window size
@@ -150,7 +160,7 @@ class ViewController: NSViewController, WebFrameLoadDelegate, WebUIDelegate {
150160 mainWebview. preferences. plugInsEnabled = true
151161 }
152162
153- func loadUrl( url: String ) {
163+ func loadUrl( url: String ) {
154164 loadingBar. stopAnimation ( self )
155165
156166 let URL = NSURL ( string: url)
@@ -289,10 +299,121 @@ class ViewController: NSViewController, WebFrameLoadDelegate, WebUIDelegate {
289299 jsContext. objectForKeyedSubscript ( " navigator " ) . setObject ( unsafeBitCast ( vibrateNow, AnyObject . self) , forKeyedSubscript: " vibrate " )
290300 }
291301
302+ // Quit the app (there must be a better way)
303+ func Quit( sender: AnyObject ) {
304+ exit ( 0 )
305+ }
306+
307+ // Edit contextmenu...
308+ func webView( sender: WebView ! , contextMenuItemsForElement element: [ NSObject : AnyObject ] ! , var defaultMenuItems: [ AnyObject ] ! ) -> [ AnyObject ] ! {
309+ // Add debug menu. (if enabled)
310+ if ( SETTINGS [ " debugmode " ] as! Bool ) {
311+ let debugMenu = NSMenu ( title: " Debug " )
312+ debugMenu. addItem ( NSMenuItem . init ( title: " Open New window " , action: Selector ( " _debugNewWindow: " ) , keyEquivalent: " " ) )
313+ debugMenu. addItem ( NSMenuItem . init ( title: " Print arguments " , action: Selector ( " _debugDumpArguments: " ) , keyEquivalent: " " ) )
314+
315+ let item = NSMenuItem . init ( title: " Debug " , action: Selector ( " _doNothing: " ) , keyEquivalent: " " )
316+ item. submenu = debugMenu
317+
318+ defaultMenuItems. append ( item)
319+ }
320+
321+ defaultMenuItems. append ( NSMenuItem . separatorItem ( ) )
322+ defaultMenuItems. append ( NSMenuItem . init ( title: " Quit " , action: Selector ( " Quit: " ) , keyEquivalent: " " ) )
323+ return defaultMenuItems
324+ }
292325
293- var notificationCount = 0
326+ // Debug: doNothing
327+ func _doNothing( Sender: AnyObject ) {
328+ // _doNothing
329+ }
294330
295- func clearNotificationCount( ) {
331+ // Debug: Open new window
332+ func _debugNewWindow( Sender: AnyObject ) {
333+ openNewWindow ( " https://www.google.nl/search?client=safari&rls=en&q=new+window&ie=UTF-8&oe=UTF-8&gws_rd=cr&ei=_8eKVs2WFIbFPd7Sr_gN " , height: " 0 " , width: " 0 " )
334+ }
335+
336+ // Debug: Print arguments
337+ func _debugDumpArguments( Sender: AnyObject ) {
338+ print ( Process . arguments)
339+ }
340+
341+ // Function to call for the window.open (popup)
342+ func openNewWindow( url: String , height: String , width: String ) {
343+ dispatch_async ( dispatch_get_global_queue ( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ) , { ( ) -> Void in
344+ dispatch_async ( dispatch_get_main_queue ( ) , { ( ) -> Void in
345+
346+ // TODO: This one freezes our main window.
347+ // TODO: even in the qeue
348+ // TODO: Hide this window
349+
350+ let task = NSTask ( )
351+ task. launchPath = Process . arguments [ 0 ]
352+
353+ if ( self . SETTINGS [ " debugmode " ] as! Bool ) {
354+ // With debug mode
355+ task. arguments = [ " -NSDocumentRevisionsDebugMode " , " YES " , " -url " , url, " -height " , height, " -width " , width]
356+ } else {
357+ // Production mode
358+ task. arguments = [ " -url " , url, " -height " , height, " -width " , width]
359+ }
360+
361+ print ( " Running: \( Process . arguments [ 0 ] ) -url \" \( url) \" second-argument " )
362+
363+ let pipe = NSPipe ( )
364+ task. standardOutput = pipe
365+ task. launch ( )
366+
367+ let data = pipe. fileHandleForReading. readDataToEndOfFile ( )
368+
369+ let output : String = String ( data: data, encoding: NSUTF8StringEncoding) !
370+ print ( output)
371+ } )
372+ } )
373+ }
374+
375+ // @wdg Override settings via commandline
376+ // .... Used for popups, and debug options.
377+ func checkSettings( ) {
378+ // Need to overwrite settings?
379+ if ( Process . argc > 0 ) {
380+ for (var i= 1 ; i< Int ( Process . argc ) ; i= i+ 2 ) {
381+
382+ if ( ( String( Process . arguments [ i ] ) ) == " -NSDocumentRevisionsDebugMode " ) {
383+ if ( ( Strin g ( Process . arguments [ i+ 1 ] ) ) == " YES " ) {
384+ SETTINGS [ " debugmode " ] = true
385+ SETTINGS [ " consoleSupport " ] = true
386+ }
387+ }
388+ if ( ( String ( Process . arguments [ i] ) ) . uppercaseString == " -DEBUG " ) {
389+ if ( ( String ( Process . arguments [ i+ 1 ] ) ) . uppercaseString == " YES " || ( String ( Process . arguments [ i+ 1 ] ) ) . uppercaseString == " true " ) {
390+ SETTINGS [ " debugmode " ] = true
391+ SETTINGS [ " consoleSupport " ] = true
392+ }
393+ }
394+
395+ if ( ( String ( Process . arguments [ i] ) ) == " -dump-args " ) {
396+ self . _debugDumpArguments ( " " )
397+ }
398+
399+ if ( ( String ( Process . arguments [ i] ) ) == " -url " ) {
400+ SETTINGS [ " url " ] = String ( Process . arguments [ i+ 1 ] )
401+ }
402+
403+ if ( ( String ( Process . arguments [ i] ) ) == " -height " ) {
404+ SETTINGS [ " initialWindowHeight " ] = ( Int ( Process . arguments [ i+ 1 ] ) > 30 ) ? Int ( Process . arguments [ i+ 1 ] ) : Int ( 30 )
405+ }
406+
407+ if ( ( String ( Process . arguments [ i] ) ) == " -width " ) {
408+ SETTINGS [ " initialWindowWidth " ] = ( Int ( Process . arguments [ i+ 1 ] ) > 30 ) ? Int ( Process . arguments [ i+ 1 ] ) : Int ( 30 )
409+ }
410+ }
411+ }
412+
413+ initWindow ( )
414+ }
415+
416+ func clearNotificationCount( ) {
296417 notificationCount = 0
297418 }
298419
@@ -331,7 +452,6 @@ class ViewController: NSViewController, WebFrameLoadDelegate, WebUIDelegate {
331452 // @wdg Add Notification Support
332453 // Issue: #2
333454 func flashScreen ( data: NSString ) {
334- print ( data)
335455 if ( ( Int ( data as String ) ) != nil || data. isEqualToString ( " undefined " ) ) {
336456 AudioServicesPlaySystemSound ( kSystemSoundID_FlashScreen) ;
337457 } else {
0 commit comments