JAVASCRIPT   35

default.js

Guest on 10th October 2021 06:48:58 AM

  1. (function ($) {
  2.   Drupal.behaviors.pdf = {
  3.     attach: function(context, settings) {
  4.       var info = getAcrobatInfo();
  5.       console.log(info.browser + " " + info.acrobat + " " + info.acrobatVersion);
  6.       var iframe = $('iframe.pdf');
  7.       if (info.acrobat) {
  8.         iframe.each(function(){
  9.           setIframeSrc($(this));
  10.           $(this).attr('src', $(this).text());
  11.         });
  12.       }
  13.  
  14.       if (!isCanvasSupported()) {
  15.         // pdf.js isn't going to work in this browser--let's try acrobat.
  16.         if (info.acrobat) {
  17.           iframe.each(function(){
  18.             setIframeSrc($(this));
  19.             $(this).attr('src', $(this).text());
  20.           });
  21.         }
  22.         else {
  23.           // Even Acrobat isn't going to work--output a message telling user to upgrade their browser.
  24.           $('<p/>', {
  25.             text: 'Your browser is not capable of displaying a pdf. Please upgrade your browser to view this page as it was intended.',
  26.             'class': 'pdf acrobat-browser-messsage',
  27.           }).replaceAll(iframe);
  28.         }
  29.       }
  30.     }
  31.   };
  32. })(jQuery);
  33.  
  34. /**
  35.  * Detect browser support for canvas.
  36.  *
  37.  * Canvas support is one of the main things that is needed by pdf.js
  38.  * so detecting this should rule out most of the browsers that aren't
  39.  * going to work.
  40.  *
  41.  * See: http://stackoverflow.com/questions/2745432/best-way-to-detect-that-html5-canvas-is-not-supported
  42.  */
  43. function isCanvasSupported(){
  44.   var elem = document.createElement('canvas');
  45.   return !!(elem.getContext && elem.getContext('2d'));
  46. }
  47.  
  48. /**
  49.  * Set the iframe's source to be the value that was passed through in the
  50.  * data-src attribute.
  51.  */
  52. function setIframeSrc(e){
  53.   e.attr('src', e.attr('data-src')).removeAttr('data-src');
  54. }

Raw Paste


Login or Register to edit or fork this paste. It's free.