This repository has been archived by the owner on Jan 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
SafariExtensionHandler.swift
95 lines (84 loc) · 3.11 KB
/
SafariExtensionHandler.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
//
// SafariExtensionHandler.swift
// SafariExtension
//
// Created by Sahakyan on 8/6/18.
// Copyright © 2018 Ghostery. All rights reserved.
//
import SafariServices
class SafariExtensionHandler: SFSafariExtensionHandler {
override func messageReceived(withName messageName: String, from page: SFSafariPage, userInfo: [String : Any]?) {
// This method will be called when a content script provided by your extension calls safari.extension.dispatchMessage("message").
/*
page.getPropertiesWithCompletionHandler { properties in
NSLog("The extension received a message (\(messageName)) from a script injected into (\(String(describing: properties?.url))) with userInfo (\(userInfo ?? [:]))")
}
*/
if messageName == "recordPageInfo" {
page.getPropertiesWithCompletionHandler { (properties) in
if let ui = userInfo,
let latency = ui["latency"] as? String,
let url = ui["domain"] as? String,
url == (properties?.url?.absoluteString ?? "") {
PageLatencyDataSource.shared.pageLoaded(url: url, latency: latency)
SafariExtensionViewController.shared.updatePageLatency(url, latency)
}
}
}
}
override func toolbarItemClicked(in window: SFSafariWindow) {
// This method will be called when your toolbar item is clicked.
NSLog("The extension's toolbar item was clicked")
}
override func validateToolbarItem(in window: SFSafariWindow, validationHandler: @escaping ((Bool, String) -> Void)) {
TelemetryManager.shared.sendSignal(.active)
// This is called when Safari's state changed in some way that would require the extension's toolbar item to be validated again.
handleTabUrlChange(window) { (url) in
let d = UserDefaults(suiteName: Constants.AppsGroupID)
d?.set(url?.normalizedHost ?? "", forKey: "newDomain")
d?.synchronize()
}
validationHandler(true, "")
}
override func popoverViewController() -> SFSafariExtensionViewController {
return SafariExtensionViewController.shared
}
override func popoverWillShow(in window: SFSafariWindow) {
self.updatePopoverUrl(window)
}
/*
private func urlChanges(window: SFSafariWindow) {
window.getActiveTab { (tab) in
if let t = tab {
self.getTabURL(t)
} else {
AntiTrackingManager.shared.domainChanged(nil)
}
}
}
*/
private func updatePopoverUrl(_ window: SFSafariWindow) {
handleTabUrlChange(window) { (url) in
SafariExtensionViewController.shared.currentUrl = url?.absoluteString ?? ""
SafariExtensionViewController.shared.currentDomain = url?.normalizedHost ?? ""
}
}
private func handleTabUrlChange(_ window: SFSafariWindow, handler: @escaping((URL?) -> Void)) {
window.getActiveTab { (tab) in
tab?.getActivePage(completionHandler: { (activePage) in
activePage?.getPropertiesWithCompletionHandler({ (properties) in
handler(properties?.url)
})
})
}
}
private func reloadCurrentPage() {
SFSafariApplication.getActiveWindow(completionHandler: { (window) in
window?.getActiveTab(completionHandler: { (tab) in
tab?.getActivePage(completionHandler: { (page) in
page?.reload()
})
})
})
}
}