QR from highlight

Create a QR code from anything highlighted

ของเมื่อวันที่ 18-10-2023 ดู เวอร์ชันล่าสุด

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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

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

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

(I already have a user script manager, let me install it!)

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.

(I already have a user style manager, let me install it!)

// ==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);
    }
  });
})();