addqrcode

qrcode

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/468518/1204970/addqrcode.js

  1. // @license End-User License Agreement
  2. function DECqrcode() {
  3. let dialogHtml = '<div id="qrcodeDialog" style="position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); z-index: 9999; width: 400px; background-color: #ffffff; border-radius: 5px; padding: 20px; box-shadow: 0 4px 8px rgba(0,0,0,0.2);">';
  4. dialogHtml += '<div style="font-size: 20px;">选择需要解析的二维码图片</div>';
  5. dialogHtml += '<hr>';
  6. dialogHtml += '<input id="qrcodeFileInput" type="file" accept="image/*" style="margin-bottom: 20px;">';
  7. dialogHtml += '<button id="qrcodeCancelBtn" style="float: right; padding: 2px 5px; font-size: 14px; text-align: center; color: rgb(255, 255, 255); background-color: rgb(66, 185, 131); border: none;border-radius: 3px; cursor: pointer;">关闭</button>';
  8. dialogHtml += '<button id="qrcodeConfirmBtn" style="float: right; margin-right: 10px; padding: 2px 5px; font-size: 14px; text-align: center; color: rgb(255, 255, 255); background-color: rgb(66, 185, 131); border: none;border-radius: 3px; cursor: pointer;">确定</button>';
  9. dialogHtml += '</div>';
  10. $('body').append(dialogHtml);
  11.  
  12. // 取消按钮事件
  13. $('#qrcodeCancelBtn').on('click', function() {
  14. $('#qrcodeDialog').remove();
  15. });
  16.  
  17. // 确认按钮事件
  18. $('#qrcodeConfirmBtn').on('click', function() {
  19. let fileInput = $('#qrcodeFileInput')[0];
  20. if (fileInput.files.length === 0) {
  21. toastr.error('请选择需要解析的二维码图片!', '', { positionClass: 'toast-top-center', showDuration: 300, hideDuration: 1000, timeOut: 3000, extendedTimeOut: 1000, showEasing: 'swing', hideEasing: 'linear', showMethod: 'fadeIn', hideMethod: 'fadeOut' });
  22. return;
  23. }
  24.  
  25. let file = fileInput.files[0];
  26. let reader = new FileReader();
  27. reader.onload = function(event) {
  28. let imageData = event.target.result;
  29.  
  30. // 创建一个 Canvas 元素,并将图片渲染到 Canvas 上
  31. let canvas = document.createElement('canvas');
  32. let context = canvas.getContext('2d');
  33. let image = new Image();
  34. image.onload = function() {
  35. canvas.width = image.width;
  36. canvas.height = image.height;
  37. context.drawImage(image, 0, 0);
  38.  
  39. // 获取 Canvas 上的图片数据,并解析二维码
  40. let imageData = context.getImageData(0, 0, canvas.width, canvas.height);
  41. let code = jsQR(imageData.data, imageData.width, imageData.height);
  42. if (code !== null) {
  43. let resultHtml = '<div style="font-size: 16px;">解析结果:' + code.data + '</div>';
  44. resultHtml += '<hr>';
  45. resultHtml += '<button id="qrcodeCloseBtn" style="float: right;margin-right: 10px; padding: 2px 5px; font-size: 14px; text-align: center; color: rgb(255, 255, 255); background-color: rgb(66, 185, 131); border: none;border-radius: 3px; cursor: pointer;">关闭</button>';
  46. resultHtml += '<button id="qrcodeCopyBtn" style="float: right; margin-right: 10px;padding: 2px 5px; font-size: 14px; text-align: center; color: rgb(255, 255, 255); background-color: rgb(66, 185, 131); border: none;border-radius: 3px; cursor: pointer;">复制</button>';
  47. $('#qrcodeDialog').html(resultHtml);
  48.  
  49. // 复制按钮事件
  50. $('#qrcodeCopyBtn').on('click', function() {
  51. let copyText = document.createElement('textarea');
  52. copyText.value = code.data;
  53. document.body.appendChild(copyText);
  54. copyText.select();
  55. document.execCommand('copy');
  56. document.body.removeChild(copyText);
  57.  
  58. toastr.success('已复制到剪贴板!', '', { positionClass: 'toast-bottom-right', showDuration: 300, hideDuration: 1000, timeOut: 3000, extendedTimeOut: 1000, showEasing: 'swing', hideEasing: 'linear', showMethod: 'fadeIn', hideMethod: 'fadeOut' });
  59. });
  60.  
  61. // 关闭按钮事件
  62. $('#qrcodeCloseBtn').on('click', function() {
  63. $('#qrcodeDialog').remove();
  64. });
  65. } else {
  66. toastr.error('未找到二维码!', '', { positionClass: 'toast-top-center', showDuration: 300, hideDuration: 1000, timeOut: 3000, extendedTimeOut: 1000, showEasing: 'swing', hideEasing: 'linear', showMethod: 'fadeIn', hideMethod: 'fadeOut' });
  67.  
  68. }
  69. };
  70. image.src = imageData;
  71. };
  72. reader.readAsDataURL(file);
  73. });
  74. }
  75. function ADDqrcode() {
  76.  
  77. // 创建关闭按钮并设置样式
  78. var $closeBtn = $('<button>X</button>').css({
  79. 'position': 'absolute',
  80. 'top': '5px',
  81. 'right': '5px',
  82. 'font-size': '16px',
  83. 'line-height': '20px',
  84. 'cursor': 'pointer',
  85. 'background-color': 'rgb(169 169 169)',
  86. 'border': '1px solid rgb(204, 204, 204)',
  87. 'color': 'aliceblue',
  88. });
  89.  
  90. // 关闭按钮事件
  91. $closeBtn.on('click', function() {
  92. $qrDiv.remove();
  93. });
  94.  
  95. // 创建二维码元素并设置样式
  96. var $qrDiv = $('<div id="getqrCode"/>').css({
  97. 'position': 'fixed',
  98. 'top': '50%',
  99. 'left': '50%',
  100. 'transform': 'translate(-50%, -50%)',
  101. 'background-color': 'white',
  102. 'border': '1px solid #c5c5c5',
  103. 'padding': '20px',
  104. 'z-index': '9999',
  105. 'border-radius': '10px',
  106. 'text-align': 'center',
  107. });
  108.  
  109. // 将关闭按钮添加到二维码元素
  110. $qrDiv.append($closeBtn);
  111.  
  112. // 创建输入框和确定按钮并添加到二维码元素中
  113. var $inputWrapper = $('<div/>').css({
  114. 'width': '100%',
  115. 'margin-top': '10px'
  116. }).appendTo($qrDiv);
  117.  
  118. // 创建输入框并设置样式
  119. var $input = $('<input type="text" placeholder="请输入需要生成的内容"/>').css({
  120. 'width': '100%',
  121. 'padding': '5px',
  122. 'font-size': '16px',
  123. 'border': '1px solid #ccc',
  124. 'border-radius': '3px'
  125. }).appendTo($inputWrapper);
  126.  
  127. // 创建确定按钮并设置样式
  128. var $confirmBtn = $('<button>生成二维码</button>').css({
  129. 'padding': '5px 10px',
  130. 'margin-left': '10px',
  131. 'font-size': '16px',
  132. 'text-align': 'center',
  133. 'color': '#fff',
  134. 'background-color': '#42b983',
  135. 'border': 'none',
  136. 'border-radius': '3px',
  137. 'cursor': 'pointer',
  138. 'margin': '10px',
  139. }).on('click', function() {
  140. var text = $input.val();
  141. if (text) {
  142. // 对输入文本进行编码转换
  143. text = toUtf8(text);
  144. // 生成自定义的二维码并替换原有的二维码
  145. $canvas.find('canvas').remove();
  146. $canvas.qrcode({
  147. 'width': 200,
  148. 'height': 200,
  149. 'text': text
  150. });
  151. } else {
  152. toastr.error('请输入需要生成的内容!', '', { positionClass: 'toast-top-center', showDuration: 300, hideDuration: 1000, timeOut: 3000, extendedTimeOut: 1000, showEasing: 'swing', hideEasing: 'linear', showMethod: 'fadeIn', hideMethod: 'fadeOut' });
  153. }
  154. }).appendTo($inputWrapper);
  155.  
  156.  
  157. // 创建确定按钮并设置样式
  158. var $confirmBtn = $('<button>解析二维码</button>').css({
  159. 'padding': '5px 10px',
  160. 'margin-left': '10px',
  161. 'font-size': '16px',
  162. 'text-align': 'center',
  163. 'color': '#fff',
  164. 'background-color': '#42b983',
  165. 'border': 'none',
  166. 'border-radius': '3px',
  167. 'cursor': 'pointer',
  168. 'margin': '10px',
  169. }).on('click', function() {
  170. DECqrcode();
  171. }).appendTo($inputWrapper);
  172.  
  173. // 创建包裹二维码的 canvas 容器
  174. var $canvas = $('<div/>').css({
  175. 'margin-top': '20px',
  176. 'text-align': 'center'
  177. }).appendTo($qrDiv);
  178.  
  179. // 添加二维码元素到 body 中
  180. $('body').append($qrDiv);
  181.  
  182. // 生成二维码
  183. $canvas.qrcode({
  184. 'width': 200,
  185. 'height': 200,
  186. 'text': window.location.href
  187. });
  188. $canvas.after('<p style="padding: 10px" >首次打开默认的二维码为当前页面的网址</p>')
  189. };
  190.  
  191.  
  192. function toUtf8(str) {
  193. var out, i, len, c;
  194. out = "";
  195. len = str.length;
  196. for (i = 0; i < len; i++) {
  197. c = str.charCodeAt(i);
  198. if ((c >= 0x0001) && (c <= 0x007F)) {
  199. out += str.charAt(i);
  200. } else if (c > 0x07FF) {
  201. out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
  202. out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
  203. out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
  204. } else {
  205. out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
  206. out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
  207. }
  208. }
  209. return out;
  210. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址