JQuery Plugin Google Viewer Embed

by javajack

HTML

<div style="background:#fff; width:960px; margin:0 auto; padding:20px;">
    <!-- <p>See <a href="http://www.jawish.org/blog/archives/394-Google-Docs-Viewer-plugin-for-jQuery.html" target="_blank">documentation and usage guide</a></p> -->
</div>
<div>
    <a href="https://futurewise-temp.s3.amazonaws.com/helloworld.txt?AWSAccessKeyId=AKIAIKCGSVAFKYXP4VSQ&Expires=1386830770&Signature=HduBfc9fB6vwUGGL4SkGSCgE8cY%3D" class="embed" style="text-align : center; display : block;">PDF test georgetown.edu</a>

    <!-- <a href="http://plugindoc.mozdev.org/testpages/test.pdf" class="embed">PDF at mozdev.org</a>

		<h3>URLs with id: embedURL</h3>
		<a href="www.snee.com/xml/xslt/sample.doc" id="embedURL">Sample PDF at samplepdf.com</a>
		-->
</div>

JavaScript

(function($){
	$.fn.gdocsViewer = function(options) {
	
		var settings = {
			width  : '600',
			height : '700'
		};
		
		if (options) { 
			$.extend(settings, options);
		}
		
		return this.each(function() {
			var file = $(this).attr('href');
            // int SCHEMA = 2, DOMAIN = 3, PORT = 5, PATH = 6, FILE = 8, QUERYSTRING = 9, HASH = 12
			var extregex = /^((http[s]?|ftp):\/)?\/?([^:\/\s]+)(:([^\/]*))?((\/[\w/-]+)*\/)([\w\-\.]+[^#?\s]+)(\?([^#]*))?(#(.*))?$/i;
			var ext = file.match(extregex)[8].split(".").pop();
            console.log("Extension : "+ext);
			if (/^(tiff|pdf|ppt|pps|doc|docx|txt)$/.test(ext)) {
				$(this).after(function () {
					var id = $(this).attr('id');
					var gdvId = (typeof id !== 'undefined' && id !== false) ? id + '-gdocsviewer' : '';
					return '<div id="' + gdvId + '" class="gdocsviewer"><iframe src="http://docs.google.com/viewer?embedded=true&url=' + encodeURIComponent(file) + '" width="' + settings.width + '" height="' + settings.height + '" style="border: none;margin : 0 auto; display : block;"></iframe></div>';
				})
			}
		});
	};
})( jQuery );

/*<![CDATA[*/
$(document).ready(function () {
    $('a.embed').gdocsViewer({
        width: 740,
        height: 742
    });
    $('#embedURL').gdocsViewer();
});
/*]]>*/