MediaWiki:Gadget-herzing-pdf.js

From Doc Moates Wiki
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* Herzing guide: inline PDF viewer. Template:Herzing syllabus outputs
   <div class="hz-pdf" data-file="URL">; this adds a frame that shows the file with the
   browser's own PDF viewer. Only PDFs uploaded to this wiki are accepted.
   Source: wiki/onwiki/herzing/ in the website repo. See docs/wiki/herzing-pages.md. */
( function () {
	var ok = /^(https?:)?(\/\/wiki\.docmoates\.com)?\/images\/[A-Za-z0-9\/._%-]+\.pdf$/;
	mw.hook( 'wikipage.content' ).add( function ( $content ) {
		$content.find( '.hz-pdf[data-file]' ).each( function () {
			var url = this.getAttribute( 'data-file' ) || '';
			if ( this.querySelector( 'iframe' ) || !ok.test( url ) ) {
				return;
			}
			var frame = document.createElement( 'iframe' );
			frame.className = 'hz-pdf-frame';
			frame.loading = 'lazy';
			frame.title = this.getAttribute( 'data-title' ) || 'PDF document';
			frame.src = url + '#view=FitH';
			this.appendChild( frame );
		} );
	} );
}() );