Skip to content

Commit

Permalink
Implement window.close for WebView browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
JingMatrix committed Jul 27, 2024
1 parent 12a9773 commit ff35a9e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ Currently, ChromeXt supports almost all [Tampermonkey APIs](https://www.tampermo
3. @include = @match, @exclude
4. @run-at: document-start, document-end, document-idle (the default and fallback value)
5. @grant: GM_addStyle, GM_addElement, GM_xmlhttpRequest, GM_openInTab, GM_registerMenuCommand (shown in the `Resources` panel of eruda), GM_unregisterMenuCommand, GM_download, unsafeWindow (= window)
6. @grant: GM_setValue, GM_getValue (less powerful than GM.getValue), GM_listValues, GM_addValueChangeListener, GM_removeValueChangeListener, GM_setClipboard, GM_cookie, GM_notification
6. @grant: GM_setValue, GM_getValue (less powerful than GM.getValue), GM_listValues, GM_addValueChangeListener, GM_removeValueChangeListener, GM_setClipboard, GM_cookie, GM_notification, window.close
7. @require, @resource (without [Subresource Integrity](https://www.tampermonkey.net/documentation.php#api:Subresource_Integrity))
8. window.close (implemented for most Chromium based browsers)

These APIs are implemented differently from the official ones, please refer to the source files
[Local.kt](app/src/main/java/org/matrix/chromext/script/Local.kt) and
Expand Down
15 changes: 11 additions & 4 deletions app/src/main/java/org/matrix/chromext/Listener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,26 @@ object Listener {
.onFailure { callback(null) }
}

fun startAction(text: String, currentTab: Any? = null) {
fun startAction(text: String, currentTab: Any? = null, auxObject: Any? = null) {
runCatching {
val data = JSONObject(text)
val action = data.getString("action")
val key = data.getDouble("key")
val payload = data.optString("payload")
if (checkPermisson(action, key, currentTab)) {
val callback = on(action, payload, currentTab)
val callback = on(action, payload, currentTab, auxObject)
if (callback != null) Chrome.evaluateJavascript(listOf(callback), currentTab)
}
}
.onFailure { Log.i("${it::class.java.name}: startAction fails with " + text) }
}

fun on(action: String, payload: String = "", currentTab: Any? = null): String? {
fun on(
action: String,
payload: String = "",
currentTab: Any? = null,
auxObject: Any? = null
): String? {
var callback: String? = null
when (action) {
"copy" -> {
Expand All @@ -147,7 +152,9 @@ object Listener {
}
"close" -> {
val activity = Chrome.getContext()
if (Chrome.isSamsung &&
if (WebViewHook.isInit && currentTab != null && auxObject != null) {
auxObject.invokeMethod(currentTab) { name == "onCloseWindow" }
} else if (Chrome.isSamsung &&
currentTab != null &&
activity::class.java ==
Chrome.load("com.sec.android.app.sbrowser.SBrowserMainActivity")) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/matrix/chromext/hook/WebView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ object WebViewHook : BaseHook() {
}
?.get()
} else Chrome.getTab()
Listener.startAction(message, webView)
Listener.startAction(message, webView, chromeClient)
} else {
Log.d(messageLevel.toString() + ": [${sourceId}@${lineNumber}] ${message}")
}
Expand Down

0 comments on commit ff35a9e

Please sign in to comment.