Greasy Fork is available in English.

jquery.qrcode.js

The original author jeromeetienne https://github.com/jeromeetienne/jquery-qrcode

Dette script bør ikke installeres direkte. Det er et bibliotek, som andre scripts kan inkludere med metadirektivet // @require https://update.greasyfork.org/scripts/31885/208856/jqueryqrcodejs.js

  1. // ==UserScript==
  2. // @license MIT License
  3. // ==/UserScript==
  4.  
  5.  
  6. (function( $ ){
  7. $.fn.qrcode = function(options) {
  8. // if options is string,
  9. if( typeof options === 'string' ){
  10. options = { text: options };
  11. }
  12.  
  13. // set default values
  14. // typeNumber < 1 for automatic calculation
  15. options = $.extend( {}, {
  16. render : "canvas",
  17. width : 256,
  18. height : 256,
  19. typeNumber : -1,
  20. correctLevel : QRErrorCorrectLevel.H,
  21. background : "#ffffff",
  22. foreground : "#000000"
  23. }, options);
  24.  
  25. var createCanvas = function(){
  26. // create the qrcode itself
  27. var qrcode = new QRCode(options.typeNumber, options.correctLevel);
  28. qrcode.addData(options.text);
  29. qrcode.make();
  30.  
  31. // create canvas element
  32. var canvas = document.createElement('canvas');
  33. canvas.width = options.width;
  34. canvas.height = options.height;
  35. var ctx = canvas.getContext('2d');
  36.  
  37. // compute tileW/tileH based on options.width/options.height
  38. var tileW = options.width / qrcode.getModuleCount();
  39. var tileH = options.height / qrcode.getModuleCount();
  40.  
  41. // draw in the canvas
  42. for( var row = 0; row < qrcode.getModuleCount(); row++ ){
  43. for( var col = 0; col < qrcode.getModuleCount(); col++ ){
  44. ctx.fillStyle = qrcode.isDark(row, col) ? options.foreground : options.background;
  45. var w = (Math.ceil((col+1)*tileW) - Math.floor(col*tileW));
  46. var h = (Math.ceil((row+1)*tileW) - Math.floor(row*tileW));
  47. ctx.fillRect(Math.round(col*tileW),Math.round(row*tileH), w, h);
  48. }
  49. }
  50. // return just built canvas
  51. return canvas;
  52. }
  53.  
  54. // from Jon-Carlos Rivera (https://github.com/imbcmdth)
  55. var createTable = function(){
  56. // create the qrcode itself
  57. var qrcode = new QRCode(options.typeNumber, options.correctLevel);
  58. qrcode.addData(options.text);
  59. qrcode.make();
  60. // create table element
  61. var $table = $('<table></table>')
  62. .css("width", options.width+"px")
  63. .css("height", options.height+"px")
  64. .css("border", "0px")
  65. .css("border-collapse", "collapse")
  66. .css('background-color', options.background);
  67. // compute tileS percentage
  68. var tileW = options.width / qrcode.getModuleCount();
  69. var tileH = options.height / qrcode.getModuleCount();
  70.  
  71. // draw in the table
  72. for(var row = 0; row < qrcode.getModuleCount(); row++ ){
  73. var $row = $('<tr></tr>').css('height', tileH+"px").appendTo($table);
  74. for(var col = 0; col < qrcode.getModuleCount(); col++ ){
  75. $('<td></td>')
  76. .css('width', tileW+"px")
  77. .css('background-color', qrcode.isDark(row, col) ? options.foreground : options.background)
  78. .appendTo($row);
  79. }
  80. }
  81. // return just built canvas
  82. return $table;
  83. }
  84.  
  85. return this.each(function(){
  86. var element = options.render == "canvas" ? createCanvas() : createTable();
  87. $(element).appendTo(this);
  88. });
  89. };
  90. })( jQuery );