forked from kkevsekk1/kkevsekk1.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
334 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# base64编码 | ||
# Base64编码 | ||
|
||
提供基本的Base64转换函数。 | ||
提供基本的 Base64 转换函数。 | ||
|
||
# $base64.encode(str[, encoding = 'utf-8') | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# 协程 | ||
|
||
见 App 中 `示例代码 / 协程` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,61 @@ | ||
# crypto | ||
|
||
$crypto模块提供了对称加密(例如AES)、非对称加密(例如RSA)、消息摘要(例如MD5, SHA)等支持。 | ||
$crypto 模块提供了对称加密(例如AES)、非对称加密(例如RSA)、消息摘要(例如MD5, SHA)等支持。 | ||
|
||
见 app 中的 `示例 / 消息处理(加密、摘要、编码)` | ||
|
||
|
||
```js | ||
let message = "未加密字符串"; | ||
log("明文: ", message); | ||
|
||
// 密钥,由于AES等算法要求是16位的倍数,我们这里用一个16位的密钥 | ||
let key = new $crypto.Key("password12345678"); | ||
log("密钥: ", key); | ||
|
||
// AES加密 | ||
let aes = $crypto.encrypt(message, key, "AES/ECB/PKCS5padding"); | ||
log("AES加密后二进制数据: ", aes); | ||
log("AES解密: ", $crypto.decrypt(aes, key, "AES/ECB/PKCS5padding", {output: 'string'})); | ||
|
||
// RSA加密 | ||
|
||
// 生成RSA密钥 | ||
let keyPair = $crypto.generateKeyPair("RSA"); | ||
log("密钥对: ", keyPair); | ||
|
||
// 使用私钥加密 | ||
let rsa = $crypto.encrypt(message, keyPair.privateKey, "RSA/ECB/PKCS1padding"); | ||
log("RSA私钥加密后二进制数据: ", rsa); | ||
|
||
// 使用公钥解密 | ||
log("RSA公钥解密: ", $crypto.decrypt(rsa, keyPair.publicKey, "RSA/ECB/PKCS1padding", {output: 'string'})); | ||
|
||
``` | ||
|
||
|
||
|
||
```js | ||
|
||
// 字符串消息摘要 | ||
let message = "Hello, Autox.js"; | ||
// 输出各种消息摘要算法结果的hex值 | ||
log("字符串: ", message); | ||
log("MD5: ", $crypto.digest(message, "MD5")); | ||
log("SHA1: ", $crypto.digest(message, "SHA-1")); | ||
log("SHA256: ", $crypto.digest(message, "SHA-256")); | ||
|
||
// 输出各种消息摘要算法结果的base64值 | ||
log("MD5 [base64]: ", $crypto.digest(message, "MD5", {output: 'base64'})); | ||
log("SHA1 [base64]: ", $crypto.digest(message, "SHA-1", {output: 'base64'})); | ||
log("SHA256 [base64]: ", $crypto.digest(message, "SHA-256", {output: 'base64'})); | ||
|
||
|
||
// 文件消息摘要 | ||
let file = "/sdcard/脚本/_test_for_message_digest.js" | ||
// 写入文件内容,提供为后续计算MD5等 | ||
$files.write(file, "Test!"); | ||
log("文件: ", file); | ||
log("MD5: ", $crypto.digest(file, "MD5", {input: 'file'})); | ||
log("SHA1: ", $crypto.digest(file, "SHA-1", {input: 'file'})); | ||
log("SHA256: ", $crypto.digest(file, "SHA-256", {input: 'file'})); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
# WebView 与 HTML | ||
|
||
```js | ||
"ui"; | ||
ui.layout( | ||
<vertical> | ||
<horizontal bg="#c7edcc" gravity="center" h="auto"> | ||
<button text="网络冲浪" id="surfInternetBtn" style="Widget.AppCompat.Button.Colored" w="auto" /> | ||
<button text="记忆翻牌" id="loadLocalHtmlBtn" style="Widget.AppCompat.Button.Colored" w="auto" /> | ||
<button text="控制台" id="consoleBtn" style="Widget.AppCompat.Button.Colored" w="auto" /> | ||
</horizontal> | ||
<vertical h="*" w="*"> | ||
<webview id="webView" layout_below="title" w="*" h="*" /> | ||
</vertical> | ||
</vertical> | ||
); | ||
|
||
function callJavaScript(webViewWidget, script, callback) { | ||
try { | ||
console.assert(webViewWidget != null, "webView控件为空"); | ||
//console.log(script.toString()) | ||
webViewWidget.evaluateJavascript("javascript:" + script, new JavaAdapter(android.webkit.ValueCallback, { | ||
onReceiveValue: (val) => { | ||
if (callback) { | ||
callback(val); | ||
} | ||
} | ||
})); | ||
} catch (e) { | ||
console.error("执行JavaScript失败"); | ||
console.trace(e); | ||
} | ||
} | ||
|
||
function AutoX() { | ||
let getAutoXFrame = () => { | ||
let bridgeFrame = document.getElementById("AutoXFrame"); | ||
if (!bridgeFrame) { | ||
bridgeFrame = document.createElement('iframe'); | ||
bridgeFrame.id = "AutoXFrame"; | ||
bridgeFrame.style = "display: none"; | ||
document.body.append(bridgeFrame); | ||
} | ||
return bridgeFrame; | ||
}; | ||
const h5Callbackers = {}; | ||
let h5CallbackIndex = 1; | ||
let setCallback = (callback) => { | ||
let callId = h5CallbackIndex++; | ||
h5Callbackers[callId] = { | ||
"callback": callback | ||
}; | ||
return callId; | ||
}; | ||
let getCallback = (callId) => { | ||
let callback = h5Callbackers[callId]; | ||
if (callback) { | ||
delete h5Callbackers[callId]; | ||
} | ||
return callback; | ||
}; | ||
|
||
function invoke(cmd, params, callback) { | ||
let callId = null; | ||
try { | ||
let paramsStr = JSON.stringify(params); | ||
let AutoXFrame = getAutoXFrame(); | ||
callId = setCallback(callback); | ||
AutoXFrame.src = "jsbridge://" + cmd + "/" + callId + "/" + encodeURIComponent(paramsStr); | ||
} catch (e) { | ||
if (callId) { | ||
getCallback(callId); | ||
} | ||
console.trace(e); | ||
} | ||
}; | ||
let callback = (data) => { | ||
let callId = data.callId; | ||
let params = data.params; | ||
let callbackFun = getCallback(callId); | ||
if (callbackFun) { | ||
callbackFun.callback(params); | ||
} | ||
}; | ||
return { | ||
invoke: invoke, | ||
callback: callback | ||
}; | ||
}; | ||
function bridgeHandler_handle(cmd, params) { | ||
console.log('bridgeHandler处理 cmd=%s, params=%s', cmd, JSON.stringify(params)); | ||
let fun = this[cmd]; | ||
if (!fun) { | ||
throw new Error("cmd= " + cmd + " 没有定义实现"); | ||
} | ||
let ret = fun(params) | ||
return ret; | ||
} | ||
function mFunction(params) { | ||
toastLog(params.toString()); | ||
device.vibrate(120); | ||
return files.isDir('/storage/emulated/0/Download')//'toast提示成功'; | ||
} | ||
function webViewExpand_init(webViewWidget) { | ||
webViewWidget.webViewClient = new JavaAdapter(android.webkit.WebViewClient, { | ||
onPageFinished: (webView, curUrl) => { | ||
try { | ||
// 注入 AutoX | ||
callJavaScript(webView, AutoX.toString() + ";var auto0 = AutoX();auto0.invoke('mFunction','This is AutoX!',(data) => {console.log('接收到callback1:' + JSON.stringify(data));});", null); | ||
} catch (e) { | ||
console.trace(e) | ||
} | ||
}, | ||
shouldOverrideUrlLoading: (webView, request) => { | ||
let url = ''; | ||
try { | ||
url = (request.a && request.a.a) || (request.url); | ||
if (url instanceof android.net.Uri) { | ||
url = url.toString(); | ||
} | ||
if (url.indexOf("jsbridge://") == 0) { | ||
let uris = url.split("/"); | ||
let cmd = uris[2]; | ||
let callId = uris[3]; | ||
let params = java.net.URLDecoder.decode(uris[4], "UTF-8"); | ||
console.log('AutoX处理JavaScript调用请求: callId=%s, cmd=%s, params=%s', callId, cmd, params); | ||
let result = null; | ||
try { | ||
result = bridgeHandler_handle(cmd, JSON.parse(params)); | ||
} catch (e) { | ||
console.trace(e); | ||
result = { | ||
message: e.message | ||
}; | ||
} | ||
result = result || {}; | ||
webView.loadUrl("javascript:auto0.callback({'callId':" + callId + ", 'params': " + JSON.stringify(result) + "});"); | ||
} else if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("file://") || url.startsWith("ws://") || url.startsWith("wss://")) { | ||
webView.loadUrl(url); | ||
} else { | ||
} | ||
return true; | ||
} catch (e) { | ||
if (e.javaException instanceof android.content.ActivityNotFoundException) { | ||
webView.loadUrl(url); | ||
} else { | ||
toastLog('无法打开URL: ' + url); | ||
} | ||
console.trace(e); | ||
} | ||
}, | ||
onReceivedError: (webView, webResourceRequest, webResourceError) => { | ||
let url = webResourceRequest.getUrl(); | ||
let errorCode = webResourceError.getErrorCode(); | ||
let description = webResourceError.getDescription(); | ||
console.trace(errorCode + " " + description + " " + url); | ||
} | ||
}); | ||
webViewWidget.webChromeClient = new JavaAdapter(android.webkit.WebChromeClient, { | ||
onConsoleMessage: (msg) => { | ||
console.log("[%s:%s]: %s", msg.sourceId(), msg.lineNumber(), msg.message()); | ||
} | ||
}); | ||
} | ||
webViewExpand_init(ui.webView) | ||
ui.webView.loadUrl("https://wht.im"); | ||
|
||
ui.surfInternetBtn.on("click", () => { | ||
webViewExpand_init(ui.webView); | ||
ui.webView.loadUrl("https://wht.im"); | ||
}); | ||
ui.consoleBtn.on("click", () => { | ||
app.startActivity("console"); | ||
}); | ||
ui.loadLocalHtmlBtn.on('click', () => { | ||
webViewExpand_init(ui.webView); | ||
let path = "file:" + files.path("game.html"); | ||
ui.webView.loadUrl(path); | ||
}); | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# 压缩与解压 | ||
|
||
## 压缩 | ||
|
||
```js | ||
|
||
//压缩文件路径(必须是完整路径) | ||
var filePath = "/sdcard/脚本.7z"; | ||
//目录路径(必须是完整路径) | ||
var dirPath = "/sdcard/脚本"; | ||
//压缩类型 | ||
//支持的压缩类型包括: | ||
// zip 7z bz2 bzip2 tbz2 tbz gz gzip tgz tar wim swm xz txz。 | ||
var type = "7z"; | ||
//压缩密码 | ||
var password = "password" | ||
|
||
//7z加密压缩(若文件已存在则跳过) | ||
//zips.A(type, filePath, dirPath, password) | ||
|
||
//压缩 | ||
switch (zips.A(type, filePath, dirPath)) { | ||
case 0: | ||
toastLog("压缩成功!文件已保存为: " + filePath) | ||
break; | ||
case 1: | ||
toastLog("压缩结束,存在非致命错误(例如某些文件正在被使用,没有被压缩)") | ||
break; | ||
case 2: | ||
toastLog("致命错误") | ||
break; | ||
case 7: | ||
toastLog("命令行错误") | ||
break; | ||
case 8: | ||
toastLog("没有足够内存") | ||
break; | ||
case 255: | ||
toastLog("用户中止操作") | ||
break; | ||
default: toastLog("未知错误") | ||
} | ||
|
||
``` | ||
|
||
|
||
## 解压 | ||
|
||
```js | ||
//压缩文件路径(必须是完整路径) | ||
var filePath = files.path("./bonus.rar"); | ||
//目录路径(必须是完整路径) | ||
var dirPath = "/sdcard/脚本"; | ||
//压缩密码 | ||
var password = "password" | ||
|
||
//支持的解压缩类型包括: | ||
// zip、7z、bz2、bzip2、tbz2、tbz、gz、gzip、tgz、tar、 | ||
// wim、swm、xz、txz以及rar、chm、iso、msi等众多格式。 | ||
|
||
//解压无加密的压缩包(若文件已存在则跳过) | ||
//zips.X(filePath, dirPath) | ||
|
||
//解压加密的压缩包(若文件已存在则跳过) | ||
switch (zips.X(filePath, dirPath, password)) { | ||
case 0: | ||
toastLog("解压缩成功!请到 " + dirPath + " 目录下查看。") | ||
break; | ||
case 1: | ||
toastLog("压缩结束,存在非致命错误(例如某些文件正在被使用,没有被压缩)") | ||
break; | ||
case 2: | ||
toastLog("致命错误") | ||
break; | ||
case 7: | ||
toastLog("命令行错误") | ||
break; | ||
case 8: | ||
toastLog("没有足够内存") | ||
break; | ||
case 255: | ||
toastLog("用户中止操作") | ||
break; | ||
default: toastLog("未知错误") | ||
} | ||
|
||
``` |