Skip to content

Commit

Permalink
Bugfix FXIOS-6206 [v112.2] Restore iOS version checks on js injection (
Browse files Browse the repository at this point in the history
…#14011) (#14016)

* Restore iOS version checks on js injection

* Add a comment for future reference

* Fix swiftlint

(cherry picked from commit d28cd9d)

Co-authored-by: OrlaM <[email protected]>
  • Loading branch information
mergify[bot] and OrlaM authored Apr 19, 2023
1 parent cb190bd commit 7514068
Showing 1 changed file with 49 additions and 13 deletions.
62 changes: 49 additions & 13 deletions Shared/Extensions/WKWebViewExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,81 @@ import Foundation
import WebKit

extension WKWebView {
/// This calls WebKit evaluateJavaScript function
/// This calls different WebKit evaluateJavaScript functions depending on iOS version
/// - If iOS14 or higher, evaluates Javascript in a .defaultClient sandboxed content world
/// - If below iOS14, evaluates Javascript without sandboxed environment
/// - Parameters:
/// - javascript: String representing javascript to be evaluated
public func evaluateJavascriptInDefaultContentWorld(_ javascript: String) {
self.evaluateJavaScript(javascript, in: nil, in: .defaultClient, completionHandler: { _ in })
// iOS 14.3 is required here because of a webkit bug in lower iOS versions with this API
if #available(iOS 14.3, *) {
self.evaluateJavaScript(javascript, in: nil, in: .defaultClient, completionHandler: { _ in })
} else {
self.evaluateJavaScript(javascript)
}
}

/// This calls WebKit evaluateJavaScript function with a completion that passes a tuple with optional data or an optional error
/// This calls different WebKit evaluateJavaScript functions depending on iOS version with a completion that passes a tuple with optional data or an optional error
/// - If iOS14 or higher, evaluates Javascript in a .defaultClient sandboxed content world
/// - If below iOS14, evaluates Javascript without sandboxed environment
/// - Parameters:
/// - javascript: String representing javascript to be evaluated
/// - completion: Tuple containing optional data and an optional error
public func evaluateJavascriptInDefaultContentWorld(_ javascript: String, _ completion: @escaping (Any?, Error?) -> Void) {
self.evaluateJavaScript(javascript, in: nil, in: .defaultClient) { result in
switch result {
case .success(let value):
completion(value, nil)
case .failure(let error):
completion(nil, error)
// iOS 14.3 is required here because of a webkit bug in lower iOS versions with this API
if #available(iOS 14.3, *) {
self.evaluateJavaScript(javascript, in: nil, in: .defaultClient) { result in
switch result {
case .success(let value):
completion(value, nil)
case .failure(let error):
completion(nil, error)
}
}
} else {
self.evaluateJavaScript(javascript) { data, error in
completion(data, error)
}
}
}
}

extension WKUserContentController {
public func addInDefaultContentWorld(scriptMessageHandler: WKScriptMessageHandler, name: String) {
add(scriptMessageHandler, contentWorld: .defaultClient, name: name)
// iOS 14.3 is required here because of a webkit bug in lower iOS versions with this API
if #available(iOS 14.3, *) {
add(scriptMessageHandler, contentWorld: .defaultClient, name: name)
} else {
add(scriptMessageHandler, name: name)
}
}

public func addInPageContentWorld(scriptMessageHandler: WKScriptMessageHandler, name: String) {
add(scriptMessageHandler, contentWorld: .page, name: name)
// iOS 14.3 is required here because of a webkit bug in lower iOS versions with this API
if #available(iOS 14.3, *) {
add(scriptMessageHandler, contentWorld: .page, name: name)
} else {
add(scriptMessageHandler, name: name)
}
}
}

extension WKUserScript {
public class func createInDefaultContentWorld(source: String, injectionTime: WKUserScriptInjectionTime, forMainFrameOnly: Bool) -> WKUserScript {
return WKUserScript(source: source, injectionTime: injectionTime, forMainFrameOnly: forMainFrameOnly, in: .defaultClient)
// iOS 14.3 is required here because of a webkit bug in lower iOS versions with this API
if #available(iOS 14.3, *) {
return WKUserScript(source: source, injectionTime: injectionTime, forMainFrameOnly: forMainFrameOnly, in: .defaultClient)
} else {
return WKUserScript(source: source, injectionTime: injectionTime, forMainFrameOnly: forMainFrameOnly)
}
}

public class func createInPageContentWorld(source: String, injectionTime: WKUserScriptInjectionTime, forMainFrameOnly: Bool) -> WKUserScript {
return WKUserScript(source: source, injectionTime: injectionTime, forMainFrameOnly: forMainFrameOnly, in: .page)
// iOS 14.3 is required here because of a webkit bug in lower iOS versions with this API
if #available(iOS 14.3, *) {
return WKUserScript(source: source, injectionTime: injectionTime, forMainFrameOnly: forMainFrameOnly, in: .page)
} else {
return WKUserScript(source: source, injectionTime: injectionTime, forMainFrameOnly: forMainFrameOnly)
}
}
}

1 comment on commit 7514068

@firefoxci-taskcluster
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh oh! Looks like an error! Details

InterpreterError at template.tasks[0].extra[0].treeherder[1].symbol: unknown context value cron

Please sign in to comment.