Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix posts page crashes evaluating JavaScript #1173

Merged
merged 2 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions App/Extensions/WKWebView+eval.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// WKWebView+eval.swift
//
// Copyright 2015 Awful Contributors. CC BY-NC-SA 3.0 US https://github.com/Awful/Awful.app

import WebKit

extension WKWebView {
/**
Evaluates the specified JavaScript string.

The async-await version of `evaluateJavaScript(_:)` in the overlay assumes a non-`nil` result of evaluating `javaScript`. This async-await version does not make that assumption.
*/
@discardableResult
func eval(_ javaScript: String) async throws -> Any? {
try await withCheckedThrowingContinuation { continuation in
evaluateJavaScript(javaScript, completionHandler: { result, error in
if let error {
continuation.resume(throwing: error)
} else {
continuation.resume(returning: result)
}
})
}
}
}
20 changes: 11 additions & 9 deletions App/View Controllers/AcknowledgementsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,19 @@ final class AcknowledgementsViewController: ViewController {

override func themeDidChange() {
super.themeDidChange()

let js = """
var s = document.body.style;
s.backgroundColor = "\(backgroundColor.hexCode)";
s.color = "\(textColor.hexCode)";
"""
webView.evaluateJavaScript(js, completionHandler: { result, error in
if let error = error {

Task {
let js = """
var s = document.body.style;
s.backgroundColor = "\(backgroundColor.hexCode)";
s.color = "\(textColor.hexCode)";
"""
do {
try await webView.eval(js)
} catch {
Log.e("error running script `\(js)` in acknowledgements screen: \(error)")
}
})
}
}

override func viewWillAppear(_ animated: Bool) {
Expand Down
86 changes: 31 additions & 55 deletions App/View Controllers/Posts/PostsPageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,56 +33,43 @@ final class PostsPageViewController: ViewController {
private var webViewDidLoadOnce = false

lazy var threadActionsMenu: UIMenu = {
var threadActions: [UIMenuElement] = []

let bookmarkTitle = self.thread.bookmarked ? "Remove Bookmark" : "Bookmark Thread"
let bookmarkImage = self.thread.bookmarked ?
UIImage(named: "remove-bookmark")!.withRenderingMode(.alwaysTemplate)
:
UIImage(named: "add-bookmark")!.withRenderingMode(.alwaysTemplate)
let yourPostsImage = UIImage(named: "single-users-posts")!.withRenderingMode(.alwaysTemplate)
let copyURLImage = UIImage(named: "copy-url")!.withRenderingMode(.alwaysTemplate)
let voteImage = UIImage(named: "vote")!.withRenderingMode(.alwaysTemplate)

var threadActions: [UIAction] = []

// Bookmark
let bookmarkIdentifier = UIAction.Identifier("bookmark")
let bookmarkAction = UIAction(title: bookmarkTitle, image: bookmarkImage, identifier: bookmarkIdentifier, handler: { [unowned self] action in
bookmark(action: action)
})
bookmarkAction.attributes = self.thread.bookmarked ? [.destructive] : []
let bookmarkAction = UIAction(
title: thread.bookmarked ? "Remove Bookmark" : "Bookmark Thread",
image: UIImage(named: thread.bookmarked ? "remove-bookmark" : "add-bookmark")!.withRenderingMode(.alwaysTemplate),
identifier: .init("bookmark"),
handler: { [unowned self] in bookmark(action: $0) }
)
bookmarkAction.attributes = thread.bookmarked ? .destructive : []
threadActions.append(bookmarkAction)

// Copy link
let copyLinkIdentifier = UIAction.Identifier("copyLink")
let copyLinkAction = UIAction(title: "Copy link", image: copyURLImage, identifier: copyLinkIdentifier, handler: { [unowned self] action in
copyLink(action: action)
})
threadActions.append(copyLinkAction)

threadActions.append(.init(
title: "Copy link",
image: UIImage(named: "copy-url")!.withRenderingMode(.alwaysTemplate),
identifier: .init("copyLink"),
handler: { [unowned self] in copyLink(action: $0) }
))

// Vote
let voteIdentifier = UIAction.Identifier("vote")
let voteAction = UIAction(title: "Vote", image: voteImage, identifier: voteIdentifier, handler: { [unowned self] action in
vote(action: action)
})
threadActions.append(voteAction)

threadActions.append(.init(
title: "Vote",
image: UIImage(named: "vote")!.withRenderingMode(.alwaysTemplate),
identifier: .init("vote"),
handler: { [unowned self] in vote(action: $0) }
))

// Your posts
let yourPostsIdentifier = UIAction.Identifier("yourPosts")
let yourPostsAction = UIAction(title: "Your posts", image: yourPostsImage, identifier: yourPostsIdentifier, handler: { [unowned self] action in
yourPosts(action: action)
})
threadActions.append(yourPostsAction)

if #available(iOS 14.0, *) {
// no op. iOS14+ uses UIMenu, 13 uses Chidori third party menus
} else {
actionMappings[bookmarkIdentifier] = bookmark(action:)
actionMappings[copyLinkIdentifier] = copyLink(action:)
actionMappings[voteIdentifier] = vote(action:)
actionMappings[yourPostsIdentifier] = yourPosts(action:)
}

return UIMenu(title: self.thread.title ?? "", image: nil, identifier: nil, options: [.displayInline], children: threadActions)
threadActions.append(.init(
title: "Your posts",
image: UIImage(named: "single-users-posts")!.withRenderingMode(.alwaysTemplate),
identifier: .init("yourPosts"),
handler: { [unowned self] in yourPosts(action: $0) }
))

return UIMenu(title: thread.title ?? "", image: nil, identifier: nil, options: .displayInline, children: threadActions)
}()


Expand Down Expand Up @@ -1026,8 +1013,6 @@ final class PostsPageViewController: ViewController {

self.navigationController?.pushViewController(postsVC, animated: true)

print("Your Posts")

}
}

Expand Down Expand Up @@ -1287,15 +1272,6 @@ final class PostsPageViewController: ViewController {
self.selectedPost = posts[postIndex + hiddenPosts]
self.selectedFrame = frame

let possessiveUsername: String
if self.selectedPost!.author?.username == UserDefaults.standard.loggedInUsername {
possessiveUsername = "Your"
} else {
possessiveUsername = "\(self.selectedPost!.author?.username ?? "")'s"
}

print("\(possessiveUsername)")

let postActionMenu: UIMenu = {
// edit post
if self.selectedPost!.editable {
Expand Down
Loading
Loading