Skip to content

Commit

Permalink
Implement window.close for Chrome
Browse files Browse the repository at this point in the history
Partially fulfill #180
  • Loading branch information
JingMatrix committed Jul 27, 2024
1 parent f412167 commit 3bd7f80
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Currently, ChromeXt supports almost all [Tampermonkey APIs](https://www.tampermo
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
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
9 changes: 9 additions & 0 deletions app/src/main/assets/GM.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ function GM_addStyle(css) {
}
// Kotlin separator

window.close = new Proxy(window.close, {
apply(_target, _this, _args) {
const ChromeXt = LockedChromeXt.unlock(key);
ChromeXt.dispatch("close");
return Reflect.apply(...arguments);
},
});
// Kotlin separator

const unsafeWindow = window;
// Kotlin separator

Expand Down
32 changes: 32 additions & 0 deletions app/src/main/java/org/matrix/chromext/Listener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import org.matrix.chromext.script.parseScript
import org.matrix.chromext.utils.ERUD_URL
import org.matrix.chromext.utils.Log
import org.matrix.chromext.utils.XMLHttpRequest
import org.matrix.chromext.utils.findMethod
import org.matrix.chromext.utils.invalidUserScriptUrls
import org.matrix.chromext.utils.isChromeXtFrontEnd
import org.matrix.chromext.utils.isDevToolsFrontEnd
Expand Down Expand Up @@ -143,6 +144,37 @@ object Listener {
if (isUserScript(url)) invalidUserScriptUrls.add(url!!)
callback = "if (Symbol.ChromeXt) Symbol.ChromeXt.lock(${Local.key},'${Local.name}');"
}
"close" -> {
val activity = Chrome.getContext()
if (currentTab != null &&
activity::class.java == UserScriptProxy.chromeTabbedActivity &&
!Chrome.isSamsung) {
val tab = Chrome.load("org.chromium.chrome.browser.tab.Tab")
val tabModel = Chrome.load("org.chromium.chrome.browser.tabmodel.TabModel")
val getCurrentTabModel =
findMethod(activity::class.java, true) {
parameterTypes.size == 0 && returnType == tabModel
}
val model = getCurrentTabModel.invoke(activity)!!
val closeTab =
findMethod(model::class.java) {
returnType == Boolean::class.java &&
parameterTypes contentDeepEquals
arrayOf(
tab,
tab,
Boolean::class.java,
Boolean::class.java,
Boolean::class.java,
Int::class.java)
}
closeTab.invoke(model, currentTab, null, false, false, false, 0)
} else {
val msg = "Action close failed with tab ${currentTab} and context ${activity}"
Log.w(msg)
callback = "console.error('ChromeXt Error', ${msg});"
}
}
"focus" -> {
Chrome.updateTab(currentTab)
}
Expand Down

0 comments on commit 3bd7f80

Please sign in to comment.