From 53d804788108b580d071178858bfa38c0e1ca478 Mon Sep 17 00:00:00 2001 From: Wesley de Groot Date: Tue, 12 Apr 2016 22:18:06 +0200 Subject: [PATCH] Added support for custom user agents || fixes #52 --- WebShell/ViewController.swift | 29 ++-- WebShell/WebShellPageActions.swift | 226 +++++++++++++++-------------- WebShell/WebshellViewDid.swift | 3 +- 3 files changed, 140 insertions(+), 118 deletions(-) diff --git a/WebShell/ViewController.swift b/WebShell/ViewController.swift index 8dbdc7b..a57c8b4 100755 --- a/WebShell/ViewController.swift +++ b/WebShell/ViewController.swift @@ -19,7 +19,7 @@ import CoreLocation // @wdg Clean up code base // Issue: #43 class ViewController: NSViewController, WebFrameLoadDelegate, WebUIDelegate, WebResourceLoadDelegate, WebPolicyDelegate, CLLocationManagerDelegate { - + @IBOutlet var mainWindow: NSView! @IBOutlet weak var mainWebview: WebView! @IBOutlet weak var launchingLabel: NSTextField! @@ -28,38 +28,43 @@ class ViewController: NSViewController, WebFrameLoadDelegate, WebUIDelegate, Web var firstAppear = true var notificationCount = 0 let locationManager = CLLocationManager() - + // TODO: configure your app here var SETTINGS: [String: Any] = [ - + // Url to browse to. "url": "https://www.google.com", - + + // set the app title "title": NSBundle.mainBundle().infoDictionary!["CFBundleName"] as! String, - + + // if you want to use the default one then leave it default || default = title/version based on Safari/AppleWebKit (KHTML, like Gecko) + // otherwise change it to a useragent you want. + "useragent": "default", + // Do you want to use the document title? (Default: true) "useDocumentTitle": true, - + // Multilanguage loading text! "launchingText": NSLocalizedString("Launching...", comment: "Launching..."), - + // Note that the window min height is 640 and min width is 1000 by default. You could change it in Main.storyboard "initialWindowHeight": 640, "initialWindowWidth": 1000, - + // Open target=_blank in a new screen? (Default: false) "openInNewScreen": false, - + // Do you want a loading bar? (Default: true) "showLoadingBar": true, - + // Add console.log support? (Default: false) "consoleSupport": false, - + // Does the app needs Location support (Default: false) // note: if true, then WebShell always uses location, whenever it is used or not "needLocation": false, - + // run the app in debug mode? (Default: false) // will be overridden by xCode (runs with -NSDocumentRevisionsDebugMode YES) "debugmode": false diff --git a/WebShell/WebShellPageActions.swift b/WebShell/WebShellPageActions.swift index 10e3bb0..b24506f 100755 --- a/WebShell/WebShellPageActions.swift +++ b/WebShell/WebShellPageActions.swift @@ -10,109 +10,125 @@ import Foundation import AppKit extension ViewController { - func addObservers() { - // add menu action observers - let observers = ["goHome", "reload", "copyUrl", "clearNotificationCount", "printThisPage"] - - for observer in observers { - NSNotificationCenter.defaultCenter().addObserver(self, selector: NSSelectorFromString(observer), name: observer, object: nil) - } - } - - func goHome() { - loadUrl((SETTINGS["url"] as? String)!) - } - - func reload() { - let currentUrl: String = (mainWebview.mainFrame.dataSource?.request.URL?.absoluteString)! - loadUrl(currentUrl) - } - - func copyUrl() { - let currentUrl: String = (mainWebview.mainFrame.dataSource?.request.URL?.absoluteString)! - let clipboard: NSPasteboard = NSPasteboard.generalPasteboard() - clipboard.clearContents() - - clipboard.setString(currentUrl, forType: NSStringPboardType) - } - - func initSettings() { - // controll the progress bar - if (!(SETTINGS["showLoadingBar"] as? Bool)!) { - progressBar.hidden = true // @wdg: Better progress indicator | Issue: #37 - } - - // set launching text - launchingLabel.stringValue = (SETTINGS["launchingText"] as? String)! - } - - func initWindow() { - firstAppear = false - - // set window size - var frame: NSRect = mainWindow.frame - - let WIDTH: CGFloat = CGFloat(SETTINGS["initialWindowWidth"] as! Int), - HEIGHT: CGFloat = CGFloat(SETTINGS["initialWindowHeight"] as! Int) - - frame.size.width = WIDTH - frame.size.height = HEIGHT - - // @wdg Fixed screen position (now it centers) - // Issue: #19 - // Note: do not use HEIGHT, WIDTH for some strange reason the window will be positioned 25px from bottom! - let ScreenHeight: CGFloat = (NSScreen.mainScreen()?.frame.size.width)!, - WindowHeight: CGFloat = CGFloat(SETTINGS["initialWindowWidth"] as! Int), // do not use HEIGHT! - ScreenWidth: CGFloat = (NSScreen.mainScreen()?.frame.size.height)!, - WindowWidth: CGFloat = CGFloat(SETTINGS["initialWindowHeight"] as! Int) // do not use WIDTH! - frame.origin.x = (ScreenHeight / 2 - WindowHeight / 2) - frame.origin.y = (ScreenWidth / 2 - WindowWidth / 2) - - // @froge-xyz Fixed initial window size - // Issue: #1, #45 - mainWindow.window?.setFrame(frame, display: true) - // defims Fixed the initial window size. - mainWindow.frame = frame - - // set window title - mainWindow.window?.title = SETTINGS["title"] as! String - - // Force some preferences before loading... - mainWebview.preferences.javaScriptEnabled = true - mainWebview.preferences.javaScriptCanOpenWindowsAutomatically = true - mainWebview.preferences.plugInsEnabled = true - } - - func loadUrl(url: String) { - if ((SETTINGS["showLoadingBar"] as? Bool)!) { - progressBar.hidden = false - progressBar.startAnimation(self) - progressBar.maxValue = 100; - progressBar.minValue = 1; - progressBar.incrementBy(24) - } - let URL = NSURL(string: url) - mainWebview.mainFrame.loadRequest(NSURLRequest(URL: URL!)) - - // Inject Webhooks - self.injectWebhooks(mainWebview.mainFrame.javaScriptContext) - self.loopThroughiFrames() - } - - // @wdg Add Print Support - // Issue: #39 - func printThisPage(Sender: AnyObject? = "") -> Void { - let url = mainWebview.mainFrame.dataSource?.request?.URL?.absoluteString - - let operation: NSPrintOperation = NSPrintOperation.init(view: mainWebview) - operation.jobTitle = "Printing \(url!)" - - // If want to print landscape - operation.printInfo.orientation = NSPaperOrientation.Landscape - operation.printInfo.scalingFactor = 0.7 - - if operation.runOperation() { - print("Printed?") - } - } + func addObservers() { + // add menu action observers + let observers = ["goHome", "reload", "copyUrl", "clearNotificationCount", "printThisPage"] + + for observer in observers { + NSNotificationCenter.defaultCenter().addObserver(self, selector: NSSelectorFromString(observer), name: observer, object: nil) + } + } + + func goHome() { + loadUrl((SETTINGS["url"] as? String)!) + } + + func reload() { + let currentUrl: String = (mainWebview.mainFrame.dataSource?.request.URL?.absoluteString)! + loadUrl(currentUrl) + } + + func copyUrl() { + let currentUrl: String = (mainWebview.mainFrame.dataSource?.request.URL?.absoluteString)! + let clipboard: NSPasteboard = NSPasteboard.generalPasteboard() + clipboard.clearContents() + + clipboard.setString(currentUrl, forType: NSStringPboardType) + } + + func initSettings() { + // controll the progress bar + if (!(SETTINGS["showLoadingBar"] as? Bool)!) { + progressBar.hidden = true // @wdg: Better progress indicator | Issue: #37 + } + + // @wdg Add Custom useragent support + // Issue: #52 + if (SETTINGS["useragent"] as! String == "default") { + var UA: String = SETTINGS["title"] as! String + UA = UA.stringByAppendingString("/") + UA = UA.stringByAppendingString(NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String) + UA = UA.stringByAppendingString(" based on Safari/AppleWebKit (KHTML, like Gecko)") + + NSUserDefaults.standardUserDefaults().registerDefaults(["UserAgent": UA]) // For iOS + mainWebview.customUserAgent = UA // For Mac OS X + } else { + let UA: String = SETTINGS["useragent"] as! String + NSUserDefaults.standardUserDefaults().registerDefaults(["UserAgent": UA]) // For iOS + mainWebview.customUserAgent = UA // For Mac OS X + } + + // set launching text + launchingLabel.stringValue = (SETTINGS["launchingText"] as? String)! + } + + func initWindow() { + firstAppear = false + + // set window size + var frame: NSRect = mainWindow.frame + + let WIDTH: CGFloat = CGFloat(SETTINGS["initialWindowWidth"] as! Int), + HEIGHT: CGFloat = CGFloat(SETTINGS["initialWindowHeight"] as! Int) + + frame.size.width = WIDTH + frame.size.height = HEIGHT + + // @wdg Fixed screen position (now it centers) + // Issue: #19 + // Note: do not use HEIGHT, WIDTH for some strange reason the window will be positioned 25px from bottom! + let ScreenHeight: CGFloat = (NSScreen.mainScreen()?.frame.size.width)!, + WindowHeight: CGFloat = CGFloat(SETTINGS["initialWindowWidth"] as! Int), // do not use HEIGHT! + ScreenWidth: CGFloat = (NSScreen.mainScreen()?.frame.size.height)!, + WindowWidth: CGFloat = CGFloat(SETTINGS["initialWindowHeight"] as! Int) // do not use WIDTH! + frame.origin.x = (ScreenHeight / 2 - WindowHeight / 2) + frame.origin.y = (ScreenWidth / 2 - WindowWidth / 2) + + // @froge-xyz Fixed initial window size + // Issue: #1, #45 + mainWindow.window?.setFrame(frame, display: true) + // defims Fixed the initial window size. + mainWindow.frame = frame + + // set window title + mainWindow.window?.title = SETTINGS["title"] as! String + + // Force some preferences before loading... + mainWebview.preferences.javaScriptEnabled = true + mainWebview.preferences.javaScriptCanOpenWindowsAutomatically = true + mainWebview.preferences.plugInsEnabled = true + } + + func loadUrl(url: String) { + if ((SETTINGS["showLoadingBar"] as? Bool)!) { + progressBar.hidden = false + progressBar.startAnimation(self) + progressBar.maxValue = 100; + progressBar.minValue = 1; + progressBar.incrementBy(24) + } + let URL = NSURL(string: url) + mainWebview.mainFrame.loadRequest(NSURLRequest(URL: URL!)) + + // Inject Webhooks + self.injectWebhooks(mainWebview.mainFrame.javaScriptContext) + self.loopThroughiFrames() + } + + // @wdg Add Print Support + // Issue: #39 + func printThisPage(Sender: AnyObject? = "") -> Void { + let url = mainWebview.mainFrame.dataSource?.request?.URL?.absoluteString + + let operation: NSPrintOperation = NSPrintOperation.init(view: mainWebview) + operation.jobTitle = "Printing \(url!)" + + // If want to print landscape + operation.printInfo.orientation = NSPaperOrientation.Landscape + operation.printInfo.scalingFactor = 0.7 + + if operation.runOperation() { + print("Printed?") + } + } } \ No newline at end of file diff --git a/WebShell/WebshellViewDid.swift b/WebShell/WebshellViewDid.swift index b3c7561..b6b98f4 100755 --- a/WebShell/WebshellViewDid.swift +++ b/WebShell/WebshellViewDid.swift @@ -49,5 +49,6 @@ extension ViewController { initSettings() goHome() } - + +// println(WebView.stringByEvaluatingJavaScriptFromString("navigator.userAgent")); } \ No newline at end of file