From 16bbb3d9c33877688223cacc8ae4333b265608f0 Mon Sep 17 00:00:00 2001 From: ACTCD <101378590+ACTCD@users.noreply.github.com> Date: Sat, 28 Sep 2024 13:21:05 +0800 Subject: [PATCH 001/128] feat: do not open safari when change save location If Safari is not running, do not open it, only send messages to the extension when Safari is running. --- xcode/App-Mac/ViewController.swift | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/xcode/App-Mac/ViewController.swift b/xcode/App-Mac/ViewController.swift index fc7598ee..bf6a4267 100644 --- a/xcode/App-Mac/ViewController.swift +++ b/xcode/App-Mac/ViewController.swift @@ -65,14 +65,16 @@ class ViewController: NSViewController { // update user interface text display self.saveLocation.stringValue = url.absoluteString self.saveLocation.toolTip = url.absoluteString - // notify browser extension of relevant updates - sendExtensionMessage( - name: "SAVE_LOCATION_CHANGED", - userInfo: [ - "saveLocation": url.absoluteString.removingPercentEncoding ?? url.absoluteString, - "returnApp": true - ] - ) + // notify browser extension of relevant updates only when Safari is running + if !NSRunningApplication.runningApplications(withBundleIdentifier: "com.apple.Safari").isEmpty { + sendExtensionMessage( + name: "SAVE_LOCATION_CHANGED", + userInfo: [ + "saveLocation": url.absoluteString.removingPercentEncoding ?? url.absoluteString, + "returnApp": true + ] + ) + } }) } From 5ecb9f83bd0fc043dccacbbb4834c0e5c0400625 Mon Sep 17 00:00:00 2001 From: ACTCD <101378590+ACTCD@users.noreply.github.com> Date: Sat, 28 Sep 2024 14:14:52 +0800 Subject: [PATCH 002/128] build: update xcconfig files require team id --- xcode/xcconfig/Userscripts-Debug.xcconfig | 8 +++++--- xcode/xcconfig/Userscripts-Vite.xcconfig | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/xcode/xcconfig/Userscripts-Debug.xcconfig b/xcode/xcconfig/Userscripts-Debug.xcconfig index f8f993b4..8d2ac4cd 100644 --- a/xcode/xcconfig/Userscripts-Debug.xcconfig +++ b/xcode/xcconfig/Userscripts-Debug.xcconfig @@ -4,8 +4,10 @@ INFOPLIST_KEY_NSHumanReadableCopyright = Copyright © Userscripts CURRENT_PROJECT_VERSION = 1 MARKETING_VERSION = 1.0.0 -// Sign to Run Locally -CODE_SIGN_IDENTITY = - +// Development +CODE_SIGN_IDENTITY = Apple Development +// Developer Team ID +DEVELOPMENT_TEAM = XXXXXXXXXX // Organization Identifier ORG_IDENTIFIER = dev.debug // Application Identifier @@ -14,7 +16,7 @@ APP_IDENTIFIER = $(ORG_IDENTIFIER).userscripts EXT_IDENTIFIER = $(APP_IDENTIFIER).ext.safari // App Groups and Group Containers IOS_SHARED_GID = group.$(ORG_IDENTIFIER).userscripts -MAC_SHARED_GID = 00000.$(ORG_IDENTIFIER).userscripts +MAC_SHARED_GID = $(DEVELOPMENT_TEAM).$(ORG_IDENTIFIER).userscripts // App URL Scheme APP_URL_SCHEME = userscriptsdebug diff --git a/xcode/xcconfig/Userscripts-Vite.xcconfig b/xcode/xcconfig/Userscripts-Vite.xcconfig index 16a4ced3..6224a75b 100644 --- a/xcode/xcconfig/Userscripts-Vite.xcconfig +++ b/xcode/xcconfig/Userscripts-Vite.xcconfig @@ -4,8 +4,10 @@ INFOPLIST_KEY_NSHumanReadableCopyright = Copyright © Userscripts CURRENT_PROJECT_VERSION = 2 MARKETING_VERSION = 2.0.0 -// Sign to Run Locally -CODE_SIGN_IDENTITY = - +// Development +CODE_SIGN_IDENTITY = Apple Development +// Developer Team ID +DEVELOPMENT_TEAM = XXXXXXXXXX // Organization Identifier ORG_IDENTIFIER = dev.vite // Application Identifier @@ -14,7 +16,7 @@ APP_IDENTIFIER = $(ORG_IDENTIFIER).userscripts EXT_IDENTIFIER = $(APP_IDENTIFIER).ext.safari // App Groups and Group Containers IOS_SHARED_GID = group.$(ORG_IDENTIFIER).userscripts -MAC_SHARED_GID = 00000.$(ORG_IDENTIFIER).userscripts +MAC_SHARED_GID = $(DEVELOPMENT_TEAM).$(ORG_IDENTIFIER).userscripts // App URL Scheme APP_URL_SCHEME = userscripts-vite From 65159a57d733c28690ca75494595a283b9ea6586 Mon Sep 17 00:00:00 2001 From: ACTCD <101378590+ACTCD@users.noreply.github.com> Date: Sat, 28 Sep 2024 14:16:13 +0800 Subject: [PATCH 003/128] docs: update dev doc add team id requirements --- docs/dev.md | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/docs/dev.md b/docs/dev.md index e34d084d..2bfa7681 100644 --- a/docs/dev.md +++ b/docs/dev.md @@ -22,6 +22,7 @@ Reviewing the template will help you understand the composition of the project. - [`Node.js`](https://nodejs.org/) - [`Xcode`](https://geo.itunes.apple.com/app/id497799835) +- `Developer Team ID` (i.e. Apple Account, see [Xcode section](#xcode)) # Dev @@ -30,6 +31,10 @@ Reviewing the template will help you understand the composition of the project. # Build +> [!NOTE] +> Before building the app with Xcode make sure you create the `.dev.xcconfig` files [below](#xcconfig) and fill in your `Developer Team ID`. +> Otherwise `App groups` related functions will not work properly. + - `npm run build:mac` [^1][^2] - `cd ./xcode` - `xcodebuild -scheme Mac` [^1][^2][^3] or build with `Xcode` App @@ -42,32 +47,27 @@ Reviewing the template will help you understand the composition of the project. # Xcode +Please note that a developer account is required, which can be a free Apple Account that has agreed to the Apple Developer Agreement. This is required to obtain a `Team ID` and use the `App groups` capability. + ### Configurations -The Xcode project contains two configurations, which have independent `xcconfig` configuration files, and can run on your local at the same time without conflicts. +The Xcode project contains several configurations, which have independent `xcconfig` configuration files, and can run on your local at the same time without conflicts. + +- `Vite`: for vite real-time development. -- `Debug`: No developer account is required, that is `Sign to Run Locally` by default, which will speed up the build during development. This is convenient for developers without an account, and contributors who only need to do simple local debugging. +- `Debug`: for development and debugging. -- `Release`: A developer account is required, which means it can be used for distribution as well as running on real iOS/iPadOS devices. You will need to override your developer account information in `xcconfig` to complete the build. +- `Release`: for building and distributing. ### xcconfig All `xcconfig` files are in the [`/xcode/xcconfig/`](../xcode/xcconfig) directory. Each `.xcconfig` file can be overridden by `.dev.xcconfig` in the same path, they will be ignored by `git`, so you can override any build settings locally. -For example, you can create an `Userscripts-Release.dev.xcconfig` file to override [`Userscripts-Release.xcconfig`](../xcode/xcconfig/Userscripts-Release.xcconfig) and fill in your own developer account information there: - -`Userscripts-Release.dev.xcconfig` - -``` -DEVELOPMENT_TEAM = XXXXXXXXXX -``` - -Another example, if you want `Debug` builds to be also signed, so instead of enabling `Allow Unsigned Extensions` every time in Safari, you can create: +For example, you can create an `Userscripts-Debug.dev.xcconfig` file to override [`Userscripts-Debug.xcconfig`](../xcode/xcconfig/Userscripts-Debug.xcconfig) and fill in your own developer account `Team ID` there: `Userscripts-Debug.dev.xcconfig` ``` -CODE_SIGN_IDENTITY = Apple Development DEVELOPMENT_TEAM = XXXXXXXXXX ``` @@ -79,4 +79,4 @@ Note that all existing `.xcconfig` files already include `.dev.xcconfig` files o # About -[Userscripts](https://github.com/quoid/userscripts) @ 2018-2023 +[Userscripts](https://github.com/quoid/userscripts) @ 2018-2024 From b073f12e858449a39b0ad6d155c0bc4e073b376a Mon Sep 17 00:00:00 2001 From: ACTCD <101378590+ACTCD@users.noreply.github.com> Date: Sun, 29 Sep 2024 19:13:06 +0800 Subject: [PATCH 004/128] refactor: use async replyable message handler --- xcode/App-Shared/ViewController.swift | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/xcode/App-Shared/ViewController.swift b/xcode/App-Shared/ViewController.swift index ff51fb9d..0c4e9468 100644 --- a/xcode/App-Shared/ViewController.swift +++ b/xcode/App-Shared/ViewController.swift @@ -2,7 +2,7 @@ import WebKit private let logger = USLogger(#fileID) -class ViewController: UIViewController, WKNavigationDelegate, WKScriptMessageHandler, UIDocumentPickerDelegate { +class ViewController: UIViewController, WKNavigationDelegate, WKScriptMessageHandlerWithReply, UIDocumentPickerDelegate { @IBOutlet var webView: WKWebView! @@ -13,7 +13,7 @@ class ViewController: UIViewController, WKNavigationDelegate, WKScriptMessageHan // https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/2875766-seturlschemehandler configuration.setURLSchemeHandler(USchemeHandler(), forURLScheme: AppWebViewUrlScheme) // https://developer.apple.com/documentation/webkit/wkusercontentcontroller - configuration.userContentController.add(self, name: "controller") + configuration.userContentController.addScriptMessageHandler(self, contentWorld: .page, name: "controller") // https://developer.apple.com/documentation/webkit/wkwebview self.webView = WKWebView(frame: .zero, configuration: configuration) // https://developer.apple.com/documentation/webkit/wknavigationdelegate @@ -79,10 +79,13 @@ class ViewController: UIViewController, WKNavigationDelegate, WKScriptMessageHan decisionHandler(.allow) } - func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { + func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) async -> ( + Any?, + String? + ) { guard let name = message.body as? String else { logger?.error("\(#function, privacy: .public) - Userscripts iOS received a message without a name") - return + return (nil, "bad message body") } if name == "CHANGE_DIRECTORY" { // https://developer.apple.com/documentation/uikit/view_controllers/providing_access_to_directories @@ -101,6 +104,7 @@ class ViewController: UIViewController, WKNavigationDelegate, WKScriptMessageHan UIApplication.shared.open(url) } } + return (nil, nil) } func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) { From 3c961e5e9aa2fa57987cc6e3dc4fc52d73a6e8d9 Mon Sep 17 00:00:00 2001 From: ACTCD <101378590+ACTCD@users.noreply.github.com> Date: Sun, 29 Sep 2024 19:24:39 +0800 Subject: [PATCH 005/128] fix: use more stable version number init logic --- src/app/App.svelte | 95 ++++++++++++++++----------- xcode/App-Shared/ViewController.swift | 33 ++++++---- 2 files changed, 78 insertions(+), 50 deletions(-) diff --git a/src/app/App.svelte b/src/app/App.svelte index 47d4f995..2f3a9433 100644 --- a/src/app/App.svelte +++ b/src/app/App.svelte @@ -3,9 +3,8 @@ import logo from "./img/logo.svg?raw"; const baseUrl = "https://github.com/quoid/userscripts"; + const webkit = window.webkit.messageHandlers.controller; - let version = "v0.0.0"; - let build = "(0)"; let directory = "init"; window.APP = { @@ -19,6 +18,12 @@ }, }; + async function initialize() { + const app = await webkit.postMessage("INIT"); + directory = app.directory; + return app; + } + function changeDirectory() { window.webkit?.messageHandlers.controller.postMessage("CHANGE_DIRECTORY"); } @@ -29,44 +34,56 @@
-
- Userscripts App Icon - - -
- {#if import.meta.env.GIT_TAG && import.meta.env.GIT_COMMIT} - - {import.meta.env.GIT_TAG} - - ( - {import.meta.env.GIT_COMMIT.slice(0, 7)} - ) - {:else} - {version} - {build} - {/if} + {#await initialize() then app} +
+ Userscripts App Icon + + +
+ {#if import.meta.env.GIT_TAG && import.meta.env.GIT_COMMIT} + + {import.meta.env.GIT_TAG} + + ( + {import.meta.env.GIT_COMMIT.slice(0, 7)} + ) + {:else} + v{app.version} + ({app.build}) + {/if} +
-
-
-

- You can turn on the Userscripts iOS Safari extension in Settings or - Safari, then use the extension in Safari. Please refer to the "Usage" - section in the - README of this version. -

-
-
- -
CURRENT DIRECTORY:
- -
+
+

+ You can turn on the Userscripts iOS Safari extension in Settings or + Safari, then use the extension in Safari. Please refer to the "Usage" + section in the + README of this version. +

+
+
+ +
CURRENT DIRECTORY:
+ +
+ {:catch error} +
+ {error} +
+ {/await} +