js: simplify the exec call
This commit is contained in:
@@ -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' });
|
||||||
console.log(stdout);
|
if (errno === 0) {
|
||||||
|
// success
|
||||||
|
console.log(stdout);
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### fullScreen
|
### fullScreen
|
||||||
|
|||||||
26
js/index.js
26
js/index.js
@@ -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,19 +27,18 @@ 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);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function fullScreen(isFullScreen) {
|
export function fullScreen(isFullScreen) {
|
||||||
ksu.fullScreen(isFullScreen);
|
ksu.fullScreen(isFullScreen);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function toast(message) {
|
export function toast(message) {
|
||||||
ksu.toast(message);
|
ksu.toast(message);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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": {
|
||||||
|
|||||||
@@ -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,25 +110,17 @@ 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 stdout = result.out.joinToString(separator = "\n")
|
||||||
val jsCode =
|
val stderr = result.err.joinToString(separator = "\n")
|
||||||
"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 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)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user