Skip to content

Commit 5441a0c

Browse files
xiaohuohumaxgithub-actions[bot]
authored andcommitted
build: 📦 build user script
1 parent 23b80a9 commit 5441a0c

1 file changed

Lines changed: 52 additions & 19 deletions

File tree

dist/common-qr-code.user.js

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ==UserScript==
22
// @name 图片二维码识别(Common QR Code)
33
// @namespace xiaohuohumax/userscripts/common-qr-code
4-
// @version 1.1.1
4+
// @version 1.2.0
55
// @author xiaohuohumax
66
// @description 右键图片,识别二维码并复制到剪贴板。
77
// @license MIT
@@ -1032,20 +1032,38 @@
10321032
return sweetalert_min$1.exports;
10331033
}
10341034
var sweetalert_minExports = requireSweetalert_min();
1035-
const swal = /* @__PURE__ */ getDefaultExportFromCjs(sweetalert_minExports);
1035+
const Swal = /* @__PURE__ */ getDefaultExportFromCjs(sweetalert_minExports);
10361036
const ID = "common-qr-code";
1037-
const VERSION = "1.1.1";
1037+
const VERSION = "1.2.0";
10381038
async function decodeQrCode(url) {
10391039
return new Promise((resolve, reject) => {
10401040
const image2 = new Image();
10411041
image2.onload = () => {
1042-
var _a;
1042+
const results = [];
10431043
const { width, height } = image2;
10441044
const canvas = new OffscreenCanvas(width, height);
10451045
const context = canvas.getContext("2d");
10461046
context.drawImage(image2, 0, 0);
1047-
const imageData = context.getImageData(0, 0, width, height);
1048-
resolve((_a = jsQR(imageData.data, width, height)) == null ? void 0 : _a.data.replace(/^\s+|\s+$/g, ""));
1047+
while (true) {
1048+
const imageData = context.getImageData(0, 0, width, height);
1049+
const code = jsQR(imageData.data, width, height);
1050+
if (!code) {
1051+
break;
1052+
}
1053+
const result = code.data.replace(/^\s+|\s+$/g, "");
1054+
context.fillStyle = "white";
1055+
context.beginPath();
1056+
const { topLeftCorner, topRightCorner, bottomRightCorner, bottomLeftCorner } = code.location;
1057+
context.moveTo(topLeftCorner.x, topLeftCorner.y);
1058+
context.lineTo(topRightCorner.x, topRightCorner.y);
1059+
context.lineTo(bottomRightCorner.x, bottomRightCorner.y);
1060+
context.lineTo(bottomLeftCorner.x, bottomLeftCorner.y);
1061+
context.lineTo(topLeftCorner.x, topLeftCorner.y);
1062+
context.fill();
1063+
context.closePath();
1064+
results.push(result);
1065+
}
1066+
resolve(results);
10491067
};
10501068
image2.onerror = reject;
10511069
_GM_xmlhttpRequest({
@@ -1069,34 +1087,49 @@
10691087
if (!image) {
10701088
return notiflixNotifyAio.Notify.warning("未选择图片, 请先右键选择图片");
10711089
}
1072-
decodeQrCode(image.src).then((data) => {
1073-
if (data === void 0) {
1090+
decodeQrCode(image.src).then((results) => {
1091+
if (results.length === 0) {
10741092
return notiflixNotifyAio.Notify.warning("未识别到二维码, 请确认图片是否有效");
10751093
}
1094+
const isMultiple = results.length > 1;
10761095
const element = document.createElement("div");
1077-
element.textContent = data;
1078-
element.style.fontSize = "16px";
1079-
element.style.padding = "10px";
1080-
element.style.border = "1px solid #ccc";
1081-
element.style.borderRadius = "5px";
1082-
element.style.maxHeight = "200px";
1083-
element.style.overflowY = "auto";
1084-
element.style.color = "rgba(0, 0, 0, .65)";
1085-
swal({
1096+
for (const [index, result] of results.entries()) {
1097+
const resultButton = document.createElement("button");
1098+
resultButton.innerHTML = result;
1099+
resultButton.style.fontSize = "16px";
1100+
resultButton.style.maxHeight = "200px";
1101+
resultButton.style.overflowY = "auto";
1102+
resultButton.style.width = "100%";
1103+
resultButton.className = "swal-button swal-button--cancel";
1104+
if (index > 0) {
1105+
resultButton.style.marginTop = "10px";
1106+
}
1107+
resultButton.dataset.result = result;
1108+
element.appendChild(resultButton);
1109+
}
1110+
element.addEventListener("click", (event) => {
1111+
if (event.target instanceof HTMLButtonElement && event.target.dataset.result) {
1112+
_GM_setClipboard(event.target.dataset.result, "text");
1113+
notiflixNotifyAio.Notify.success("已复制到剪贴板");
1114+
Swal.close();
1115+
}
1116+
});
1117+
Swal({
10861118
icon: "success",
10871119
title: "识别成功",
1120+
text: isMultiple ? "识别到多个二维码, 点击文本内容可单独复制" : void 0,
10881121
content: {
10891122
element
10901123
},
10911124
buttons: {
10921125
confirm: {
1093-
text: "复制到剪贴板",
1126+
text: isMultiple ? "全部复制到剪贴板" : "复制到剪贴板",
10941127
value: "copy"
10951128
}
10961129
}
10971130
}).then((result) => {
10981131
if (result === "copy") {
1099-
_GM_setClipboard(data, "text");
1132+
_GM_setClipboard(results.join("\n"), "text");
11001133
notiflixNotifyAio.Notify.success("已复制到剪贴板");
11011134
}
11021135
});

0 commit comments

Comments
 (0)