From 5e8e39219f300cc14816dd13a8a8e91b09cc710c Mon Sep 17 00:00:00 2001 From: Franz Hoepfner Date: Sat, 2 Nov 2024 22:08:40 +0100 Subject: [PATCH] Add title and content of webpage to share extension --- App/Lib/WallabagSession.swift | 2 +- Intents/AddEntryIntent.swift | 2 +- .../Endpoint/WallabagEntryEndpoint.swift | 6 +++--- bagit/ExtensionClass.js | 4 +++- bagit/ShareViewController.swift | 17 ++++++++++------- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/App/Lib/WallabagSession.swift b/App/Lib/WallabagSession.swift index df57e8ad..2b27c1fb 100644 --- a/App/Lib/WallabagSession.swift +++ b/App/Lib/WallabagSession.swift @@ -49,7 +49,7 @@ final class WallabagSession: ObservableObject { } func addEntry(url: String) async throws { - let wallabagEntry: WallabagEntry = try await kit.send(to: WallabagEntryEndpoint.add(url: url)) + let wallabagEntry: WallabagEntry = try await kit.send(to: WallabagEntryEndpoint.add(url: url, title: nil, content: nil)) let entry = Entry(context: coreDataContext) entry.hydrate(from: wallabagEntry) diff --git a/Intents/AddEntryIntent.swift b/Intents/AddEntryIntent.swift index bc759a06..c17f4b45 100644 --- a/Intents/AddEntryIntent.swift +++ b/Intents/AddEntryIntent.swift @@ -15,7 +15,7 @@ struct AddEntryIntent: WallabagIntent { func perform() async throws -> some IntentResult { _ = try await kit.requestTokenAsync() - _ = try await kit.send(to: WallabagEntryEndpoint.add(url: url.absoluteString)) + _ = try await kit.send(to: WallabagEntryEndpoint.add(url: url.absoluteString, title: nil, content: nil)) return .result() } diff --git a/WallabagKit/Sources/WallabagKit/Endpoint/WallabagEntryEndpoint.swift b/WallabagKit/Sources/WallabagKit/Endpoint/WallabagEntryEndpoint.swift index 7ee3f8bb..22769619 100644 --- a/WallabagKit/Sources/WallabagKit/Endpoint/WallabagEntryEndpoint.swift +++ b/WallabagKit/Sources/WallabagKit/Endpoint/WallabagEntryEndpoint.swift @@ -4,7 +4,7 @@ public enum WallabagEntryEndpoint: WallabagKitEndpoint { public typealias Object = WallabagEntry case get(page: Int = 1, perPage: Int = 30) - case add(url: String) + case add(url: String, title: String?, content: String?) case addTag(tag: String, entry: Int) case delete(id: Int) case deleteTag(tagId: Int, entry: Int) @@ -54,9 +54,9 @@ public enum WallabagEntryEndpoint: WallabagKitEndpoint { public func getBody() -> Data { switch self { - case let .add(url): + case let .add(url, title, content): // swiftlint:disable:next force_try - try! JSONSerialization.data(withJSONObject: ["url": url], options: .prettyPrinted) + try! JSONSerialization.data(withJSONObject: ["url": url, "title": title, "content": content], options: .prettyPrinted) case let .update(_, parameters): // swiftlint:disable:next force_try try! JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) diff --git a/bagit/ExtensionClass.js b/bagit/ExtensionClass.js index 906ea2c0..6cd029fd 100644 --- a/bagit/ExtensionClass.js +++ b/bagit/ExtensionClass.js @@ -3,7 +3,9 @@ var ExtensionClass = function() {}; ExtensionClass.prototype = { run: function(arguments) { arguments.completionFunction({ - "href": document.location.href + "href": document.location.href, + "contentHTML": document.body.innerHTML, + "title": document.title.toString() }); } }; diff --git a/bagit/ShareViewController.swift b/bagit/ShareViewController.swift index 4e1afc9e..3d3407f7 100644 --- a/bagit/ShareViewController.swift +++ b/bagit/ShareViewController.swift @@ -51,7 +51,7 @@ class ShareViewController: UIViewController { kit.username = WallabagUserDefaults.login kit.password = WallabagUserDefaults.password - getUrl { shareURL in + getUrl { shareURL, title, contentHtml in guard let shareURL else { self.clearView(withError: .retrievingURL) return @@ -60,7 +60,7 @@ class ShareViewController: UIViewController { Task { do { _ = try await kit.requestTokenAsync() - let _: WallabagEntry = try await kit.send(to: WallabagEntryEndpoint.add(url: shareURL)) + let _: WallabagEntry = try await kit.send(to: WallabagEntryEndpoint.add(url: shareURL, title: title, content: contentHtml)) self.clearView(withError: nil) } catch { self.clearView(withError: .duringAdding) @@ -74,9 +74,9 @@ class ShareViewController: UIViewController { } } - private func getUrl(completion: @escaping (String?) -> Void) { + private func getUrl(completion: @escaping (String?, String?, String?) -> Void) { guard let item = extensionContext?.inputItems.first as? NSExtensionItem else { - completion(nil) + completion(nil, nil, nil) return } @@ -93,18 +93,21 @@ class ShareViewController: UIViewController { let results = dictionary[NSExtensionJavaScriptPreprocessingResultsKey] as? NSDictionary, let href = results["href"] as? String else { - completion(nil) + completion(nil, nil, nil) return } - completion(href) + let title = results["title"] as? String + let contentHtml = results["contentHTML"] as? String + + completion(href, title, contentHtml) } ) } if attachment.hasItemConformingToTypeIdentifier(publicURL) { attachment.loadItem(forTypeIdentifier: publicURL, options: nil) { item, _ in - completion((item as? NSURL)!.absoluteString!) + completion((item as? NSURL)!.absoluteString!, nil, nil) } } }