js: simplify the exec call
This commit is contained in:
@@ -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
|
||||
|
||||
26
js/index.js
26
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);
|
||||
}
|
||||
ksu.toast(message);
|
||||
}
|
||||
|
||||
@@ -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": {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user