Greasy Fork

Greasy Fork is available in English.

addqrcode

qrcode

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

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.icu/scripts/468518/1204144/addqrcode.js

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    function ADDqrcode() {

  // 创建关闭按钮并设置样式
  var $closeBtn = $('<button>X</button>').css({
    'position': 'absolute',
    'top': '5px',
    'right': '5px',
    'font-size': '16px',
    'line-height': '20px',
    'cursor': 'pointer',
    'background-color': 'rgb(169 169 169)',
    'border': '1px solid rgb(204, 204, 204)',
    'color': 'aliceblue',
  });

  // 关闭按钮事件
  $closeBtn.on('click', function() {
    $qrDiv.remove();
  });

  // 创建二维码元素并设置样式
  var $qrDiv = $('<div id="getqrCode"/>').css({
    'position': 'fixed',
    'top': '50%',
    'left': '50%',
    'transform': 'translate(-50%, -50%)',
    'background-color': 'white',
    'border': '1px solid #c5c5c5',
    'padding': '20px',
    'z-index': '9999',
    'border-radius': '10px',
  });

  // 将关闭按钮添加到二维码元素
  $qrDiv.append($closeBtn);

  // 创建输入框和确定按钮并添加到二维码元素中
  var $inputWrapper = $('<div/>').css({
    'width': '100%',
    'margin-top': '10px'
  }).appendTo($qrDiv);

  // 创建输入框并设置样式
  var $input = $('<input type="text" placeholder="请输入需要生成的内容"/>').css({
    'width': 'calc(100% - 80px)',
    'padding': '5px',
    'font-size': '16px',
    'border': '1px solid #ccc',
    'border-radius': '3px'
  }).appendTo($inputWrapper);

  // 创建确定按钮并设置样式
  var $confirmBtn = $('<button>生成</button>').css({
    'display': 'inline-block', 
    'padding': '5px 10px',
    'margin-left': '10px',
    'font-size': '16px',
    'text-align': 'center',
    'color': '#fff',
    'background-color': '#42b983',
    'border': 'none',
    'border-radius': '3px',
    'cursor': 'pointer'
  }).on('click', function() {
    var text = $input.val();
    if (text) {
      // 对输入文本进行编码转换
      text = toUtf8(text);
      // 生成自定义的二维码并替换原有的二维码
      $canvas.find('canvas').remove();
      $canvas.qrcode({
        'width': 200,
        'height': 200,
        'text': text
      });
    } else {
      alert('请输入需要生成的内容!');
    }
  }).appendTo($inputWrapper);

  // 创建包裹二维码的 canvas 容器
  var $canvas = $('<div/>').css({
    'margin-top': '20px',
    'text-align': 'center'
  }).appendTo($qrDiv);

  // 添加二维码元素到 body 中
  $('body').append($qrDiv);

  // 生成二维码
  $canvas.qrcode({
    'width': 200,
    'height': 200,
    'text': window.location.href
  });
    $canvas.after('<p  style="padding: 10px" >首次打开默认的二维码为当前页面的网址</p>')
 };


    function toUtf8(str) {
    var out, i, len, c;
    out = "";
    len = str.length;
    for (i = 0; i < len; i++) {
        c = str.charCodeAt(i);
        if ((c >= 0x0001) && (c <= 0x007F)) {
            out += str.charAt(i);
        } else if (c > 0x07FF) {
            out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
            out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
            out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
        } else {
            out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
            out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
        }
    }
    return out;
}