图片二维码识别(Common QR Code)

右键图片,识别二维码并复制到剪贴板。

当前为 2025-06-06 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         图片二维码识别(Common QR Code)
// @namespace    xiaohuohumax/userscripts/common-qr-code
// @version      1.0.0
// @author       xiaohuohumax
// @description  右键图片,识别二维码并复制到剪贴板。
// @license      MIT
// @icon         https://raw.githubusercontent.com/xiaohuohumax/logo/refs/heads/main/logos/logo.svg
// @source       https://github.com/xiaohuohumax/userscripts.git
// @match        http*://*/*
// @require      https://unpkg.com/[email protected]/dist/jsQR.js
// @require      https://unpkg.com/[email protected]/build/notiflix-notify-aio.js
// @grant        GM_registerMenuCommand
// @grant        GM_setClipboard
// @grant        GM_xmlhttpRequest
// @run-at       document-start
// @noframes
// ==/UserScript==

(function (notiflixNotifyAio, jsQR) {
  'use strict';

  var _GM_registerMenuCommand = /* @__PURE__ */ (() => typeof GM_registerMenuCommand != "undefined" ? GM_registerMenuCommand : void 0)();
  var _GM_setClipboard = /* @__PURE__ */ (() => typeof GM_setClipboard != "undefined" ? GM_setClipboard : void 0)();
  var _GM_xmlhttpRequest = /* @__PURE__ */ (() => typeof GM_xmlhttpRequest != "undefined" ? GM_xmlhttpRequest : void 0)();
  const ID = "common-qr-code";
  const VERSION = "1.0.0";
  async function decodeQrCode(url) {
    return new Promise((resolve, reject) => {
      const image2 = new Image();
      image2.onload = () => {
        var _a;
        const { width, height } = image2;
        const canvas = new OffscreenCanvas(width, height);
        const context = canvas.getContext("2d");
        context.drawImage(image2, 0, 0);
        const imageData = context.getImageData(0, 0, width, height);
        resolve((_a = jsQR(imageData.data, width, height)) == null ? void 0 : _a.data.replace(/^\s+|\s+$/g, ""));
      };
      image2.onerror = reject;
      _GM_xmlhttpRequest({
        method: "GET",
        url,
        anonymous: true,
        responseType: "blob",
        onload: (response) => {
          if (response.status !== 200) {
            reject(new Error(`Failed to load image: ${response.status} ${response.statusText}`));
            return;
          }
          image2.src = URL.createObjectURL(response.response);
        },
        onerror: reject
      });
    });
  }
  console.log(`${ID}(v${VERSION})`);
  let image = null;
  _GM_registerMenuCommand("Decode QR Code", () => {
    if (!image) {
      return notiflixNotifyAio.Notify.warning("未选择图片, 请先右键选择图片");
    }
    decodeQrCode(image.src).then((data) => {
      if (data === void 0) {
        return notiflixNotifyAio.Notify.warning("未识别到二维码, 请确认图片是否有效");
      }
      notiflixNotifyAio.Notify.success("识别成功, 已复制到剪贴板");
      _GM_setClipboard(data, "text");
    }).catch((error) => {
      notiflixNotifyAio.Notify.failure("识别失败, 请检查图片是否有效");
      console.error(error);
    }).finally(() => image = null);
  });
  document.addEventListener("contextmenu", (event) => {
    if (event.target instanceof HTMLImageElement) {
      image = event.target;
    }
  });

})(Notiflix, jsQR);