Skip to content

Commit 008958a

Browse files
jeffpeng3backslashxx
authored andcommitted
js: add moduleInfo method (tiann#2057)
Add a `ksu.moduleInfo()` in JS. resolves tiann#1571 test module's index.html ![image](https://github.com/user-attachments/assets/39920606-452c-4b19-abca-967c25146d6a) The test module: [moduleInfo_test.zip](https://github.com/user-attachments/files/17001977/moduleInfo_test.zip) test module's result: ![image](https://github.com/user-attachments/assets/9dbbd2e8-f7ea-418f-b545-66d33ce1b3ae)
1 parent 7f15dab commit 008958a

File tree

6 files changed

+45
-5
lines changed

6 files changed

+45
-5
lines changed

js/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,12 @@ Show a toast message.
109109
import { toast } from 'kernelsu';
110110
toast('Hello, world!');
111111
```
112+
113+
### moduleInfo
114+
115+
Get Module info.
116+
```javascript
117+
import { moduleInfo } from 'kernelsu';
118+
// print moduleId in console
119+
console.log(moduleInfo());
120+
```

js/index.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ declare function fullScreen(isFullScreen: boolean);
3737

3838
declare function toast(message: string);
3939

40+
declare function moduleInfo(): string;
41+
4042
export {
4143
exec,
4244
spawn,
4345
fullScreen,
44-
toast
46+
toast,
47+
moduleInfo
4548
}

js/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,7 @@ export function fullScreen(isFullScreen) {
113113
export function toast(message) {
114114
ksu.toast(message);
115115
}
116+
117+
export function moduleInfo() {
118+
return ksu.moduleInfo();
119+
}

js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kernelsu",
3-
"version": "1.0.6",
3+
"version": "1.0.7",
44
"description": "Library for KernelSU's module WebUI",
55
"main": "index.js",
66
"types": "index.d.ts",

manager/app/src/main/java/me/weishu/kernelsu/ui/webui/WebUIActivity.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class WebUIActivity : ComponentActivity() {
2929
val prefs = getSharedPreferences("settings", Context.MODE_PRIVATE)
3030
WebView.setWebContentsDebuggingEnabled(prefs.getBoolean("enable_web_debugging", false))
3131

32-
val webRoot = File("/data/adb/modules/${moduleId}/webroot")
32+
val moduleDir = "/data/adb/modules/${moduleId}"
33+
val webRoot = File("${moduleDir}/webroot")
3334
val rootShell = createRootShell(true).also { this.rootShell = it }
3435
val webViewAssetLoader = WebViewAssetLoader.Builder()
3536
.setDomain("mui.kernelsu.org")
@@ -52,7 +53,7 @@ class WebUIActivity : ComponentActivity() {
5253
settings.javaScriptEnabled = true
5354
settings.domStorageEnabled = true
5455
settings.allowFileAccess = false
55-
webviewInterface = WebViewInterface(this@WebUIActivity, this)
56+
webviewInterface = WebViewInterface(this@WebUIActivity, this, moduleDir)
5657
addJavascriptInterface(webviewInterface, "ksu")
5758
setWebViewClient(webViewClient)
5859
loadUrl("https://mui.kernelsu.org/index.html")

manager/app/src/main/java/me/weishu/kernelsu/ui/webui/WebViewInterface.kt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ import androidx.core.view.WindowInsetsCompat
1414
import androidx.core.view.WindowInsetsControllerCompat
1515
import com.topjohnwu.superuser.CallbackList
1616
import com.topjohnwu.superuser.ShellUtils
17+
import me.weishu.kernelsu.ui.util.listModules
1718
import me.weishu.kernelsu.ui.util.createRootShell
1819
import me.weishu.kernelsu.ui.util.withNewRootShell
1920
import org.json.JSONArray
2021
import org.json.JSONObject
2122
import java.util.concurrent.CompletableFuture
23+
import java.io.File
2224

23-
class WebViewInterface(val context: Context, private val webView: WebView) {
25+
class WebViewInterface(val context: Context, private val webView: WebView, private val modDir: String) {
2426

2527
@JavascriptInterface
2628
fun exec(cmd: String): String {
@@ -170,6 +172,27 @@ class WebViewInterface(val context: Context, private val webView: WebView) {
170172
}
171173
}
172174

175+
@JavascriptInterface
176+
fun moduleInfo(): String {
177+
val moduleInfos = JSONArray(listModules())
178+
var currentModuleInfo = JSONObject()
179+
currentModuleInfo.put("moduleDir", modDir)
180+
val moduleId = File(modDir).getName()
181+
for (i in 0 until moduleInfos.length()) {
182+
val currentInfo = moduleInfos.getJSONObject(i)
183+
184+
if (currentInfo.getString("id") != moduleId) {
185+
continue
186+
}
187+
188+
var keys = currentInfo.keys()
189+
for(key in keys) {
190+
currentModuleInfo.put(key, currentInfo.get(key));
191+
}
192+
break;
193+
}
194+
return currentModuleInfo.toString();
195+
}
173196
}
174197

175198
fun hideSystemUI(window: Window) {

0 commit comments

Comments
 (0)