|
1 | 1 | // ==UserScript== |
2 | 2 | // @name 图片二维码识别(Common QR Code) |
3 | 3 | // @namespace xiaohuohumax/userscripts/common-qr-code |
4 | | -// @version 1.1.1 |
| 4 | +// @version 1.2.0 |
5 | 5 | // @author xiaohuohumax |
6 | 6 | // @description 右键图片,识别二维码并复制到剪贴板。 |
7 | 7 | // @license MIT |
|
1032 | 1032 | return sweetalert_min$1.exports; |
1033 | 1033 | } |
1034 | 1034 | var sweetalert_minExports = requireSweetalert_min(); |
1035 | | - const swal = /* @__PURE__ */ getDefaultExportFromCjs(sweetalert_minExports); |
| 1035 | + const Swal = /* @__PURE__ */ getDefaultExportFromCjs(sweetalert_minExports); |
1036 | 1036 | const ID = "common-qr-code"; |
1037 | | - const VERSION = "1.1.1"; |
| 1037 | + const VERSION = "1.2.0"; |
1038 | 1038 | async function decodeQrCode(url) { |
1039 | 1039 | return new Promise((resolve, reject) => { |
1040 | 1040 | const image2 = new Image(); |
1041 | 1041 | image2.onload = () => { |
1042 | | - var _a; |
| 1042 | + const results = []; |
1043 | 1043 | const { width, height } = image2; |
1044 | 1044 | const canvas = new OffscreenCanvas(width, height); |
1045 | 1045 | const context = canvas.getContext("2d"); |
1046 | 1046 | 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); |
1049 | 1067 | }; |
1050 | 1068 | image2.onerror = reject; |
1051 | 1069 | _GM_xmlhttpRequest({ |
|
1069 | 1087 | if (!image) { |
1070 | 1088 | return notiflixNotifyAio.Notify.warning("未选择图片, 请先右键选择图片"); |
1071 | 1089 | } |
1072 | | - decodeQrCode(image.src).then((data) => { |
1073 | | - if (data === void 0) { |
| 1090 | + decodeQrCode(image.src).then((results) => { |
| 1091 | + if (results.length === 0) { |
1074 | 1092 | return notiflixNotifyAio.Notify.warning("未识别到二维码, 请确认图片是否有效"); |
1075 | 1093 | } |
| 1094 | + const isMultiple = results.length > 1; |
1076 | 1095 | 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({ |
1086 | 1118 | icon: "success", |
1087 | 1119 | title: "识别成功", |
| 1120 | + text: isMultiple ? "识别到多个二维码, 点击文本内容可单独复制" : void 0, |
1088 | 1121 | content: { |
1089 | 1122 | element |
1090 | 1123 | }, |
1091 | 1124 | buttons: { |
1092 | 1125 | confirm: { |
1093 | | - text: "复制到剪贴板", |
| 1126 | + text: isMultiple ? "全部复制到剪贴板" : "复制到剪贴板", |
1094 | 1127 | value: "copy" |
1095 | 1128 | } |
1096 | 1129 | } |
1097 | 1130 | }).then((result) => { |
1098 | 1131 | if (result === "copy") { |
1099 | | - _GM_setClipboard(data, "text"); |
| 1132 | + _GM_setClipboard(results.join("\n"), "text"); |
1100 | 1133 | notiflixNotifyAio.Notify.success("已复制到剪贴板"); |
1101 | 1134 | } |
1102 | 1135 | }); |
|
0 commit comments