Skip to content

Commit 1d54acf

Browse files
committed
1.2.0 Added an extension that switches tabs to other profiles
1 parent 720eb79 commit 1d54acf

6 files changed

Lines changed: 285 additions & 7 deletions

File tree

Extension/Extension.entitlements

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>com.apple.security.app-sandbox</key>
6+
<true/>
7+
<key>com.apple.security.files.user-selected.read-only</key>
8+
<true/>
9+
</dict>
10+
</plist>

Extension/Info.plist

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>NSExtension</key>
6+
<dict>
7+
<key>NSExtensionPointIdentifier</key>
8+
<string>com.apple.Safari.extension</string>
9+
<key>NSExtensionPrincipalClass</key>
10+
<string>$(PRODUCT_MODULE_NAME).SafariExtensionHandler</string>
11+
<key>SFSafariContentScript</key>
12+
<array>
13+
<dict>
14+
<key>Script</key>
15+
<string>script.js</string>
16+
</dict>
17+
</array>
18+
<key>SFSafariToolbarItem</key>
19+
<dict>
20+
<key>Action</key>
21+
<string>Command</string>
22+
<key>Identifier</key>
23+
<string>com.andadinosaur.Switcheroo.SwitchButton</string>
24+
<key>Image</key>
25+
<string>Switch.pdf</string>
26+
<key>Label</key>
27+
<string>Switch to Profile</string>
28+
</dict>
29+
<key>SFSafariWebsiteAccess</key>
30+
<dict>
31+
<key>Level</key>
32+
<string>All</string>
33+
</dict>
34+
</dict>
35+
<key>NSHumanReadableDescription</key>
36+
<string>Switch tabs from one Safari profile to another.</string>
37+
</dict>
38+
</plist>

Extension/Resources/Switch.pdf

6.18 KB
Binary file not shown.

Extension/Resources/script.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
(() => {
2+
'use strict';
3+
4+
window.addEventListener('keydown', (e) => {
5+
if (e.target.tagName == 'INPUT' ||
6+
e.target.tagName == 'SELECT' ||
7+
e.target.tagName == 'TEXTAREA' ||
8+
e.target.isContentEditable) {
9+
return;
10+
}
11+
12+
if (!e.altKey || !e.metaKey || e.ctrlKey) return;
13+
14+
if (e.code === 'KeyS') {
15+
e.stopImmediatePropagation();
16+
safari.extension.dispatchMessage('switchProfile');
17+
}
18+
}, true);
19+
})();
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// SafariExtensionHandler.swift
3+
// Extension
4+
//
5+
// Created by Zhenyi Tan on 13/7/25.
6+
//
7+
8+
import SafariServices
9+
10+
11+
class SafariExtensionHandler: SFSafariExtensionHandler {
12+
13+
override func messageReceived(withName messageName: String, from page: SFSafariPage, userInfo: [String : Any]?) {
14+
page.getPropertiesWithCompletionHandler { properties in
15+
if messageName == "switchProfile" {
16+
guard let url = properties?.url else { return }
17+
18+
page.getContainingTab { tab in
19+
NSWorkspace.shared.open(url)
20+
tab.close()
21+
}
22+
}
23+
}
24+
}
25+
26+
override func toolbarItemClicked(in window: SFSafariWindow) {
27+
window.getActiveTab { tab in
28+
guard let tab = tab else { return }
29+
30+
tab.getActivePage { page in
31+
guard let page = page else { return }
32+
33+
page.getPropertiesWithCompletionHandler { properties in
34+
guard let url = properties?.url else { return }
35+
36+
NSWorkspace.shared.open(url)
37+
tab.close()
38+
}
39+
}
40+
}
41+
}
42+
43+
}

0 commit comments

Comments
 (0)