Compare commits
10 Commits
fix/resour
...
master
| Author | SHA1 | Date |
|---|---|---|
|
|
b40f0ac185 | |
|
|
dbcad92ff0 | |
|
|
605e1aeeef | |
|
|
6e712bf664 | |
|
|
0b860372e9 | |
|
|
c8f4fb213e | |
|
|
7ba0aff437 | |
|
|
b3994f932e | |
|
|
1d178f7154 | |
|
|
784187e911 |
2
Cartfile
2
Cartfile
|
|
@ -1 +1 @@
|
||||||
github "ReactiveX/RxSwift" ~> 4.3
|
binary "https://raw.github.com/TouchInstinct/CarthageBinaries/master/RxSwift/RxSwift.json"
|
||||||
|
|
@ -1 +1 @@
|
||||||
github "ReactiveX/RxSwift" "4.4.0"
|
binary "https://raw.github.com/TouchInstinct/CarthageBinaries/master/RxSwift/RxSwift.json" "4.5.0"
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,8 @@ import WebKit
|
||||||
/**
|
/**
|
||||||
*/
|
*/
|
||||||
public class ReCaptcha {
|
public class ReCaptcha {
|
||||||
|
public typealias BoolParameterClosure = (Bool) -> ()
|
||||||
|
|
||||||
fileprivate struct Constants {
|
fileprivate struct Constants {
|
||||||
struct InfoDictKeys {
|
struct InfoDictKeys {
|
||||||
static let APIKey = "ReCaptchaKey"
|
static let APIKey = "ReCaptchaKey"
|
||||||
|
|
@ -101,6 +103,12 @@ public class ReCaptcha {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Callback for WebView loading state changing
|
||||||
|
public var onLoadingChanged: BoolParameterClosure? {
|
||||||
|
get { return manager.onLoadingChanged }
|
||||||
|
set { manager.onLoadingChanged = newValue }
|
||||||
|
}
|
||||||
|
|
||||||
/// The worker that handles webview events and communication
|
/// The worker that handles webview events and communication
|
||||||
let manager: ReCaptchaWebViewManager
|
let manager: ReCaptchaWebViewManager
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,13 @@ internal class ReCaptchaWebViewManager {
|
||||||
static let ExecuteJSCommand = "execute();"
|
static let ExecuteJSCommand = "execute();"
|
||||||
static let ResetCommand = "reset();"
|
static let ResetCommand = "reset();"
|
||||||
static let BotUserAgent = "Googlebot/2.1"
|
static let BotUserAgent = "Googlebot/2.1"
|
||||||
|
static let NumberOfDivsCommand = "document.getElementsByTagName(\"div\").length"
|
||||||
|
|
||||||
|
static let MinNumberOfDivs = 5
|
||||||
|
static let NumberOfDivsFinishedLoadingAttempts = 10
|
||||||
|
|
||||||
|
// A page doesn't have enough time to load on old devices
|
||||||
|
static let NumberOfDivsLoadingDelay = 0.5
|
||||||
}
|
}
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
|
|
@ -37,6 +44,9 @@ internal class ReCaptchaWebViewManager {
|
||||||
public var shouldSkipForTests = false
|
public var shouldSkipForTests = false
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/// Callback for loading state changing
|
||||||
|
var onLoadingChanged: ReCaptcha.BoolParameterClosure?
|
||||||
|
|
||||||
/// Sends the result message
|
/// Sends the result message
|
||||||
var completion: ((ReCaptchaResult) -> Void)?
|
var completion: ((ReCaptchaResult) -> Void)?
|
||||||
|
|
||||||
|
|
@ -99,6 +109,7 @@ internal class ReCaptchaWebViewManager {
|
||||||
*/
|
*/
|
||||||
init(html: String, apiKey: String, baseURL: URL, endpoint: String) {
|
init(html: String, apiKey: String, baseURL: URL, endpoint: String) {
|
||||||
self.endpoint = endpoint
|
self.endpoint = endpoint
|
||||||
|
|
||||||
self.decoder = ReCaptchaDecoder { [weak self] result in
|
self.decoder = ReCaptchaDecoder { [weak self] result in
|
||||||
self?.handle(result: result)
|
self?.handle(result: result)
|
||||||
}
|
}
|
||||||
|
|
@ -126,6 +137,7 @@ internal class ReCaptchaWebViewManager {
|
||||||
Starts the challenge validation
|
Starts the challenge validation
|
||||||
*/
|
*/
|
||||||
func validate(on view: UIView) {
|
func validate(on view: UIView) {
|
||||||
|
onLoadingChanged?(true)
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
guard !shouldSkipForTests else {
|
guard !shouldSkipForTests else {
|
||||||
completion?(.token(""))
|
completion?(.token(""))
|
||||||
|
|
@ -166,7 +178,7 @@ internal class ReCaptchaWebViewManager {
|
||||||
/** Private methods for ReCaptchaWebViewManager
|
/** Private methods for ReCaptchaWebViewManager
|
||||||
*/
|
*/
|
||||||
fileprivate extension ReCaptchaWebViewManager {
|
fileprivate extension ReCaptchaWebViewManager {
|
||||||
/** Executes the JS command that loads the ReCaptcha challenge.
|
/** Executes the JS command that loads the ReCaptcha challenge after a page finished loading.
|
||||||
This method has no effect if the webview hasn't finished loading.
|
This method has no effect if the webview hasn't finished loading.
|
||||||
*/
|
*/
|
||||||
func execute() {
|
func execute() {
|
||||||
|
|
@ -175,10 +187,65 @@ fileprivate extension ReCaptchaWebViewManager {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
evaluateExecuteWhenLoadingFinished(count: 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
- parameter count: Number of checks of number of divs
|
||||||
|
|
||||||
|
Executes the JS command that returns number of divs.
|
||||||
|
*/
|
||||||
|
func evaluateExecuteWhenLoadingFinished(count: Int) {
|
||||||
|
webView.evaluateJavaScript(Constants.NumberOfDivsCommand) { [weak self] (result, error) -> Void in
|
||||||
|
if let error = error {
|
||||||
|
self?.decoder.send(error: .unexpected(error))
|
||||||
|
self?.onLoadingChanged?(false)
|
||||||
|
} else {
|
||||||
|
self?.handleNumberOfDivs(result: result, count: count)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
- parameters:
|
||||||
|
- result: Result of number of divs command evaluation
|
||||||
|
- count: Number of checks of divs count
|
||||||
|
|
||||||
|
Handles number of divs command result.
|
||||||
|
*/
|
||||||
|
func handleNumberOfDivs(result: Any?, count: Int) {
|
||||||
|
if let result = result as? Int, result >= Constants.MinNumberOfDivs {
|
||||||
|
evaluateExecute()
|
||||||
|
} else {
|
||||||
|
handleInvalidNumberOfDivsResult(count: count)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
- parameter count: Number of checks of number of divs
|
||||||
|
|
||||||
|
Handles invalid number of divs.
|
||||||
|
*/
|
||||||
|
func handleInvalidNumberOfDivsResult(count: Int) {
|
||||||
|
if count < Constants.NumberOfDivsFinishedLoadingAttempts {
|
||||||
|
DispatchQueue.main.asyncAfter(deadline: .now() + Constants.NumberOfDivsLoadingDelay) { [weak self] in
|
||||||
|
self?.evaluateExecuteWhenLoadingFinished(count: count + 1)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
decoder.send(error: .htmlLoadError)
|
||||||
|
onLoadingChanged?(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Executes the JS command that loads the ReCaptcha challenge.
|
||||||
|
*/
|
||||||
|
func evaluateExecute() {
|
||||||
webView.evaluateJavaScript(Constants.ExecuteJSCommand) { [weak self] _, error in
|
webView.evaluateJavaScript(Constants.ExecuteJSCommand) { [weak self] _, error in
|
||||||
if let error = error {
|
if let error = error {
|
||||||
self?.decoder.send(error: .unexpected(error))
|
self?.decoder.send(error: .unexpected(error))
|
||||||
}
|
}
|
||||||
|
self?.onLoadingChanged?(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,10 @@
|
||||||
import RxSwift
|
import RxSwift
|
||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
|
public enum ReCaptchaRxError: Error {
|
||||||
|
case baseWasReleased
|
||||||
|
}
|
||||||
|
|
||||||
/// Makes ReCaptcha compatible with RxSwift extensions
|
/// Makes ReCaptcha compatible with RxSwift extensions
|
||||||
extension ReCaptcha: ReactiveCompatible {}
|
extension ReCaptcha: ReactiveCompatible {}
|
||||||
|
|
||||||
|
|
@ -64,4 +68,20 @@ public extension Reactive where Base: ReCaptcha {
|
||||||
base?.reset()
|
base?.reset()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Observable of loading state
|
||||||
|
(will not work if someone changes onLoadingChanged variable; current onLodinglChanged will not work after subscription)
|
||||||
|
*/
|
||||||
|
var loadingObservable: Observable<Bool> {
|
||||||
|
return .create { [weak base] observer -> Disposable in
|
||||||
|
guard let base = base else {
|
||||||
|
observer.onError(ReCaptchaRxError.baseWasReleased)
|
||||||
|
return Disposables.create()
|
||||||
|
}
|
||||||
|
|
||||||
|
base.onLoadingChanged = { observer.onNext($0) }
|
||||||
|
return Disposables.create { [weak base] in base?.onLoadingChanged = nil }
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue