-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.swift
More file actions
90 lines (71 loc) · 3.26 KB
/
ViewController.swift
File metadata and controls
90 lines (71 loc) · 3.26 KB
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
//
// ViewController.swift
// AOS Order Checker
//
// Created by Wai Man Chan on 5/12/15.
//
//
import Cocoa
import WebKit
class ViewController: NSViewController {
@IBOutlet var webView: WebView?
var oldResult: [String] = []
var resultInit = false
override func webView(sender: WebView!, didFinishLoadForFrame frame: WebFrame!) {
let addrJS = "document.location.href"
let addr = sender.stringByEvaluatingJavaScriptFromString(addrJS)
if ((addr as NSString).containsString("sign_in")) {
//Not login yet
} else if (addr == "https://secure2.store.apple.com/us/signout") {
self.webView?.mainFrameURL = "https://secure2.store.apple.com/us/order/list?hist=90"
} else {
let checker_JS = "document.getElementsByClassName(\"sb-heading\").length"
let result = sender.stringByEvaluatingJavaScriptFromString(checker_JS)
if (result == "0") {
//No order
let alert = NSAlert()
alert.messageText = "No order found"
alert.informativeText = "I can't find any order in this account. Please try log into another account"
alert.runModal()
//Logout first
self.webView?.mainFrameURL = "https://secure2.store.apple.com/us/signout"
usleep(1000*1000)
} else {
//Login with orders
let numberOfOrder = (result as NSString).integerValue
var i = 0
var newResult = [String](count: numberOfOrder, repeatedValue: "")
for(; i < numberOfOrder; i++) {
let fetcher = "document.getElementsByClassName(\"sb-heading\")["+String(i)+"].getElementsByTagName(\"h3\")[0].getElementsByTagName(\"span\")[0].innerHTML"
newResult[i] = sender.stringByEvaluatingJavaScriptFromString(fetcher)
}
//Check orders status
if (resultInit && oldResult != newResult) {
//Not equal
let notification = NSUserNotification()
notification.title = "Order status change"
notification.subtitle = "Your order status has changed"
NSUserNotificationCenter.defaultUserNotificationCenter().removeAllDeliveredNotifications()
NSUserNotificationCenter.defaultUserNotificationCenter().deliverNotification(notification)
}
oldResult = newResult
resultInit = true
//Set refresh timer
NSTimer.scheduledTimerWithTimeInterval(30, target: self, selector: "refresh:", userInfo: nil, repeats: false)
}
}
}
func refresh(timer: NSTimer) {
webView?.mainFrame.reload()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.webView?.mainFrameURL = "https://secure2.store.apple.com/us/order/list?hist=90"
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
}