From 92f8218960c7c87d3ac95be145a939ebfceab7bf Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Thu, 2 Jun 2022 11:37:04 -0700 Subject: [PATCH] Fetch icon URLs from pages --- .../Share/ExtensionSaveService.swift | 2 +- .../Sources/Models/PageScrapePayload.swift | 17 ++++++----------- .../Services/DataService/DataService.swift | 3 ++- .../DataService/Mutations/SaveArticle.swift | 2 +- apple/Sources/ShareExtension/ShareExtension.js | 10 +++++++++- 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/AppExtensions/Share/ExtensionSaveService.swift b/apple/OmnivoreKit/Sources/App/AppExtensions/Share/ExtensionSaveService.swift index 0f887e491..258bb853a 100644 --- a/apple/OmnivoreKit/Sources/App/AppExtensions/Share/ExtensionSaveService.swift +++ b/apple/OmnivoreKit/Sources/App/AppExtensions/Share/ExtensionSaveService.swift @@ -140,7 +140,7 @@ class ExtensionSaveService { try await services.dataService.syncUrl(id: requestId, url: pageScrapePayload.url) case let .pdf(localUrl): try await services.dataService.syncPdf(id: requestId, localPdfURL: localUrl, url: pageScrapePayload.url) - case let .html(html, title): + case let .html(html, title, _): try await services.dataService.syncPage(id: requestId, originalHtml: html, title: title, url: pageScrapePayload.url) } diff --git a/apple/OmnivoreKit/Sources/Models/PageScrapePayload.swift b/apple/OmnivoreKit/Sources/Models/PageScrapePayload.swift index f5bd16e6e..ee4435cc2 100644 --- a/apple/OmnivoreKit/Sources/Models/PageScrapePayload.swift +++ b/apple/OmnivoreKit/Sources/Models/PageScrapePayload.swift @@ -9,15 +9,9 @@ import UniformTypeIdentifiers let URLREGEX = #"[(http(s)?):\/\/(www\.)?a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)"# public struct PageScrapePayload { - public struct HTMLPayload { - let url: String - let title: String? - let html: String - } - public enum ContentType { case none - case html(html: String, title: String?) + case html(html: String, title: String?, iconURL: String?) case pdf(localUrl: URL) } @@ -34,9 +28,9 @@ public struct PageScrapePayload { self.contentType = .pdf(localUrl: localUrl) } - init(url: String, title: String?, html: String) { + init(url: String, title: String?, html: String, iconURL: String? = nil) { self.url = url - self.contentType = .html(html: html, title: title) + self.contentType = .html(html: html, title: title, iconURL: iconURL) } } @@ -280,8 +274,9 @@ private extension PageScrapePayload { static func makeFromDictionary(_ dictionary: NSDictionary) -> PageScrapePayload? { let results = dictionary[NSExtensionJavaScriptPreprocessingResultsKey] as? NSDictionary guard let url = results?["url"] as? String else { return nil } - let html = results?["documentHTML"] as? String + let html = results?["originalHTML"] as? String let title = results?["title"] as? String + let iconURL = results?["iconURL"] as? String let contentType = results?["contentType"] as? String // If we were not able to capture any HTML, treat this as a URL and @@ -297,7 +292,7 @@ private extension PageScrapePayload { } if let html = html { - return PageScrapePayload(url: url, title: title, html: html) + return PageScrapePayload(url: url, title: title, html: html, iconURL: iconURL) } return PageScrapePayload(url: url) diff --git a/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift b/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift index 3e005425a..b2db01cab 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift @@ -154,9 +154,10 @@ public final class DataService: ObservableObject { // self.createThumbnailFor(inputUrl: localUrl, at: thumbnailUrl) // linkedItem.imageURLString = thumbnailUrl.absoluteString - case let .html(html: html, title: title): + case let .html(html: html, title: title, iconURL: iconURL): linkedItem.contentReader = "WEB" linkedItem.originalHtml = html + linkedItem.imageURLString = iconURL linkedItem.title = title ?? self.titleFromPdfFile(pageScrape.url) case .none: print("SAVING URL", linkedItem.unwrappedPageURLString) diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/SaveArticle.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/SaveArticle.swift index bd1b0fdfa..9406867ad 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/SaveArticle.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/SaveArticle.swift @@ -92,7 +92,7 @@ public extension DataService { } let preparedDocument: InputObjects.PreparedDocumentInput? = { - if case let .html(html, title) = pageScrapePayload.contentType { + if case let .html(html, title, _) = pageScrapePayload.contentType { return InputObjects.PreparedDocumentInput( document: html, pageInfo: InputObjects.PageInfoInput(title: OptionalArgument(title)) diff --git a/apple/Sources/ShareExtension/ShareExtension.js b/apple/Sources/ShareExtension/ShareExtension.js index fd7c4c0b6..24de12ff9 100644 --- a/apple/Sources/ShareExtension/ShareExtension.js +++ b/apple/Sources/ShareExtension/ShareExtension.js @@ -1,12 +1,20 @@ var ShareExtension = function() {}; +function iconURL() { + try { + return document.querySelector("link[rel='apple-touch-icon'], link[rel='shortcut icon'], link[rel='icon']").href + } catch {} + return null +} + ShareExtension.prototype = { run: function(arguments) { arguments.completionFunction({ 'url': window.location.href, 'title': document.title.toString(), + 'iconURL': iconURL(), 'contentType': document.contentType, - 'documentHTML': new XMLSerializer().serializeToString(document), + 'originalHTML': new XMLSerializer().serializeToString(document) }); } };