|
本帖最后由 coult3 于 2025-6-1 15:13 编辑
用ai写了一个油猴/暴力猴脚本
测试地址:在线二维码生成器 | 菜鸟工具
- // ==UserScript==
- // @name 右键识别二维码并打开链接 QR Code Right Click Handler
- // @version 1.0
- // @description 右键识别二维码并打开链接
- // @author You
- // @match *://*/*
- // @grant none
- // @require https://cdn.jsdelivr.net/npm/jsqr@1.4.0/dist/jsQR.js
- // ==/UserScript==
- (function() {
- 'use strict';
- // 创建canvas用于图像处理
- const canvas = document.createElement('canvas');
- const ctx = canvas.getContext('2d');
- // 识别二维码的函数
- function decodeQRCode(imgElement) {
- return new Promise((resolve, reject) => {
- // 设置canvas尺寸
- canvas.width = imgElement.naturalWidth || imgElement.width;
- canvas.height = imgElement.naturalHeight || imgElement.height;
- // 绘制图像到canvas
- ctx.drawImage(imgElement, 0, 0);
- // 获取图像数据
- const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
- // 使用jsQR库识别二维码
- if (typeof jsQR !== 'undefined') {
- const code = jsQR(imageData.data, imageData.width, imageData.height);
- if (code) {
- resolve(code.data);
- } else {
- reject('未识别到二维码');
- }
- } else {
- reject('jsQR库未加载');
- }
- });
- }
- // 检查是否为HTTP/HTTPS链接
- function isHttpUrl(string) {
- return string.startsWith('http://') || string.startsWith('https://');
- }
- // 右键点击事件处理
- document.addEventListener('contextmenu', async function(e) {
- const target = e.target;
- // 检查是否为图片元素
- if (target.tagName === 'IMG' ||
- (target.style && target.style.backgroundImage) ||
- target.tagName === 'CANVAS') {
- try {
- let imgElement = target;
- // 如果是背景图片,创建img元素
- if (target.style && target.style.backgroundImage) {
- const bgImg = target.style.backgroundImage;
- const url = bgImg.slice(4, -1).replace(/"/g, "");
- imgElement = new Image();
- imgElement.crossOrigin = 'anonymous';
- await new Promise((resolve, reject) => {
- imgElement.onload = resolve;
- imgElement.onerror = reject;
- imgElement.src = url;
- });
- }
- // 识别二维码
- const qrData = await decodeQRCode(imgElement);
- // 如果是HTTP链接,阻止默认右键菜单并打开链接
- if (isHttpUrl(qrData)) {
- e.preventDefault();
- window.open(qrData, '_blank');
- return false;
- }
- } catch (error) {
- // 识别失败,继续显示默认右键菜单
- console.log('二维码识别失败:', error);
- }
- }
- // 默认情况下显示原本的右键菜单
- return true;
- });
- })();
复制代码
|
|