利用google api生成当前页面二维码

Add a "Generate QR Code" button to mp.weixin.qq.com

  1. // ==UserScript==
  2. // @name 利用google api生成当前页面二维码
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Add a "Generate QR Code" button to mp.weixin.qq.com
  6. // @author Cantan Tam
  7. // @match *://*/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. let isGeneratingQRCode = false;
  16.  
  17. // 创建按钮元素
  18. const qrCodeButton = document.createElement('div');
  19. const buttonContent = document.createElement('div');
  20. buttonContent.textContent = '生成二维码';
  21. qrCodeButton.appendChild(buttonContent);
  22.  
  23. // 设置按钮样式
  24. qrCodeButton.style.position = 'fixed';
  25. qrCodeButton.style.top = '10px';
  26. qrCodeButton.style.right = '20.5px';
  27. qrCodeButton.style.width = '94px';
  28. qrCodeButton.style.height = '30px';
  29. qrCodeButton.style.borderRadius = '5px';
  30. qrCodeButton.style.backgroundColor = '#f2f2f2ff';
  31. qrCodeButton.style.color = '#ccccccff';
  32. qrCodeButton.style.textAlign = 'center';
  33. qrCodeButton.style.fontSize = '14px';
  34. qrCodeButton.style.cursor = 'pointer';
  35. qrCodeButton.style.userSelect = 'none';
  36. qrCodeButton.style.zIndex = '9999';
  37. qrCodeButton.style.lineHeight = '32px'; // 垂直居中
  38.  
  39. // 按钮的悬停效果
  40. qrCodeButton.addEventListener('mouseover', function() {
  41. if (isGeneratingQRCode) {
  42. qrCodeButton.style.backgroundColor = '#ff9955ff';
  43. buttonContent.style.color = '#ffffffff';
  44. } else {
  45. qrCodeButton.style.backgroundColor = '#27ae60ff';
  46. buttonContent.style.color = '#ffffffff';
  47. }
  48. });
  49.  
  50. qrCodeButton.addEventListener('mouseout', function() {
  51. if (isGeneratingQRCode) {
  52. qrCodeButton.style.backgroundColor = '#ff9955ff';
  53. buttonContent.style.color = '#ffffffff';
  54. } else {
  55. qrCodeButton.style.backgroundColor = '#f2f2f2ff';
  56. buttonContent.style.color = '#ccccccff';
  57. }
  58. });
  59.  
  60. // 按钮点击事件
  61. qrCodeButton.addEventListener('click', function() {
  62. if (!isGeneratingQRCode) {
  63. isGeneratingQRCode = true;
  64. buttonContent.textContent = '下载二维码';
  65. qrCodeButton.style.backgroundColor = '#ff9955ff';
  66. buttonContent.style.color = '#ffffffff';
  67. generateQRCode();
  68. } else {
  69. downloadQRCode();
  70. }
  71. });
  72.  
  73. // 生成二维码并插入到页面
  74. function generateQRCode() {
  75. const url = window.location.href;
  76. const qrCodeUrl = `https://chart.googleapis.com/chart?chs=500x500&cht=qr&chl=${encodeURIComponent(url)}&chld=L|0`; // 设置白边为0
  77.  
  78. // 清除页面上生成的125x125二维码图像
  79. clearGeneratedQRCode();
  80.  
  81. const qrCodeImage = document.createElement('img');
  82. qrCodeImage.src = qrCodeUrl;
  83. qrCodeImage.style.position = 'fixed';
  84. qrCodeImage.style.top = '45px'; // 20px + 30px (按钮高度)
  85. qrCodeImage.style.right = '5px';
  86. qrCodeImage.style.width = '125px';
  87. qrCodeImage.style.height = '125px';
  88.  
  89. document.body.appendChild(qrCodeImage);
  90. }
  91.  
  92. // 下载二维码
  93. function downloadQRCode() {
  94. const url = window.location.href;
  95. const qrCodeUrl = `https://chart.googleapis.com/chart?chs=500x500&cht=qr&chl=${encodeURIComponent(url)}&chld=L|0`; // 设置白边为0
  96.  
  97. // 获取前网页的<title></title>内容,并将其用作文件名
  98. const pageTitle = document.title || 'QRCode';
  99.  
  100. fetch(qrCodeUrl)
  101. .then(response => response.blob())
  102. .then(blob => {
  103. const url = window.URL.createObjectURL(blob);
  104. const a = document.createElement('a');
  105. a.href = url;
  106. a.download = `${pageTitle}.png`;
  107. a.style.display = 'none'; // 隐藏下载链接
  108. document.body.appendChild(a);
  109. a.click();
  110. window.URL.revokeObjectURL(url);
  111. a.remove();
  112. });
  113.  
  114. // 延迟0.5秒后更新按钮文字、颜色
  115. setTimeout(function() {
  116. isGeneratingQRCode = false;
  117. buttonContent.textContent = '下载已开始';
  118. qrCodeButton.style.backgroundColor = '#f2f2f2ff';
  119. buttonContent.style.color = '#666666ff';
  120. }, 500);
  121.  
  122. // 清除页面上生成的125x125二维码图像
  123. clearGeneratedQRCode();
  124. }
  125.  
  126. // 清除页面上生成的125x125二维码图像
  127. function clearGeneratedQRCode() {
  128. const qrCodeImages = document.querySelectorAll('img[src*="chart.googleapis.com"]');
  129. qrCodeImages.forEach(img => {
  130. img.remove();
  131. });
  132. }
  133.  
  134. // 将按钮添加到页面
  135. document.body.appendChild(qrCodeButton);
  136. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址