Skip to content

Commit

Permalink
Add title and content of webpage to share extension (#420)
Browse files Browse the repository at this point in the history
  • Loading branch information
kinnarr authored Nov 4, 2024
1 parent bbead69 commit d4a78c4
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion App/Lib/WallabagSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Intents/AddEntryIntent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion bagit/ExtensionClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
});
}
};
Expand Down
17 changes: 10 additions & 7 deletions bagit/ShareViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
}

Expand All @@ -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)
}
}
}
Expand Down

0 comments on commit d4a78c4

Please sign in to comment.