<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>我的插件</title>
<script>
// 核心功能库
const Core = {
// 输出日志
outputLog: function(port, pluginName, text) {
const url = `http://127.0.0.1:${port}/output`;
const params = new URLSearchParams({
name: pluginName,
text: text
});
fetch(url + '?' + params.toString());
},
// 发送群消息
sendGroupMessage: function(port, pluginName, msg, group, qq) {
const url = `http://127.0.0.1:${port}/sendgroup`;
const params = new URLSearchParams({
name: pluginName,
msg: msg,
group: group,
qq: qq
});
fetch(url + '?' + params.toString());
},
// 获取消息详情
getMessage: function(port, msgid) {
return new Promise((resolve, reject) => {
const url = `http://127.0.0.1:${port}/getmsg?msgid=${msgid}`;
fetch(url)
.then(response => response.json())
.then(data => resolve(data))
.catch(error => reject(error));
});
},
// HTTP GET请求(带参数)
httpGet: function(url, params) {
return new Promise((resolve, reject) => {
try {
const paramObj = JSON.parse(params);
const query = new URLSearchParams(paramObj).toString();
fetch(`${url}?${query}`)
.then(response => response.text())
.then(data => resolve(data))
.catch(error => reject(error));
} catch (e) {
reject("参数解析错误");
}
});
},
// 解析JSON
parseJson: function(jsonText) {
try {
return JSON.parse(jsonText);
} catch (e) {
return null;
}
},
// 从JSON对象获取文本
getJsonText: function(jsonObj, path) {
const keys = path.split('.');
let value = jsonObj;
for (const key of keys) {
if (value && typeof value === 'object' && key in value) {
value = value[key];
} else {
return "";
}
}
return value || "";
},
// 从JSON对象获取整数
getJsonNumber: function(jsonObj, path) {
const value = this.getJsonText(jsonObj, path);
return isNaN(Number(value)) ? 0 : Number(value);
},
// 从JSON对象获取布尔值
getJsonBoolean: function(jsonObj, path) {
const value = this.getJsonText(jsonObj, path);
return value === true || value === "true";
},
// 取左边字符
leftText: function(str, length) {
try {
return str.substring(0, parseInt(length));
} catch (e) {
return "";
}
},
// 取右边字符
rightText: function(str, length) {
try {
return str.substring(str.length - parseInt(length));
} catch (e) {
return "";
}
},
// 取中间文本
midText: function(str, start, end) {
try {
return str.substring(parseInt(start), parseInt(end));
} catch (e) {
return "";
}
},
// 文本替换
textReplace: function(str, from, to) {
try {
return str.split(from).join(to);
} catch (e) {
return str;
}
}
};
</script>
<script>
// 插件信息
const pluginInfo = {
name: "我的插件",
author: "开发者",
version: "1.0.0",
description: "由Blockly生成的插件"
};
// 分布式处理函数
function handlePluginInfoRequest() {
// 返回插件信息
return JSON.stringify(pluginInfo);
}
function handleGroupMessage(port, msgid) {
return new Promise((resolve) => {
// 获取消息详情
Core.getMessage(port, msgid).then(data => {
// 自动解析消息参数为变量
const bot = data.bot; // 机qi人QQ
const msg = data.msg; // 消息内容
const group = data.group; // 群号
const sender = data.sender; // 发送者QQ
const req = data.req; // 请求类型
const random = data.random; // 随机数
// 用户逻辑开始
try {
resolve("消息处理完成");
} catch (e) {
resolve("消息处理出错: " + e);
}
// 用户逻辑结束
}).catch(error => {
Core.outputLog(port, pluginInfo.name, "获取消息失败: " + error);
resolve("获取消息失败");
});
});
}
// 主函数
function main() {
const params = new URLSearchParams(window.location.search);
const type = params.get('type');
const port = params.get('port');
const msgid = params.get('msgid');
// 分布式处理请求
if (type === '0') {
// 处理插件信息请求
const result = handlePluginInfoRequest();
document.write(result);
} else if (type === '1' && msgid) {
// 处理群消息
handleGroupMessage(port, msgid).then(result => {
// 可选:返回处理结果
// document.write(result);
});
}
}
// 启动
main();
</script>
</head>
<body>
</body>
</html>