From d02855a40a111a43d820dc8e203fcaf269acbb12 Mon Sep 17 00:00:00 2001 From: weishu Date: Thu, 22 Feb 2024 22:41:50 +0800 Subject: [PATCH] js: simplify the exec call --- js/README.md | 7 ++-- js/index.js | 26 ++++---------- js/package.json | 2 +- .../me/weishu/kernelsu/ui/screen/WebScreen.kt | 35 +++++++------------ 4 files changed, 26 insertions(+), 44 deletions(-) diff --git a/js/README.md b/js/README.md index e8fa2bb0..e81dae38 100644 --- a/js/README.md +++ b/js/README.md @@ -20,8 +20,11 @@ options: ```javascript import { exec } from 'kernelsu'; -const { stdout, stderr } = await exec('ls -l', { cwd: '/tmp'}); -console.log(stdout); +const { errno, stdout, stderr } = await exec('ls -l', { cwd: '/tmp' }); +if (errno === 0) { + // success + console.log(stdout); +} ``` ### fullScreen diff --git a/js/index.js b/js/index.js index a261d81d..6e7eaa7d 100644 --- a/js/index.js +++ b/js/index.js @@ -13,24 +13,13 @@ export function exec(command, options) { const callbackFuncName = getUniqueCallbackName(); // Define the success callback function - window[callbackFuncName] = (stdout, stderr) => { - resolve({ stdout, stderr }); + window[callbackFuncName] = (errno, stdout, stderr) => { + resolve({ errno, stdout, stderr }); cleanup(callbackFuncName); }; - // Define the failure callback function - const errorFuncName = callbackFuncName + "_error"; - window[errorFuncName] = (error) => { - reject(error); - cleanup(callbackFuncName, errorFuncName); - }; - - // Cleanup function to remove the callbacks - function cleanup(successName, errorName = successName) { + function cleanup(successName) { delete window[successName]; - if (errorName) { - delete window[errorName]; - } } try { @@ -38,19 +27,18 @@ export function exec(command, options) { command, JSON.stringify(options), callbackFuncName, - errorFuncName ); } catch (error) { reject(error); - cleanup(callbackFuncName, errorFuncName); + cleanup(callbackFuncName); } }); } export function fullScreen(isFullScreen) { - ksu.fullScreen(isFullScreen); + ksu.fullScreen(isFullScreen); } export function toast(message) { - ksu.toast(message); -} \ No newline at end of file + ksu.toast(message); +} diff --git a/js/package.json b/js/package.json index d06a4f86..f5fce642 100644 --- a/js/package.json +++ b/js/package.json @@ -1,6 +1,6 @@ { "name": "kernelsu", - "version": "1.0.2", + "version": "1.0.3", "description": "Library for KernelSU's module WebUI", "main": "index.js", "scripts": { diff --git a/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/WebScreen.kt b/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/WebScreen.kt index 0579634e..75a3e187 100644 --- a/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/WebScreen.kt +++ b/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/WebScreen.kt @@ -79,16 +79,15 @@ class WebViewInterface(val context: Context, val webView: WebView) { } @JavascriptInterface - fun exec(cmd: String, successCallbackName: String, errorCallbackName: String) { - exec(cmd, null, successCallbackName, errorCallbackName) + fun exec(cmd: String, callbackFunc: String) { + exec(cmd, null, callbackFunc) } @JavascriptInterface fun exec( cmd: String, options: String?, - successCallbackName: String, - errorCallbackName: String + callbackFunc: String ) { val opts = if (options == null) JSONObject() else { JSONObject(options) @@ -111,25 +110,17 @@ class WebViewInterface(val context: Context, val webView: WebView) { val shell = createRootShell() val result = shell.newJob().add(finalCommand.toString()).to(ArrayList(), ArrayList()).exec() - if (!result.isSuccess) { - val jsCode = - "javascript: (function() { try { ${errorCallbackName}(${result.code}); } catch(e) { console.error(e); } })();" - webView.post { - webView.loadUrl(jsCode) - } - } else { - val stdout = result.out.joinToString(separator = "\n") - val stderr = result.err.joinToString(separator = "\n") + val stdout = result.out.joinToString(separator = "\n") + val stderr = result.err.joinToString(separator = "\n") - val jsCode = - "javascript: (function() { try { ${successCallbackName}(${JSONObject.quote(stdout)}, ${ - JSONObject.quote( - stderr - ) - }); } catch(e) { console.error(e); } })();" - webView.post { - webView.loadUrl(jsCode) - } + val jsCode = + "javascript: (function() { try { ${callbackFunc}(${result.code}, ${ + JSONObject.quote( + stdout + ) + }, ${JSONObject.quote(stderr)}); } catch(e) { console.error(e); } })();" + webView.post { + webView.loadUrl(jsCode) } }