我爱二维码

把页面链接变成二维码,方便微信扫码

  1. // ==UserScript==
  2. // @name 我爱二维码
  3. // @namespace gqqnbig.me
  4. // @version 0.1
  5. // @description 把页面链接变成二维码,方便微信扫码
  6. // @author gqqnbig
  7. // @match http://*/*
  8. // @match https://*/*
  9. // @grant none
  10. // @require https://cdn.jsdelivr.net/npm/easyqrcodejs@4.3.5/dist/easy.qrcode.min.js
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. function createQR(elements, index) {
  17. let element=elements[index];
  18. let link=element.href;
  19. if(link.startsWith('http')===false)
  20. return;
  21.  
  22. let options= { text: link };
  23. let height=element.offsetHeight;
  24. let qrSize;
  25. if(height<=20)
  26. qrSize=25;
  27. else if(height<=40)
  28. qrSize=64;
  29. else
  30. {
  31. qrSize=128;
  32. options['logo']='https://open.weixin.qq.com/zh_CN/htmledition/res/assets/res-design-download/icon32_wx_logo.png';
  33. }
  34. options['height']=qrSize;
  35. options['width']=qrSize;
  36. new QRCode(element, options);
  37. let canvas=element.querySelector('canvas');
  38. if(canvas.offsetHeight>0)
  39. element.attributes.removeNamedItem('href');
  40. else
  41. //Failed to generate QR code.
  42. canvas.remove();
  43.  
  44. index++;
  45. if(index<elements.length)
  46. setTimeout(createQR, 1, elements, index);
  47.  
  48. }
  49.  
  50. let anchors=document.querySelectorAll("a[href]");
  51.  
  52.  
  53. if(anchors.length>0)
  54. createQR(anchors, 0);
  55.  
  56. })();