Skip to content

Commit

Permalink
Added support for custom user agents || fixes #52
Browse files Browse the repository at this point in the history
  • Loading branch information
0xWDG committed Apr 12, 2016
1 parent 17d69d5 commit 53d8047
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 118 deletions.
29 changes: 17 additions & 12 deletions WebShell/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand All @@ -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
Expand Down
226 changes: 121 additions & 105 deletions WebShell/WebShellPageActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?")
}
}
}
3 changes: 2 additions & 1 deletion WebShell/WebshellViewDid.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ extension ViewController {
initSettings()
goHome()
}


// println(WebView.stringByEvaluatingJavaScriptFromString("navigator.userAgent"));
}

0 comments on commit 53d8047

Please sign in to comment.