Skip to content

Commit

Permalink
支持自定义浏览器打开二维码中的链接
Browse files Browse the repository at this point in the history
  • Loading branch information
xh321 committed Mar 10, 2024
1 parent af45a21 commit f7cbef0
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 66 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ LiteLoaderQQNT插件,快速解析聊天中的图片二维码。
**版本不兼容提示**:从1.3.3起,插件已适配1.0版本以上`LiteLoaderQQNT`框架,同时不再兼容旧版框架,请遵循[安装方法](https://liteloaderqqnt.github.io/guide/install.html)更新框架。





也可以clone或下载zip文件解压,保留文件夹结构(文件夹名称为`插件名`,内容为github上的内容),将文件夹移动至`LiteLoaderQQNT数据目录/plugins/`下面,重启QQNT即可。

直接在聊天界面直接右键图片,或者双击聊天中的图片,在新弹出的图片预览界面对图片进行右键,可发现解析二维码菜单,点击即可。
Expand All @@ -24,6 +21,8 @@ LiteLoaderQQNT插件,快速解析聊天中的图片二维码。
*注意:如果图片过大,解码时可能会比较慢,这是正常现象,所以建议不要解析过大的图片。*


你可以配置解析到链接二维码时用自定义浏览器打开。此项暂未提供配置界面,需要你手动修改配置文件:前往 `LiteLoaderQQNT数据目录\qr_decode` 中找到 `config.json` 配置文件(至少在安装插件后开过一次QQ才会有这个文件),修改其中的 `browser` 字段为你的浏览器可执行文件完整路径(例如 `C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe` ,注意路径要写双斜杠,或者反斜杠 `/` ),配置此项后,解析到包含链接的二维码时,若点击打开连接,则会采用你指定的浏览器打开。


整体项目参考了[繁化姬](https://github.com/qianxu2001/LiteLoaderQQNT-Plugin-Fanhuaji),在此表示感谢。

Expand Down
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "二维码解析",
"slug": "qr_code",
"description": "对聊天消息中的二维码进行解析",
"version": "1.3.7",
"version": "1.3.8",
"icon": "./icon.png",
"authors": [
{
Expand All @@ -16,7 +16,7 @@
"repo": "xh321/LiteLoaderQQNT-QR-Decode",
"branch": "master",
"release": {
"tag": "1.3.7"
"tag": "1.3.8"
}
},
"platform": ["win32", "darwin", "linux"],
Expand Down
138 changes: 77 additions & 61 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,89 @@
const { ipcMain, dialog, shell, clipboard } = require("electron");

const exec = require("child_process").exec;
var defaultBrowser = LiteLoader.api.config.get("qr_decode", {
browser: "",
});

onLoad();

function openUrl(url) {
if (
(defaultBrowser?.browser ?? "") != "" &&
fs.existsSync(defaultBrowser.browser)
) {
var cmd = '"' + defaultBrowser.browser + '"' + ' "' + url + '"';
exec(cmd);
} else {
shell.openExternal(url);
}
}

function onLoad() {
ipcMain.handle("LiteLoader.qr_decode.showFailed", (_, data) => {
dialog.showMessageBox({
type: "error",
title: "提示",
message:
"这可能不是一个有效的二维码,解码失败。\n错误信息为:" +
data?.stack,
buttons: ["确定"]
});
ipcMain.handle("LiteLoader.qr_decode.showFailed", (_, data) => {
dialog.showMessageBox({
type: "error",
title: "提示",
message:
"这可能不是一个有效的二维码,解码失败。\n错误信息为:" + data?.stack,
buttons: ["确定"],
});
});

ipcMain.handle("LiteLoader.qr_decode.showResult", (_, data) => {
var content = data;
ipcMain.handle("LiteLoader.qr_decode.showResult", (_, data) => {
var content = data;

if (content == null) {
dialog.showMessageBox({
type: "warning",
title: "提示",
message: "这可能不是一个有效的二维码,解码失败。",
buttons: ["确定"]
});
return;
}
if (content == null) {
dialog.showMessageBox({
type: "warning",
title: "提示",
message: "这可能不是一个有效的二维码,解码失败。",
buttons: ["确定"],
});
return;
}

var isUrl =
/^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\*\+,;=.]+$/.test(
content
);
var isUrl =
/^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\*\+,;=.]+$/.test(
content
);

var btns = ["复制", "什么都不做"];
var cancelId = 1;
if (isUrl) {
btns.unshift("打开链接");
cancelId = 2;
var btns = ["复制", "什么都不做"];
var cancelId = 1;
if (isUrl) {
btns.unshift("打开链接");
cancelId = 2;
}
dialog
.showMessageBox({
type: "info",
title: "提示",
message: `解析结果为:\n${content}\n你想对解析内容做什么?`,
buttons: btns,
cancelId: cancelId,
})
.then((idx) => {
//第一个按钮
if (idx.response == 0) {
if (isUrl) {
//打开URL
openUrl(content);
} else {
//复制内容
clipboard.writeText(content);
}
}
dialog
.showMessageBox({
type: "info",
title: "提示",
message: `解析结果为:\n${content}\n你想对解析内容做什么?`,
buttons: btns,
cancelId: cancelId
})
.then((idx) => {
//第一个按钮
if (idx.response == 0) {
if (isUrl) {
//打开URL
shell.openExternal(content);
} else {
//复制内容
clipboard.writeText(content);
}
}
//第二个按钮
else if (idx.response == 1) {
if (isUrl) {
//复制内容
clipboard.writeText(content);
} else {
//取消
}
} else {
//取消
}
});
});
//第二个按钮
else if (idx.response == 1) {
if (isUrl) {
//复制内容
clipboard.writeText(content);
} else {
//取消
}
} else {
//取消
}
});
});
}

0 comments on commit f7cbef0

Please sign in to comment.