This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathWebExtensionsDebugMenu.swift
131 lines (100 loc) · 4.07 KB
/
WebExtensionsDebugMenu.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
//
// WebExtensionsDebugMenu.swift
//
// Copyright © 2025 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
@available(macOS 14.4, *)
final class WebExtensionsDebugMenu: NSMenu {
private let webExtensionManager: WebExtensionManaging
private let selectAndLoadMenuItem = NSMenuItem(title: "Install web extension...", action: #selector(WebExtensionsDebugMenu.selectAndLoadWebExtension))
init(webExtensionManager: WebExtensionManaging = WebExtensionManager.shared) {
self.webExtensionManager = webExtensionManager
super.init(title: "")
selectAndLoadMenuItem.target = self
selectAndLoadMenuItem.isEnabled = webExtensionManager.areExtenstionsEnabled
addItems()
}
private func addItems() {
removeAllItems()
addItem(selectAndLoadMenuItem)
if !webExtensionManager.webExtensionPaths.isEmpty {
addItem(.separator())
for webExtensionPath in webExtensionManager.webExtensionPaths {
let name = webExtensionManager.extensionName(from: webExtensionPath)
self.addItem(WebExtensionMenuItem(webExtensionPath: webExtensionPath,
webExtensionName: name))
}
}
}
required init(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func update() {
super.update()
addItems()
selectAndLoadMenuItem.isEnabled = webExtensionManager.areExtenstionsEnabled
}
@objc func selectAndLoadWebExtension() {
let panel = NSOpenPanel(allowedFileTypes: [.directory], directoryURL: .downloadsDirectory)
panel.canChooseFiles = false
panel.canChooseDirectories = true
guard case .OK = panel.runModal(),
let url = panel.url else { return }
webExtensionManager.addExtension(path: url.absoluteString)
NSAlert.extensionAlert().runModal()
}
}
@available(macOS 14.4, *)
final class WebExtensionMenuItem: NSMenuItem {
required init(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
init(webExtensionPath: String, webExtensionName: String?) {
super.init(title: webExtensionName ?? webExtensionPath,
action: nil,
keyEquivalent: "")
submenu = WebExtensionSubMenu(webExtensionPath: webExtensionPath)
}
}
@available(macOS 14.4, *)
final class WebExtensionSubMenu: NSMenu {
private let webExtensionPath: String
private let webExtensionManager: WebExtensionManaging
required init(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
init(webExtensionPath: String, webExtensionManager: WebExtensionManaging = WebExtensionManager.shared) {
self.webExtensionManager = webExtensionManager
self.webExtensionPath = webExtensionPath
super.init(title: "")
buildItems {
NSMenuItem(title: "Remove the extension", action: #selector(unloadWebExtension), target: self)
}
}
@objc func unloadWebExtension() {
webExtensionManager.removeExtension(path: webExtensionPath)
NSAlert.extensionAlert().runModal()
}
}
extension NSAlert {
static func extensionAlert() -> NSAlert {
let alert = NSAlert()
alert.messageText = "Restart required"
alert.informativeText = "Please restart your browser manually to apply changes to extensions."
alert.alertStyle = .informational
alert.addButton(withTitle: UserText.ok)
return alert
}
}