Skip to content

Commit

Permalink
Refactor ContextMenuHook
Browse files Browse the repository at this point in the history
  • Loading branch information
JingMatrix committed Aug 26, 2023
1 parent 9b2ac2d commit fc86daa
Showing 1 changed file with 44 additions and 37 deletions.
81 changes: 44 additions & 37 deletions app/src/main/java/org/matrix/chromext/hook/ContextMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.webkit.WebView
import de.robv.android.xposed.XC_MethodHook.Unhook
import org.matrix.chromext.Chrome
import org.matrix.chromext.Listener
import org.matrix.chromext.R
import org.matrix.chromext.script.Local
import org.matrix.chromext.utils.findMethod
import org.matrix.chromext.utils.hookAfter
Expand All @@ -18,44 +19,50 @@ object ContextMenuHook : BaseHook() {

override fun init() {

var contextMenuHook: Unhook? = null
findMethod(View::class.java) { name == "startActionMode" && parameterTypes.size == 2 }
// public ActionMode startActionMode (ActionMode.Callback callback,
// int type)
.hookBefore {
if (it.args[1] as Int == ActionMode.TYPE_FLOATING) {
val isChromeXt = isChromeXtFrontEnd(Chrome.getUrl())
contextMenuHook?.unhook()
contextMenuHook =
findMethod(it.args[0]::class.java) { name == "onCreateActionMode" }
// public abstract boolean onCreateActionMode (ActionMode mode, Menu menu)
.hookAfter {
val mode = it.args[0] as ActionMode
val menu = it.args[1] as Menu
val erudaMenu = menu.add(Menu.NONE, Menu.NONE, Menu.NONE, "Eruda console")
if (isChromeXt) {
erudaMenu.setTitle("Developer tools")
}
erudaMenu.setOnMenuItemClickListener(
MenuItem.OnMenuItemClickListener {
if (WebViewHook.isInit) {
val webSettings = (Chrome.getTab() as WebView).settings
val javaScriptEnabled = webSettings.javaScriptEnabled
if (!javaScriptEnabled) {
webSettings.javaScriptEnabled = true
Chrome.evaluateJavascript(listOf(Local.initChromeXt))
}
}
if (isChromeXt) {
Listener.on("inspectPages")
} else {
Chrome.evaluateJavascript(listOf(Local.openEruda))
}
mode.finish()
true
})
}
val erudaMenuId = 31415926
val clickListnerFactory = { mode: ActionMode, showConsole: Boolean ->
MenuItem.OnMenuItemClickListener {
if (WebViewHook.isInit) {
val webSettings = (Chrome.getTab() as WebView).settings
val javaScriptEnabled = webSettings.javaScriptEnabled
if (!javaScriptEnabled) {
webSettings.javaScriptEnabled = true
Chrome.evaluateJavascript(listOf(Local.initChromeXt))
}
}
if (showConsole) {
Chrome.evaluateJavascript(listOf(Local.openEruda))
} else {
Listener.on("inspectPages")
}
mode.finish()
true
}
}

var actionModeFinder: Unhook? = null
actionModeFinder =
findMethod(View::class.java) { name == "startActionMode" && parameterTypes.size == 2 }
// public ActionMode startActionMode (ActionMode.Callback callback, int type)
.hookBefore {
if (it.args[1] as Int != ActionMode.TYPE_FLOATING) return@hookBefore
actionModeFinder?.unhook()
findMethod(it.args[0]::class.java) { name == "onCreateActionMode" }
// public abstract boolean onCreateActionMode (ActionMode mode, Menu menu)
.hookAfter {
val showConsole = !isChromeXtFrontEnd(Chrome.getUrl())
val mode = it.args[0] as ActionMode
val menu = it.args[1] as Menu
if (menu.findItem(erudaMenuId) != null) return@hookAfter
val titleId =
if (showConsole) R.string.main_menu_eruda_console
else R.string.main_menu_developer_tools
val erudaMenu = menu.add(titleId)
val mId = erudaMenu::class.java.getDeclaredField("mId")
mId.setAccessible(true)
mId.set(erudaMenu, erudaMenuId)
erudaMenu.setOnMenuItemClickListener(clickListnerFactory(mode, showConsole))
}
}
}
}

0 comments on commit fc86daa

Please sign in to comment.