QR from highlight

Create a QR code from anything highlighted

当前为 2023-10-18 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         QR from highlight
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Create a QR code from anything highlighted
// @match        http://*/*
// @match        https://*/*
// @grant        none
// @require      https://cdnjs.cloudflare.com/ajax/libs/qrcode-generator/1.4.4/qrcode.js
// ==/UserScript==

(function () {
  'use strict';

  // Function to get the currently selected text
  function getSelectionText() {
    var selectedText = '';
    if (window.getSelection) {
      // All modern browsers and IE9+
      selectedText = window.getSelection().toString();
    }
    return selectedText;
  }

  // Function to generate and display QR code
  function generateQRCode(text) {
    let qrElement = document.getElementById('generated-qr-code');
    if (!qrElement) {
      qrElement = document.createElement('div');
      qrElement.id = 'generated-qr-code';
      document.body.appendChild(qrElement);
    }

    qrElement.innerHTML = ''; // Clear any previous QR codes

    let typeNumber = 0; // Auto detect QR code type based on input data
    let errorCorrectionLevel = 'L'; // Low error correction level
    let qr = qrcode(typeNumber, errorCorrectionLevel);
    qr.addData(text);
    qr.make();

    // Add QR code to the container
    qrElement.innerHTML = qr.createImgTag(5);

    // Style the container
    qrElement.style.width = '200px';
    qrElement.style.height = '200px';
    qrElement.style.position = 'fixed';
    qrElement.style.bottom = '10px';
    qrElement.style.right = '10px';
    qrElement.style.zIndex = '1000';
  }

  // Listen for text selection
  document.addEventListener('mouseup', function () {
    let selectedText = getSelectionText();
    if (selectedText) {
      generateQRCode(selectedText);
    }
  });
})();