js: simplify the exec call

This commit is contained in:
weishu
2024-02-22 22:41:50 +08:00
parent b904680f13
commit d02855a40a
4 changed files with 26 additions and 44 deletions

View File

@@ -20,8 +20,11 @@ options:
```javascript ```javascript
import { exec } from 'kernelsu'; import { exec } from 'kernelsu';
const { stdout, stderr } = await exec('ls -l', { cwd: '/tmp'}); const { errno, stdout, stderr } = await exec('ls -l', { cwd: '/tmp' });
if (errno === 0) {
// success
console.log(stdout); console.log(stdout);
}
``` ```
### fullScreen ### fullScreen

View File

@@ -13,24 +13,13 @@ export function exec(command, options) {
const callbackFuncName = getUniqueCallbackName(); const callbackFuncName = getUniqueCallbackName();
// Define the success callback function // Define the success callback function
window[callbackFuncName] = (stdout, stderr) => { window[callbackFuncName] = (errno, stdout, stderr) => {
resolve({ stdout, stderr }); resolve({ errno, stdout, stderr });
cleanup(callbackFuncName); cleanup(callbackFuncName);
}; };
// Define the failure callback function function cleanup(successName) {
const errorFuncName = callbackFuncName + "_error";
window[errorFuncName] = (error) => {
reject(error);
cleanup(callbackFuncName, errorFuncName);
};
// Cleanup function to remove the callbacks
function cleanup(successName, errorName = successName) {
delete window[successName]; delete window[successName];
if (errorName) {
delete window[errorName];
}
} }
try { try {
@@ -38,11 +27,10 @@ export function exec(command, options) {
command, command,
JSON.stringify(options), JSON.stringify(options),
callbackFuncName, callbackFuncName,
errorFuncName
); );
} catch (error) { } catch (error) {
reject(error); reject(error);
cleanup(callbackFuncName, errorFuncName); cleanup(callbackFuncName);
} }
}); });
} }

View File

@@ -1,6 +1,6 @@
{ {
"name": "kernelsu", "name": "kernelsu",
"version": "1.0.2", "version": "1.0.3",
"description": "Library for KernelSU's module WebUI", "description": "Library for KernelSU's module WebUI",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

View File

@@ -79,16 +79,15 @@ class WebViewInterface(val context: Context, val webView: WebView) {
} }
@JavascriptInterface @JavascriptInterface
fun exec(cmd: String, successCallbackName: String, errorCallbackName: String) { fun exec(cmd: String, callbackFunc: String) {
exec(cmd, null, successCallbackName, errorCallbackName) exec(cmd, null, callbackFunc)
} }
@JavascriptInterface @JavascriptInterface
fun exec( fun exec(
cmd: String, cmd: String,
options: String?, options: String?,
successCallbackName: String, callbackFunc: String
errorCallbackName: String
) { ) {
val opts = if (options == null) JSONObject() else { val opts = if (options == null) JSONObject() else {
JSONObject(options) JSONObject(options)
@@ -111,27 +110,19 @@ class WebViewInterface(val context: Context, val webView: WebView) {
val shell = createRootShell() val shell = createRootShell()
val result = shell.newJob().add(finalCommand.toString()).to(ArrayList(), ArrayList()).exec() 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 stdout = result.out.joinToString(separator = "\n")
val stderr = result.err.joinToString(separator = "\n") val stderr = result.err.joinToString(separator = "\n")
val jsCode = val jsCode =
"javascript: (function() { try { ${successCallbackName}(${JSONObject.quote(stdout)}, ${ "javascript: (function() { try { ${callbackFunc}(${result.code}, ${
JSONObject.quote( JSONObject.quote(
stderr stdout
) )
}); } catch(e) { console.error(e); } })();" }, ${JSONObject.quote(stderr)}); } catch(e) { console.error(e); } })();"
webView.post { webView.post {
webView.loadUrl(jsCode) webView.loadUrl(jsCode)
} }
} }
}
@JavascriptInterface @JavascriptInterface
fun toast(msg: String) { fun toast(msg: String) {