This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 442
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added JS event listener for tab web-state.
- Loading branch information
Showing
7 changed files
with
220 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
Client/Frontend/Browser/Helpers/ReadyStateScriptHelper.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright 2022 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
import Foundation | ||
import Shared | ||
import WebKit | ||
|
||
private let log = Logger.browserLogger | ||
|
||
struct ReadyState: Codable { | ||
let securityToken: String | ||
let state: State | ||
|
||
enum State: String, Codable { | ||
// Page State | ||
case loading | ||
case interactive | ||
case complete | ||
case loaded | ||
|
||
// History State | ||
case pushstate | ||
case replacestate | ||
case popstate | ||
} | ||
|
||
public static func from(message: WKScriptMessage) -> ReadyState? { | ||
if !JSONSerialization.isValidJSONObject(message.body) { | ||
return nil | ||
} | ||
|
||
do { | ||
let data = try JSONSerialization.data(withJSONObject: message.body, options: []) | ||
return try JSONDecoder().decode(ReadyState.self, from: data) | ||
} catch { | ||
log.error("Error Decoding ReadyState: \(error)") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
private enum CodingKeys: String, CodingKey { | ||
case securityToken = "securitytoken" | ||
case state | ||
} | ||
} | ||
|
||
class ReadyStateScriptHelper: TabContentScript { | ||
private weak var tab: Tab? | ||
private var debounceTimer: Timer? | ||
|
||
required init(tab: Tab) { | ||
self.tab = tab | ||
} | ||
|
||
class func name() -> String { | ||
return "ReadyStateScriptHelper" | ||
} | ||
|
||
func scriptMessageHandlerName() -> String? { | ||
return "ReadyState_\(UserScriptManager.messageHandlerTokenString)" | ||
} | ||
|
||
func userContentController(_ userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage, replyHandler: (Any?, String?) -> Void) { | ||
|
||
defer { replyHandler(nil, nil) } | ||
|
||
guard let readyState = ReadyState.from(message: message) else { | ||
log.error("Invalid Ready State") | ||
return | ||
} | ||
|
||
guard readyState.securityToken == UserScriptManager.securityTokenString else { | ||
log.error("Invalid or Missing security token") | ||
return | ||
} | ||
|
||
tab?.onPageReadyStateChanged?(readyState.state) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2021 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
(function() { | ||
// Listen for document ready state | ||
document.addEventListener('readystatechange', (event) => { | ||
window.webkit.messageHandlers.$<handler>.postMessage({ | ||
"securitytoken": "$<security_token>", | ||
"state": document.readyState | ||
}); | ||
}); | ||
|
||
// Listen for document load state | ||
window.addEventListener('load', (event) => { | ||
window.webkit.messageHandlers.$<handler>.postMessage({ | ||
"securitytoken": "$<security_token>", | ||
"state": "loaded" | ||
}); | ||
}); | ||
|
||
// Listen for history popped | ||
window.addEventListener('popstate', (event) => { | ||
if (event.state) { | ||
// Run on the browser's next run-loop | ||
setTimeout(() => { | ||
window.webkit.messageHandlers.$<handler>.postMessage({ | ||
"securitytoken": "$<security_token>", | ||
"state": "popstate" | ||
}); | ||
}, 0); | ||
} | ||
}); | ||
|
||
// Listen for history pushed | ||
const pushState = History.prototype.pushState; | ||
History.prototype.pushState = function(state, unused, url) { | ||
pushState.call(this, state, unused, url); | ||
|
||
if (state) { | ||
// Run on the browser's next run-loop | ||
setTimeout(() => { | ||
window.webkit.messageHandlers.$<handler>.postMessage({ | ||
"securitytoken": "$<security_token>", | ||
"state": "pushstate" | ||
}); | ||
}, 0); | ||
} | ||
}; | ||
|
||
// Listen for history replaced | ||
const replaceState = History.prototype.replaceState; | ||
History.prototype.replaceState = function(state, unused, url) { | ||
replaceState.call(this, state, unused, url); | ||
|
||
if (state) { | ||
// Run on the browser's next run-loop | ||
setTimeout(() => { | ||
window.webkit.messageHandlers.$<handler>.postMessage({ | ||
"securitytoken": "$<security_token>", | ||
"state": "replacestate" | ||
}); | ||
}, 0); | ||
} | ||
}; | ||
|
||
// Hide the pushState trampoline | ||
History.prototype.pushState.toString = function() { | ||
return "function () { [native code] }"; | ||
}; | ||
|
||
// Hide the replaceState trampoline | ||
History.prototype.replaceState.toString = function() { | ||
return "function () { [native code] }"; | ||
}; | ||
})(); |