Skip to content

Safari web extension support #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
dist
# Created by https://www.toptal.com/developers/gitignore/api/node,macos,windows,linux,webstorm,visualstudiocode
# Edit at https://www.toptal.com/developers/gitignore?templates=node,macos,windows,linux,webstorm,visualstudiocode
# Created by https://www.toptal.com/developers/gitignore/api/node,linux,macos,windows,webstorm,visualstudiocode,xcode
# Edit at https://www.toptal.com/developers/gitignore?templates=node,linux,macos,windows,webstorm,visualstudiocode,xcode

### Linux ###
*~
Expand Down Expand Up @@ -219,7 +218,6 @@ dist
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
.idea

# AWS User-specific
.idea/**/aws.xml
Expand Down Expand Up @@ -323,8 +321,6 @@ fabric.properties
# https://plugins.jetbrains.com/plugin/8053-azure-toolkit-for-intellij
.idea/**/azureSettings.xml

.idea/**/*.xml

### Windows ###
# Windows thumbnail cache files
Thumbs.db
Expand All @@ -351,4 +347,21 @@ $RECYCLE.BIN/
# Windows shortcuts
*.lnk

# End of https://www.toptal.com/developers/gitignore/api/node,macos,windows,linux,webstorm,visualstudiocode
### Xcode ###
## User settings
xcuserdata/

## Xcode 8 and earlier
*.xcscmblueprint
*.xccheckout

### Xcode Patch ###
*.xcodeproj/*
!*.xcodeproj/project.pbxproj
!*.xcodeproj/xcshareddata/
!*.xcodeproj/project.xcworkspace/
!*.xcworkspace/contents.xcworkspacedata
/*.gcno
**/xcshareddata/WorkspaceSettings.xcsettings

# End of https://www.toptal.com/developers/gitignore/api/node,linux,macos,windows,webstorm,visualstudiocode,xcode
38 changes: 38 additions & 0 deletions safari/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
## Building

This belongs in `package.json`. Later.

```sh
❯ # `npm run dev` does not work, hot module reloading is broken in Safari

❯ npm run build

❯ xcodebuild -scheme RowsX -configuration Release -project ./safari/RowsX/RowsX.xcodeproj/
```

## Safari extension development for Chrome developers

Official, [long version](https://developer.apple.com/documentation/safariservices/safari_web_extensions/running_your_safari_web_extension#3744467).

### Developer mode toggle on chrome://extensions/

For Safari > 17.0

1. Safari > Settings...
2. `Advanced` tab
3. Check `Show features for web developers` at bottom
4. `Developer` tab
5. Check `Extensions: Allow unsigned extensions` at bottom

### Inspect views service worker

Develop > Web Extension Background Content > RowsX Personal

### Reloading

The `xcodebuild` command reloads the extension automatically.

Indicators that it has reloaded:

- The Inspector window will close
- If you have `RowsX` selected in the Extensions tab of Preferences, it will deselect
13 changes: 13 additions & 0 deletions safari/RowsX/RowsX Extension/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSExtension</key>
<dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.Safari.web-extension</string>
<key>NSExtensionPrincipalClass</key>
<string>$(PRODUCT_MODULE_NAME).SafariWebExtensionHandler</string>
</dict>
</dict>
</plist>
10 changes: 10 additions & 0 deletions safari/RowsX/RowsX Extension/RowsX_Extension.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
</dict>
</plist>
38 changes: 38 additions & 0 deletions safari/RowsX/RowsX Extension/SafariWebExtensionHandler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// SafariWebExtensionHandler.swift
// RowsX Extension
//
// Created by rusty on 4/23/24.
//

import SafariServices
import os.log

class SafariWebExtensionHandler: NSObject, NSExtensionRequestHandling {

func beginRequest(with context: NSExtensionContext) {
let request = context.inputItems.first as? NSExtensionItem

let profile: UUID?
if #available(iOS 17.0, macOS 14.0, *) {
profile = request?.userInfo?[SFExtensionProfileKey] as? UUID
} else {
profile = request?.userInfo?["profile"] as? UUID
}

let message: Any?
if #available(iOS 17.0, macOS 14.0, *) {
message = request?.userInfo?[SFExtensionMessageKey]
} else {
message = request?.userInfo?["message"]
}

os_log(.default, "Received message from browser.runtime.sendNativeMessage: %@ (profile: %@)", String(describing: message), profile?.uuidString ?? "none")

let response = NSExtensionItem()
response.userInfo = [ SFExtensionMessageKey: [ "echo": message ] ]

context.completeRequest(returningItems: [ response ], completionHandler: nil)
}

}
Loading