Fetch icon URLs from pages

This commit is contained in:
Jackson Harper
2022-06-02 11:37:04 -07:00
parent 2ed1ea29b9
commit 92f8218960
5 changed files with 19 additions and 15 deletions

View File

@ -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)
}

View File

@ -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)

View File

@ -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)

View File

@ -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))

View File

@ -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)
});
}
};