forked from danielbuechele/goofy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppDelegate.swift
283 lines (219 loc) · 10.5 KB
/
AppDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//
// AppDelegate.swift
// Goofy
//
// Created by Daniel Büchele on 11/29/14.
// Copyright (c) 2014 Daniel Büchele. All rights reserved.
//
import Cocoa
import WebKit
import Quartz
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, WKNavigationDelegate, WKUIDelegate, NSWindowDelegate {
@IBOutlet var window : NSWindow!
var webView : WKWebView!
@IBOutlet var view : NSView!
@IBOutlet var loadingView : NSImageView?
@IBOutlet var spinner : NSProgressIndicator!
@IBOutlet var longLoading : NSTextField!
@IBOutlet var reactivationMenuItem : NSMenuItem!
@IBOutlet var statusbarMenuItem : NSMenuItem!
@IBOutlet var toolbarTrenner : NSToolbarItem!
@IBOutlet var toolbarSpacing : NSToolbarItem!
@IBOutlet var toolbar : NSToolbar!
@IBOutlet var titleLabel : TitleLabel!
@IBOutlet var menuHandler : MenuHandler!
var timer : NSTimer!
var activatedFromBackground = false
var isFullscreen = false
var statusBar = NSStatusBar.systemStatusBar()
var statusBarItem : NSStatusItem = NSStatusItem()
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
window.backgroundColor = NSColor.whiteColor()
window.minSize = NSSize(width: 380,height: 376)
window.makeMainWindow()
window.makeKeyWindow()
window.titlebarAppearsTransparent = true
window.titleVisibility = .Hidden
window.delegate = self
loadingView?.layer?.backgroundColor = NSColor.whiteColor().CGColor
sizeWindow(window)
startLoading()
#if DEBUG
let path = NSBundle.mainBundle().objectForInfoDictionaryKey("PROJECT_DIR") as! String
let source = (try! String(contentsOfFile: path+"/server/dist/fb.js", encoding: NSUTF8StringEncoding))+"init();"
#else
let version = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
var jsurl = "https://dani.taurus.uberspace.de/goofyapp/fb" + version + ".js"
if (NSBundle.mainBundle().objectForInfoDictionaryKey("GoofyJavaScriptURL") != nil) {
jsurl = NSBundle.mainBundle().objectForInfoDictionaryKey("GoofyJavaScriptURL") as! String
}
let source = "function getScript(url,success){ var script = document.createElement('script'); script.src = url; var head = document.getElementsByTagName('head')[0], done=false; script.onload = script.onreadystatechange = function(){ if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) { done=true; success(); script.onload = script.onreadystatechange = null; head.removeChild(script); } }; head.appendChild(script); }" +
"getScript('" + jsurl + "', function() {init();});"
#endif
let reactivationToggle : Bool? = NSUserDefaults.standardUserDefaults().objectForKey("reactivationToggle") as? Bool
if (reactivationToggle != nil && reactivationToggle==true) {
self.reactivationMenuItem.state = 1
}
let userScript = WKUserScript(source: source, injectionTime: .AtDocumentEnd, forMainFrameOnly: true)
let reactDevTools = WKUserScript(source: "Object.defineProperty(window, '__REACT_DEVTOOLS_GLOBAL_HOOK__', {value: {inject: function(runtime) {this._reactRuntime = runtime; },getSelectedInstance: null,Overlay: null}});", injectionTime: .AtDocumentStart, forMainFrameOnly: true)
let userContentController = WKUserContentController()
userContentController.addUserScript(userScript)
userContentController.addUserScript(reactDevTools)
let handler = NotificationScriptMessageHandler()
userContentController.addScriptMessageHandler(handler, name: "notification")
let configuration = WKWebViewConfiguration()
configuration.userContentController = userContentController
webView = WKWebView(frame: self.view.bounds, configuration: configuration)
webView.configuration.preferences.setValue(true, forKey: "developerExtrasEnabled")
webView.navigationDelegate = self
webView.UIDelegate = self
webView.setValue(true, forKey: "drawsTransparentBackground")
// Layout
view.addSubview(webView, positioned: NSWindowOrderingMode.Below, relativeTo: view);
webView.autoresizingMask = [NSAutoresizingMaskOptions.ViewWidthSizable, NSAutoresizingMaskOptions.ViewHeightSizable]
var s = NSProcessInfo.processInfo().arguments[0].componentsSeparatedByString("/")
var st: String = s[s.count-4]
let url : String = "https://messenger.com/login"
let req = NSMutableURLRequest(URL: NSURL(string: url)!)
webView.loadRequest(req);
}
func windowDidResize(notification: NSNotification) {
sizeWindow(notification.object as! NSWindow)
}
func sizeWindow(window: NSWindow) {
if window.frame.width > 640.0 && !self.isFullscreen {
toolbarTrenner.minSize = NSSize(width: 1, height: 100)
toolbarTrenner.maxSize = NSSize(width: 1, height: 100)
toolbarTrenner.view?.frame = CGRectMake(0, 0, 1, 100)
toolbarTrenner.view?.layer?.backgroundColor = NSColor(white: 0.9, alpha: 1.0).CGColor
toolbarSpacing.minSize = NSSize(width: 157, height: 100)
toolbarSpacing.maxSize = NSSize(width: 157, height: 100)
toolbarSpacing.view = NSView(frame: CGRectMake(0, 0, 157, 100))
} else {
toolbarTrenner.view?.layer?.backgroundColor = NSColor(white: 1.0, alpha: 0.0).CGColor
toolbarSpacing.minSize = NSSize(width: 0, height: 100)
toolbarSpacing.maxSize = NSSize(width: 0, height: 100)
}
titleLabel.windowDidResize()
}
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: ((WKNavigationActionPolicy) -> Void)) {
if let url = navigationAction.request.URL {
if let host = url.host {
let inApp = url.host!.hasSuffix("messenger.com") && !url.path!.hasPrefix("/l.php");
let isLogin = url.host!.hasSuffix("facebook.com") && (url.path!.hasPrefix("/login") || url.path!.hasPrefix("/checkpoint"));
if inApp || isLogin {
decisionHandler(.Allow)
} else {
NSWorkspace.sharedWorkspace().openURL(navigationAction.request.URL!)
decisionHandler(.Cancel)
}
} else {
decisionHandler(.Cancel)
}
} else {
decisionHandler(.Cancel)
}
}
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: Selector("endLoading"), userInfo: nil, repeats: false)
if webView.URL!.absoluteString.rangeOfString("messenger.com/login") == nil{
showMenuBar()
}
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
func applicationDidBecomeActive(aNotification: NSNotification) {
NSApplication.sharedApplication().dockTile.badgeLabel = ""
if (self.activatedFromBackground) {
if (self.reactivationMenuItem.state == 1) {
webView.evaluateJavaScript("reactivation()", completionHandler: nil);
}
} else {
self.activatedFromBackground = true;
}
reopenWindow(self)
}
func changeDockIcon() {
NSApplication.sharedApplication().applicationIconImage = NSImage(named: "Image")
}
func applicationShouldOpenUntitledFile(sender: NSApplication) -> Bool {
return true
}
func applicationOpenUntitledFile(sender: NSApplication) -> Bool {
reopenWindow(self)
return true
}
func windowDidEnterFullScreen(notification: NSNotification) {
isFullscreen = true
sizeWindow(window)
}
func windowDidExitFullScreen(notification: NSNotification) {
isFullscreen = false
sizeWindow(window)
}
@IBAction func reopenWindow(sender: AnyObject) {
window.makeKeyAndOrderFront(self)
}
@IBAction func toggleReactivation(sender: AnyObject) {
let i : NSMenuItem = sender as! NSMenuItem
if (i.state == 0) {
i.state = NSOnState
NSUserDefaults.standardUserDefaults().setObject(true, forKey: "reactivationToggle")
} else {
i.state = NSOffState
NSUserDefaults.standardUserDefaults().setObject(false, forKey: "reactivationToggle")
}
NSUserDefaults.standardUserDefaults().synchronize()
}
var quicklookMediaURL: NSURL? {
didSet {
if quicklookMediaURL != nil {
QLPreviewPanel.sharedPreviewPanel().makeKeyAndOrderFront(nil);
}
}
}
func endLoading() {
timer.invalidate()
loadingView?.hidden = true
spinner.stopAnimation(self)
spinner.hidden = true
longLoading.hidden = true
sizeWindow(window)
}
func showMenuBar() {
for item in toolbar.items {
let i = item
i.view?.hidden = false
i.image = NSImage(named: i.label)
}
sizeWindow(window)
}
func hideMenuBar() {
for item in toolbar.items {
let i = item
i.view?.hidden = true
i.image = NSImage(named: "White")
}
}
func startLoading() {
loadingView?.hidden = false
spinner.startAnimation(self)
spinner.hidden = false
longLoading.hidden = true
timer = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: Selector("longLoadingMessage"), userInfo: nil, repeats: false)
hideMenuBar()
}
func longLoadingMessage() {
if (loadingView?.hidden == false) {
longLoading.hidden = false
}
}
func statusBarItemClicked() {
webView.evaluateJavaScript("reactivation()", completionHandler: nil);
reopenWindow(self)
NSApp.activateIgnoringOtherApps(true)
}
}