Skip to content

Commit

Permalink
Remove TabModel class
Browse files Browse the repository at this point in the history
Use the Chrome object to store current Tab
  • Loading branch information
JingMatrix committed Jun 8, 2023
1 parent 6725e74 commit eaa245e
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 46 deletions.
9 changes: 9 additions & 0 deletions app/src/main/java/org/matrix/chromext/Chrome.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.matrix.chromext.utils.Log

object Chrome {
private var mContext: WeakReference<Context>? = null
private var currentTab: WeakReference<Any>? = null
var isDev = false
var isEdge = false
var isVivaldi = false
Expand All @@ -31,4 +32,12 @@ object Chrome {
fun load(className: String): Class<*> {
return getContext().getClassLoader().loadClass(className)
}

fun getTab(): Any? {
return currentTab?.get()
}

fun refreshTab(tab: Any?) {
if (tab != null) currentTab = WeakReference(tab)
}
}
25 changes: 14 additions & 11 deletions app/src/main/java/org/matrix/chromext/hook/Menu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import org.matrix.chromext.Chrome
import org.matrix.chromext.DevTools
import org.matrix.chromext.R
import org.matrix.chromext.proxy.MenuProxy
import org.matrix.chromext.proxy.TabModel
import org.matrix.chromext.proxy.UserScriptProxy
import org.matrix.chromext.script.ScriptDbManager
import org.matrix.chromext.script.openEruda
Expand Down Expand Up @@ -45,7 +44,7 @@ object readerMode {
}

val manager =
readerModeManager!!.getDeclaredConstructors()[0].newInstance(TabModel.getTab(), null)
readerModeManager!!.getDeclaredConstructors()[0].newInstance(Chrome.getTab(), null)

mDistillerUrl.setAccessible(true)
mDistillerUrl.set(
Expand All @@ -61,6 +60,10 @@ object readerMode {

object MenuHook : BaseHook() {

private fun getUrl(): String {
return UserScriptProxy.parseUrl(Chrome.getTab()?.invokeMethod { name == "getUrl" }) ?: ""
}

override fun init() {

val proxy = MenuProxy
Expand All @@ -74,7 +77,7 @@ object MenuHook : BaseHook() {
proxy.pageInfoView.getDeclaredConstructors()[0].hookAfter {
val ctx = it.args[0] as Context
ResourceMerge.enrich(ctx)
val url = TabModel.getUrl()
val url = getUrl()
if (!url.startsWith("edge://")) {
val erudaRow =
proxy.pageInfoRowView.getDeclaredConstructors()[0].newInstance(ctx, null) as ViewGroup
Expand Down Expand Up @@ -111,8 +114,8 @@ object MenuHook : BaseHook() {
}
when (ctx.resources.getResourceName(id)) {
"org.matrix.chromext:id/install_script_id" -> {
if (TabModel.getUrl().startsWith("https://raw.githubusercontent.com/")) {
Download.start(TabModel.getUrl(), "UserScript/script.js") {
if (getUrl().startsWith("https://raw.githubusercontent.com/")) {
Download.start(getUrl(), "UserScript/script.js") {
ScriptDbManager.on("installScript", it)
}
} else {
Expand All @@ -122,7 +125,7 @@ object MenuHook : BaseHook() {
"org.matrix.chromext:id/developer_tools_id" -> DevTools.start()
"org.matrix.chromext:id/eruda_console_id" -> UserScriptProxy.evaluateJavascript(openEruda)
"${ctx.getPackageName()}:id/reload_menu_id" -> {
return ScriptDbManager.on("userAgentSpoof", TabModel.getUrl()) != null
return ScriptDbManager.on("userAgentSpoof", getUrl()) != null
}
}
return false
Expand Down Expand Up @@ -169,7 +172,7 @@ object MenuHook : BaseHook() {
val ctx = mContext.get(it.thisObject) as Context
ResourceMerge.enrich(ctx)
val menu = it.args[0] as Menu
TabModel.refresh(it.args[1])
Chrome.refreshTab(it.args[1])

if (menu.size() <= 20 ||
!(it.args[2] as Boolean) ||
Expand Down Expand Up @@ -198,12 +201,12 @@ object MenuHook : BaseHook() {
@Suppress("UNCHECKED_CAST")
val items = mItems.get(menu) as ArrayList<MenuItem>

if (TabModel.getUrl().endsWith("/ChromeXt/")) {
if (getUrl().endsWith("/ChromeXt/")) {
// Drop the Eruda console menu
items.removeLast()
}

if (TabModel.getUrl().endsWith(".user.js")) {
if (getUrl().endsWith(".user.js")) {
// Drop the Eruda console and the Dev Tools menus
items.removeLast()
items.removeLast()
Expand Down Expand Up @@ -239,7 +242,7 @@ object MenuHook : BaseHook() {
subType.getDeclaredFields().find { it.getType() == proxy.propertyModel } !=
null) {
readerMode.init(subType)
TabModel.refresh(it.thisObject)
Chrome.refreshTab(it.thisObject)
findReaderHook!!.unhook()
}
}
Expand All @@ -256,7 +259,7 @@ object MenuHook : BaseHook() {
// // public void release(boolean allowRefresh)
// .hookBefore {
// if (it.args[0] as Boolean) {
// it.args[0] = ScriptDbManager.on("userAgentSpoof", TabModel.getUrl()) == null
// it.args[0] = ScriptDbManager.on("userAgentSpoof", getUrl()) == null
// }
// }
// }
Expand Down
12 changes: 2 additions & 10 deletions app/src/main/java/org/matrix/chromext/hook/UserScript.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.matrix.chromext.hook
import org.json.JSONObject
import org.matrix.chromext.Chrome
import org.matrix.chromext.DEV_FRONT_END
import org.matrix.chromext.proxy.TabModel
import org.matrix.chromext.proxy.UserScriptProxy
import org.matrix.chromext.script.ScriptDbManager
import org.matrix.chromext.utils.Log
Expand All @@ -26,17 +25,10 @@ object UserScriptHook : BaseHook() {
val cosmeticFilter =
ctx.assets.open("cosmetic-filter.js").bufferedReader().use { it.readText() }

// proxy.tabModelJniBridge.getDeclaredConstructors()[0].hookAfter {
// TabModel.update(it.thisObject)
// }

// findMethod(proxy.tabModelJniBridge) { name == "destroy" }
// .hookBefore { TabModel.dropModel(it.thisObject) }

findMethod(proxy.tabWebContentsDelegateAndroidImpl) { name == "onUpdateUrl" }
// public void onUpdateUrl(GURL url)
.hookAfter {
TabModel.refresh(proxy.mTab.get(it.thisObject))
Chrome.refreshTab(proxy.mTab.get(it.thisObject))
val url = proxy.parseUrl(it.args[0])!!
proxy.evaluateJavascript("globalThis.ChromeXt=console.debug.bind(console);")
if (url.endsWith(".user.js")) {
Expand Down Expand Up @@ -74,7 +66,7 @@ object UserScriptHook : BaseHook() {
runCatching {
val callback = ScriptDbManager.on(action, payload)
if (callback != null) {
TabModel.refresh(proxy.mTab.get(it.thisObject))
Chrome.refreshTab(proxy.mTab.get(it.thisObject))
proxy.evaluateJavascript(callback)
}
}
Expand Down
21 changes: 0 additions & 21 deletions app/src/main/java/org/matrix/chromext/proxy/TabModel.kt

This file was deleted.

3 changes: 1 addition & 2 deletions app/src/main/java/org/matrix/chromext/proxy/UserScript.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ object UserScriptProxy {

val gURL = Chrome.load("org.chromium.url.GURL")
val loadUrlParams = Chrome.load("org.chromium.content_public.browser.LoadUrlParams")
// val tabModelJniBridge = Chrome.load("org.chromium.chrome.browser.tabmodel.TabModelJniBridge")
val tabWebContentsDelegateAndroidImpl =
Chrome.load("org.chromium.chrome.browser.tab.TabWebContentsDelegateAndroidImpl")
val navigationControllerImpl =
Expand All @@ -35,7 +34,7 @@ object UserScriptProxy {
private val mSpec = gURL.getDeclaredField("a")

private fun loadUrl(url: String) {
loadUrl.invoke(TabModel.getTab(), newLoadUrlParams(url))
loadUrl.invoke(Chrome.getTab(), newLoadUrlParams(url))
}

fun newLoadUrlParams(url: String): Any {
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/org/matrix/chromext/script/Manager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import org.matrix.chromext.DevTools
import org.matrix.chromext.hook.UserScriptHook
import org.matrix.chromext.hook.WebViewHook
import org.matrix.chromext.proxy.ERUD_URL
import org.matrix.chromext.proxy.TabModel
import org.matrix.chromext.proxy.UserScriptProxy
import org.matrix.chromext.utils.Download
import org.matrix.chromext.utils.Log
Expand Down Expand Up @@ -148,7 +147,7 @@ object ScriptDbManager {
"userAgentSpoof" -> {
val loadUrlParams = UserScriptProxy.newLoadUrlParams(payload)
if (UserScriptProxy.userAgentHook(payload, loadUrlParams)) {
UserScriptProxy.loadUrl.invoke(TabModel.getTab(), loadUrlParams)
UserScriptProxy.loadUrl.invoke(Chrome.getTab(), loadUrlParams)
callback = "console.log('User-Agent spoofed');"
}
}
Expand Down

0 comments on commit eaa245e

Please sign in to comment.