Skip to content

Commit

Permalink
Indicate explicitly when client has page enabled
Browse files Browse the repository at this point in the history
This helps further developments when we want to perform operations only on the first injection.
Relying on listening state is limiting.
  • Loading branch information
JingMatrix committed Jan 22, 2025
1 parent 02ca54a commit 3e26fb1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/org/matrix/chromext/Chrome.kt
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ object Chrome {
wakeUpDevTools()
var client = DevSessions.new(tabId, "page")
DevSessions.add(client)
client.enablePage()
val duplicated = client.isPageEnabled(true)
if (duplicated) return@submit
var frames: MutableMap<String, String> = mutableMapOf<String, String>()
client.listen {
if (it.has("method")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ class DevToolClient(tabId: String, tag: String? = null) : LocalSocket() {
return mClosed
}

fun enablePage() {
if (!pageEnabled) {
fun isPageEnabled(forceActivate: Boolean = false): Boolean {
val status = pageEnabled
if (forceActivate && !pageEnabled) {
command(null, "Page.enable", JSONObject())
pageEnabled = true
}
return status
}

fun command(id: Int?, method: String, params: JSONObject?) {
Expand All @@ -78,7 +80,7 @@ class DevToolClient(tabId: String, tag: String? = null) : LocalSocket() {

fun bypassCSP(bypass: Boolean) {
if (cspBypassed == bypass) return
enablePage()
isPageEnabled(true)
command(null, "Page.setBypassCSP", JSONObject().put("enabled", bypass))
cspBypassed = bypass
if (bypass) DevSessions.add(this)
Expand All @@ -94,10 +96,7 @@ class DevToolClient(tabId: String, tag: String? = null) : LocalSocket() {
}

fun listen(callback: (JSONObject) -> Unit = { msg -> Log.d(msg.toString()) }) {
if (listening) {
Log.d("skip duplicated listen calls")
return
}
if (listening) Log.w("client was being listened on")
listening = true
runCatching {
while (!isClosed()) {
Expand Down

0 comments on commit 3e26fb1

Please sign in to comment.