Skip to content

Commit a79f49b

Browse files
committed
Safari extension tweaks
1 parent 1c6fed4 commit a79f49b

12 files changed

Lines changed: 109 additions & 30 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,4 @@ jobs:
7676
- uses: maxim-lobanov/setup-xcode@v1
7777
with:
7878
xcode-version: latest
79-
- run: npm run pack:safari
79+
# - run: npm run pack:safari

safari/Config.xcconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
MARKETING_VERSION = 21.10.4
2-
CURRENT_PROJECT_VERSION = 14
1+
MARKETING_VERSION = 21.12.12
2+
CURRENT_PROJECT_VERSION = 16

safari/Refined GitHub.xcodeproj/project.pbxproj

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
E3312B3627691C89006DA596 /* Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = E3312B3527691C89006DA596 /* Utilities.swift */; };
11+
E3312B3727691C89006DA596 /* Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = E3312B3527691C89006DA596 /* Utilities.swift */; };
1012
E35EB4BB26F36E0900DDEA60 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E35EB4BA26F36E0900DDEA60 /* AppDelegate.swift */; };
1113
E35EB4BD26F36E0900DDEA60 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E35EB4BC26F36E0900DDEA60 /* SceneDelegate.swift */; };
1214
E35EB4C026F36E0900DDEA60 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = E35EB4BE26F36E0900DDEA60 /* LaunchScreen.storyboard */; };
@@ -74,6 +76,7 @@
7476
/* End PBXCopyFilesBuildPhase section */
7577

7678
/* Begin PBXFileReference section */
79+
E3312B3527691C89006DA596 /* Utilities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utilities.swift; sourceTree = "<group>"; };
7780
E33F1EF826F380AF00ACEB0F /* Config.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Config.xcconfig; sourceTree = "<group>"; };
7881
E35EB4AB26F36E0800DDEA60 /* Base */ = {isa = PBXFileReference; lastKnownFileType = text.html; name = Base; path = ../Base.lproj/Main.html; sourceTree = "<group>"; };
7982
E35EB4AC26F36E0800DDEA60 /* Icon.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Icon.png; sourceTree = "<group>"; };
@@ -150,6 +153,7 @@
150153
isa = PBXGroup;
151154
children = (
152155
E35EB4AF26F36E0800DDEA60 /* ViewController.swift */,
156+
E3312B3527691C89006DA596 /* Utilities.swift */,
153157
E35EB4B026F36E0900DDEA60 /* Assets.xcassets */,
154158
E35EB4A926F36E0800DDEA60 /* Resources */,
155159
);
@@ -320,7 +324,7 @@
320324
attributes = {
321325
BuildIndependentTargetsInParallel = 1;
322326
LastSwiftUpdateCheck = 1300;
323-
LastUpgradeCheck = 1300;
327+
LastUpgradeCheck = 1320;
324328
TargetAttributes = {
325329
E35EB4B626F36E0900DDEA60 = {
326330
CreatedOnToolsVersion = 13.0;
@@ -451,6 +455,7 @@
451455
files = (
452456
E35EB4EF26F36E0900DDEA60 /* ViewController.swift in Sources */,
453457
E35EB4BB26F36E0900DDEA60 /* AppDelegate.swift in Sources */,
458+
E3312B3627691C89006DA596 /* Utilities.swift in Sources */,
454459
E35EB4BD26F36E0900DDEA60 /* SceneDelegate.swift in Sources */,
455460
);
456461
runOnlyForDeploymentPostprocessing = 0;
@@ -459,6 +464,7 @@
459464
isa = PBXSourcesBuildPhase;
460465
buildActionMask = 2147483647;
461466
files = (
467+
E3312B3727691C89006DA596 /* Utilities.swift in Sources */,
462468
E35EB4F026F36E0900DDEA60 /* ViewController.swift in Sources */,
463469
E35EB4CC26F36E0900DDEA60 /* AppDelegate.swift in Sources */,
464470
);
@@ -780,8 +786,9 @@
780786
isa = XCBuildConfiguration;
781787
buildSettings = {
782788
CODE_SIGN_ENTITLEMENTS = "macOS (Extension)/Refined GitHub.entitlements";
789+
CODE_SIGN_IDENTITY = "Apple Development";
783790
CODE_SIGN_STYLE = Automatic;
784-
DEVELOPMENT_TEAM = "";
791+
DEVELOPMENT_TEAM = YG56YK5RN5;
785792
ENABLE_HARDENED_RUNTIME = YES;
786793
GENERATE_INFOPLIST_FILE = YES;
787794
INFOPLIST_FILE = "macOS (Extension)/Info.plist";
@@ -809,8 +816,9 @@
809816
isa = XCBuildConfiguration;
810817
buildSettings = {
811818
CODE_SIGN_ENTITLEMENTS = "macOS (Extension)/Refined GitHub.entitlements";
819+
CODE_SIGN_IDENTITY = "Apple Development";
812820
CODE_SIGN_STYLE = Automatic;
813-
DEVELOPMENT_TEAM = "";
821+
DEVELOPMENT_TEAM = YG56YK5RN5;
814822
ENABLE_HARDENED_RUNTIME = YES;
815823
GENERATE_INFOPLIST_FILE = YES;
816824
INFOPLIST_FILE = "macOS (Extension)/Info.plist";
@@ -841,9 +849,9 @@
841849
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
842850
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
843851
CODE_SIGN_ENTITLEMENTS = "macOS (App)/Refined GitHub.entitlements";
844-
CODE_SIGN_IDENTITY = "-";
852+
CODE_SIGN_IDENTITY = "Apple Development";
845853
CODE_SIGN_STYLE = Automatic;
846-
DEVELOPMENT_TEAM = "";
854+
DEVELOPMENT_TEAM = YG56YK5RN5;
847855
ENABLE_HARDENED_RUNTIME = YES;
848856
GENERATE_INFOPLIST_FILE = YES;
849857
INFOPLIST_FILE = "macOS (App)/Info.plist";

safari/Shared (App)/Base.lproj/Main.html

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,23 @@
88
<script src="../Script.js" defer></script>
99
</head>
1010
<body>
11-
<img src="../Icon.png" width="128" height="128" alt="Refined GitHub Icon">
11+
<img src="../Icon.png" width="64" height="64" alt="Refined GitHub Icon">
1212
<p class="platform-ios">You can turn on Refined GitHub’s Safari extension in Settings › Safari.</p>
13-
<p class="platform-mac state-unknown">You can turn on Refined GitHub’s extension in Safari Extensions preferences.</p>
14-
<p class="platform-mac state-on">Refined GitHub’s extension is currently on. You can turn it off in Safari Extensions preferences.</p>
15-
<p class="platform-mac state-off">Refined GitHub’s extension is currently off. You can turn it on in Safari Extensions preferences.</p>
13+
<p class="platform-mac state-unknown">
14+
<span class="heading"> </span>
15+
<br>
16+
<span class="subheading">You can turn the extension off in Safari Extensions preferences</span>
17+
</p>
18+
<p class="platform-mac state-on">
19+
<span class="heading">The extension is currently on</span>
20+
<br>
21+
<span class="subheading">You can turn it off in Safari Extensions preferences</span>
22+
</p>
23+
<p class="platform-mac state-off">
24+
<span class="heading">The extension is currently off</span>
25+
<br>
26+
<span class="subheading">You can turn it on in Safari Extensions preferences</span>
27+
</p>
1628
<button class="platform-mac open-preferences">Quit and Open Safari Extensions Preferences…</button>
1729
<a href="#" class="platform-mac open-help">Does the extension not show up?</a>
1830
</body>

safari/Shared (App)/Resources/Style.css

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,25 @@ body.state-off :is(.state-on, .state-unknown) {
5656
display: none;
5757
}
5858

59+
.heading {
60+
font-size: 1.36em;
61+
font-weight: bold;
62+
}
63+
64+
.subheading {
65+
font-size: 10px;
66+
margin-top: 6px;
67+
opacity: 70%;
68+
}
69+
70+
.state-on .heading {
71+
color: #20b83a;
72+
}
73+
74+
.state-off .heading {
75+
color: #ff3b2f;
76+
}
77+
5978
button {
6079
font-size: 1em;
6180
}
@@ -64,4 +83,12 @@ button {
6483
a {
6584
color: rgb(255 255 255 / 0.6);
6685
}
86+
87+
.state-on .heading {
88+
color: #32d74b;
89+
}
90+
91+
.state-off .heading {
92+
color: #ff463a;
93+
}
6794
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Foundation
2+
import WebKit
3+
4+
extension WKWebView {
5+
/**
6+
Whether the web view should have a background. Set to `false` to make it transparent.
7+
*/
8+
var drawsBackground: Bool {
9+
get {
10+
value(forKey: "drawsBackground") as? Bool ?? true
11+
}
12+
set {
13+
setValue(newValue, forKey: "drawsBackground")
14+
}
15+
}
16+
}

safari/Shared (App)/ViewController.swift

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ typealias XViewController = NSViewController
1111

1212
let extensionBundleIdentifier = "com.sindresorhus.Refined-GitHub.Extension"
1313

14+
@MainActor
1415
final class ViewController: XViewController, WKNavigationDelegate, WKScriptMessageHandler {
1516
@IBOutlet var webView: WKWebView!
1617

1718
override func viewDidLoad() {
1819
super.viewDidLoad()
1920

2021
webView.navigationDelegate = self
22+
webView.drawsBackground = false
2123

2224
#if os(iOS)
2325
webView.scrollView.isScrollEnabled = false
@@ -26,24 +28,32 @@ final class ViewController: XViewController, WKNavigationDelegate, WKScriptMessa
2628
webView.configuration.userContentController.add(self, name: "controller")
2729

2830
webView.loadFileURL(Bundle.main.url(forResource: "Main", withExtension: "html")!, allowingReadAccessTo: Bundle.main.resourceURL!)
31+
32+
DispatchQueue.main.async { [self] in
33+
view.window?.titlebarAppearsTransparent = true
34+
}
2935
}
3036

3137
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
38+
// TODO: Use `callAsyncJavaScript` when targeting macOS 12.
3239
#if os(iOS)
3340
webView.evaluateJavaScript("show('ios')")
3441
#elseif os(macOS)
3542
webView.evaluateJavaScript("show('mac')")
3643

37-
SFSafariExtensionManager.getStateOfSafariExtension(withIdentifier: extensionBundleIdentifier) { (state, error) in
38-
guard let state = state, error == nil else {
39-
DispatchQueue.main.async {
40-
NSApp.presentError(error!)
41-
}
42-
return
43-
}
44+
Task {
45+
do {
46+
let state = try await SFSafariExtensionManager.stateOfSafariExtension(withIdentifier: extensionBundleIdentifier)
4447

45-
DispatchQueue.main.async {
46-
webView.evaluateJavaScript("show('mac', \(state.isEnabled)")
48+
if #available(macOS 12, iOS 15, *) {
49+
_ = try await webView.callAsyncJavaScript("show('mac', isEnabled)", arguments: ["isEnabled": state.isEnabled], contentWorld: .page)
50+
} else {
51+
_ = try await webView.evaluateJavaScript("show('mac', \(state.isEnabled)); 0") // The `0` works around a bug in `evaluateJavaScript`.
52+
}
53+
} catch {
54+
_ = await MainActor.run { // Required since `presentError` is not yet annotated with `@MainActor`.
55+
NSApp.presentError(error)
56+
}
4757
}
4858
}
4959
#endif
@@ -64,16 +74,14 @@ final class ViewController: XViewController, WKNavigationDelegate, WKScriptMessa
6474
return;
6575
}
6676

67-
SFSafariApplication.showPreferencesForExtension(withIdentifier: extensionBundleIdentifier) { error in
68-
if let error = error {
69-
DispatchQueue.main.async {
77+
Task {
78+
do {
79+
try await SFSafariApplication.showPreferencesForExtension(withIdentifier: extensionBundleIdentifier)
80+
NSApplication.shared.terminate(nil)
81+
} catch {
82+
_ = await MainActor.run { // Required since `presentError` is not yet annotated with `@MainActor`.
7083
NSApp.presentError(error)
7184
}
72-
return
73-
}
74-
75-
DispatchQueue.main.async {
76-
NSApplication.shared.terminate(nil)
7785
}
7886
}
7987
#endif

safari/iOS (App)/Info.plist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,7 @@
2323
</array>
2424
</dict>
2525
</dict>
26+
<key>ITSAppUsesNonExemptEncryption</key>
27+
<false/>
2628
</dict>
2729
</plist>

safari/iOS (Extension)/Info.plist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@
99
<key>NSExtensionPrincipalClass</key>
1010
<string>$(PRODUCT_MODULE_NAME).SafariWebExtensionHandler</string>
1111
</dict>
12+
<key>ITSAppUsesNonExemptEncryption</key>
13+
<false/>
1214
</dict>
1315
</plist>

safari/macOS (App)/AppDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import Cocoa
33
@main
44
final class AppDelegate: NSObject, NSApplicationDelegate {
55
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
6-
return true
6+
true
77
}
88
}

0 commit comments

Comments
 (0)