/*! * googoose 1.0.2 * https://github.com/aadel112/googoose/js/jquery.googoose.js * @license Apache 2.0 * * Copyright (C) 2016 - aadel112.com - A project by Aaron Adel */ (function ( $ ) { $.fn.googoose = function( options, callback ) { var GG = this; var now = new Date().getTime(); var proto = new RegExp(/^(http|https|file):/); var ab = new RegExp(/^\//); GG.finish = function() { if( options.debug ) GG.debug_fn('finish action'); if (callback) { var blob = new Blob([options.html], { type: 'application/msword' }); callback(null, blob); } else { GG.saveHtmlAsFile(options.filename, options.html); } } var options = $.extend({ // These are the defaults. area: 'div.googoose-wrapper', headerfooterid: 'googoose-hdrftrtbl', margins: '1.0in', zoom: '75', filename: 'Doc1_' + now + '.doc', size: '8.5in 11.0in', display: 'Print', lang: 'en-US', toc: 'div.googoose.toc', pagebreak: 'div.googoose.break', headerarea: 'div.googoose.headerr', footerarea: 'div.googoose.footerr', headerid: 'googoose-headerr', footerid: 'googoose-footerr', headermargin: '.5in', footermargin: '.5in', currentpage: 'span.googoose.currentpage', totalpage: 'span.googoose.totalpage', finishaction: GG.finish, html: null, initobj: document, debugtype: 'alert', debug: 0, maxWidth: 624 }, options ); GG.options = options; //http://requiremind.com/memoization-speed-up-your-javascript-performance/ GG.memoize = function(fn, resolver) { var memoized = function() { resolver = resolver || JSON.stringify; var cache = memoized.cache; var args = Array.prototype.slice.call(arguments); var key = resolver.apply(this, args); if(key in cache) { GG.debug_fn('hit cache'); return cache[key]; } var result = fn.apply(this, arguments); cache[key] = result; return result; }; memoized.cache = {}; return memoized; } GG.debug_fn = function( args ) { options.debugtype == 'console' ? console.log( args ) : alert( args ); } //http://stackoverflow.com/questions/18755750/saving-text-in-a-local-file-in-internet-explorer-10 GG.saveHtmlAsFile = function( fileNameToSaveAs, textToWrite ) { /* Saves a text string as a blob file*/ var ie = navigator.userAgent.match(/MSIE\s([\d.]+)/), ie11 = navigator.userAgent.match(/Trident\/7.0/) && navigator.userAgent.match(/rv:11/), ieEDGE = navigator.userAgent.match(/Edge/g), ieVer=(ie ? ie[1] : (ie11 ? 11 : (ieEDGE ? 12 : -1))); if (ie && ieVer<10) { console.log("No blobs on IE ver<10"); return; } var textFileAsBlob = new Blob([textToWrite], { type: 'application/msword' }); if (ieVer>-1) { window.navigator.msSaveBlob(textFileAsBlob, fileNameToSaveAs); } else { var downloadLink = document.createElement("a"); downloadLink.download = fileNameToSaveAs; downloadLink.href = window.URL.createObjectURL(textFileAsBlob); downloadLink.onclick = function(e) { document.body.removeChild(e.target); }; downloadLink.style.display = "none"; document.body.appendChild(downloadLink); downloadLink.click(); } } // http://stackoverflow.com/questions/7394748/whats-the-right-way-to-decode-a-string-that-has-special-html-entities-in-it GG.decodeHtmlEntity = function(str) { return str.replace(/(\d+);/g, function(match, dec) { return String.fromCharCode(dec); }); } GG.translate_mso_features = function( html ) { if( options.debug ) GG.debug_fn('GG.translate_mso_features'); html = GG.decodeHtmlEntity(html); html = GG.remove_bad_tags(html); html = GG.convert_pagebreaks(html); html = GG.convert_toc(html); html = GG.convert_hdrftr(html); html = GG.convert_imgs(html); return html; } GG.remove_bad_tags = function( html ) { if( options.debug ) GG.debug_fn('GG.remove_bad_tags'); var thtml = $(html); thtml.find('noscript').each(function() { $(this).replaceWith(''); }); thtml.each(function() { if($(this).is(':hidden')){ $(this).remove(); } }); html = thtml[0].outerHTML; return html; } GG.convert_pagebreaks = function( html ) { if( options.debug ) GG.debug_fn('GG.convert_pagebreaks'); //user decides in html what will be a page break in word, this converts to a page break if( options.pagebreak ) { var thtml = $(html); thtml.find(options.pagebreak).replaceWith(GG.get_pagebreak()); html = thtml[0].outerHTML; } return html; } GG.convert_toc = function( html ) { if( options.debug ) GG.debug_fn('GG.convert_toc'); //user determines in html what will be the toc in word if( options.toc && $(options.toc).length ) { var thtml = $(html); thtml.find(options.toc).replaceWith(GG.get_toc_contents()); html = thtml[0].outerHTML; } return html; } GG.convert_hdrftr = function (html) { if (options.debug) GG.debug_fn('GG.convert_hdrftr'); var hvis = options.headerarea && $(options.headerarea).length; var fvis = options.footerarea && $(options.footerarea).length; if (hvis || fvis) { var thtml = $('
\n'; return html; } GG.headerend = function() { if( options.debug ) GG.debug_fn('GG.headerend'); return '
\n'; toc += '\n'; toc += 'Table of content - Please right-click and choose "Update fields".\n'; toc += '\n'; toc += '
\n'; return toc; } //TODO - figure out a way to simulate a right mpuse click, update fields GG.include_css = function( html ) { if( options.debug ) GG.debug_fn('GG.include_css'); //adding any header information that may be pertinent in the copied html var tags = ['style', 'link']; for( i = 0; i < tags.length ; ++i ) { $(document).find(tags[i]).each( function( ) { if(tags[i] != 'link' || ($(this).attr('rel') == 'stylesheet' && proto.test($(this).attr('href')))) { html += ( '\n' + $(this)[0].outerHTML + '\n' ); } } ); } return html; } GG.html = function() { if( options.debug ) GG.debug_fn('GG.html'); if( !$(options.area).length ) { return null; } // // fixes IE pre tag handling // $('pre').each(function() { // $(this)[0].outerHTML = $(this)[0].outerHTML.replace(/\n/g, "