识别网页上的二维码

悬浮在图片上时,右键菜单里给出识别二维码的选项。

25.03.2025 itibariyledir. En son verisyonu görün.

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name         识别网页上的二维码
// @name:en      Recognizing QR codes on web pages
// @name:zh-TW   辨識網頁上的二維碼
// @name:ja      ウェブページ上のQRコードを認識
// @name:ko      웹 페이지의 QR 코드 인식
// @name:fr      Reconnaissance des codes QR sur les pages web
// @name:de      QR-Codes auf Webseiten erkennen
// @name:es      Reconocimiento de códigos QR en páginas web
// @name:ru      Распознавание QR-кодов на веб-страницах
// @name:ar      التعرف على رموز QR على صفحات الويب
// @name:pt      Reconhecimento de códigos QR em páginas da web
// @name:it      Riconoscimento dei codici QR nelle pagine web
// @description  悬浮在图片上时,右键菜单里给出识别二维码的选项。
// @description:en When hovering over an image, add a QR code recognition option to the right-click menu.
// @description:zh-TW 懸浮在圖片上時,右鍵選單中提供辨識二維碼的選項。
// @description:ja 画像上にマウスホバーしたとき、右クリックメニューにQRコード認識のオプションを追加。
// @description:ko 이미지 위에 마우스를 올렸을 때, 오른쪽 클릭 메뉴에 QR 코드 인식 옵션 추가.
// @description:fr Lors du survol d'une image, ajouter une option de reconnaissance de code QR au menu contextuel.
// @description:de Beim Hovern über ein Bild eine QR-Code-Erkennungsoption zum Kontextmenü hinzufügen.
// @description:es Al pasar el cursor sobre una imagen, agregar una opción de reconocimiento de código QR al menú contextual.
// @description:ru При наведении на изображение добавить в контекстное меню опцию распознавания QR-кода.
// @description:ar عند تمرير المؤشر فوق صورة، أضف خيار التعرف على رمز QR إلى القائمة السياقية.
// @description:pt Ao passar o mouse sobre uma imagem, adicionar uma opção de reconhecimento de código QR ao menu de contexto.
// @description:it Durante il passaggio del mouse su un'immagine, aggiungere un'opzione di riconoscimento del codice QR al menu contestuale.
// @description  悬浮在图片上时,右键菜单里给出识别二维码的选项。
// @description:en When hovering over an image, the right-click menu gives the option to recognize the QR code.
// @namespace    http://tampermonkey.net/
// @version      0.2.3
// @author       aspen138
// @match        *://*/*
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @grant        GM_notification
// @require      https://unpkg.com/jsqr/dist/jsQR.js
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    let selectedImage = null;

    // 添加右键菜单选项
    document.addEventListener('contextmenu', function(event) {
        // 确定是否是图片元素
        if (event.target.tagName === 'IMG') {
            selectedImage = event.target; // 保存当前选中的图片
        } else {
            selectedImage = null;
        }
    }, false);

    // 注册菜单命令
    GM_registerMenuCommand("识别二维码", function() {
        console.log("selectedImage=", selectedImage);
        if (selectedImage) {
            decodeQRCode(selectedImage);
        }
    }, 'r');

    function decodeQRCode(image) {
        // 创建Canvas来读取图片内容
        const canvas = document.createElement('canvas');
        const context = canvas.getContext('2d');
        canvas.width = image.naturalWidth; // 使用图片的原始尺寸
        canvas.height = image.naturalHeight;
        context.drawImage(image, 0, 0, canvas.width, canvas.height);
        const imageData = context.getImageData(0, 0, canvas.width, canvas.height);

        // 使用jsQR库识别二维码
        const code = jsQR(imageData.data, imageData.width, imageData.height);

        // 如果识别出二维码,发送通知显示结果
        if (code) {
            alert(`二维码内容:${code.data}`+ '     二维码识别结果');  //别用GM_notification了吧
        } else {
            alert('未识别到二维码,请确保图片中包含一个可识别的二维码。' + '   二维码识别错误');  //别用GM_notification了吧
        }
    }
})();