shaarli-qrcode.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Show the QR-Code of a permalink (when the QR-Code icon is clicked).
  2. function showQrCode(caller,loading)
  3. {
  4. // Dynamic javascript lib loading: We only load qr.js if the QR code icon is clicked:
  5. if (typeof(qr) == 'undefined') // Load qr.js only if not present.
  6. {
  7. if (!loading) // If javascript lib is still loading, do not append script to body.
  8. {
  9. var element = document.createElement("script");
  10. element.src = "plugins/qrcode/qr-1.1.3.min.js";
  11. document.body.appendChild(element);
  12. }
  13. setTimeout(function() { showQrCode(caller,true);}, 200); // Retry in 200 milliseconds.
  14. return false;
  15. }
  16. // Remove previous qrcode if present.
  17. removeQrcode();
  18. // Build the div which contains the QR-Code:
  19. var element = document.createElement('div');
  20. element.id="permalinkQrcode";
  21. // Make QR-Code div commit sepuku when clicked:
  22. if ( element.attachEvent ){
  23. element.attachEvent('onclick', 'this.parentNode.removeChild(this);' );
  24. } else {
  25. // Damn IE
  26. element.setAttribute('onclick', 'this.parentNode.removeChild(this);' );
  27. }
  28. // Build the QR-Code:
  29. var image = qr.image({size: 8,value: caller.dataset.permalink});
  30. if (image)
  31. {
  32. element.appendChild(image);
  33. element.innerHTML += "<br>Click to close";
  34. caller.parentNode.appendChild(element);
  35. }
  36. else
  37. {
  38. element.innerHTML = "Your browser does not seem to be HTML5 compatible.";
  39. }
  40. return false;
  41. }
  42. // Remove any displayed QR-Code
  43. function removeQrcode()
  44. {
  45. var elem = document.getElementById("permalinkQrcode");
  46. if (elem) {
  47. elem.parentNode.removeChild(elem);
  48. }
  49. return false;
  50. }