Xceedas

Xceedas
xceedas

Monday, 17 November 2014

Generate Screenshot Using HTML and JavaScript

Complete Code
Screenshot.html
  1. <html lang="en">  
  2. <head>  
  3.     <title>Take Web Page Screenshot with HTML5 and JavaScript </title>  
  4. </head>  
  5. <body>  
  6.     <a class="btn btn-success" href="javascript:void(0);" onclick="generate();">Generate  
  7.         Screenshot »</a>  
  8.     <script type="text/javascript">  
  9.         (function (exports) {  
  10.             function urlsToAbsolute(nodeList) {  
  11.                 if (!nodeList.length) {  
  12.                     return [];  
  13.                 }  
  14.                 var attrName = 'href';  
  15.                 if (nodeList[0].__proto__ === HTMLImageElement.prototype || nodeList[0].__p                   roto__ === HTMLScriptElement.prototype) {  
  16.                     attrName = 'src';  
  17.                 }  
  18.                 nodeList = [].map.call(nodeList, function (el, i) {  
  19.                     var attr = el.getAttribute(attrName);  
  20.                     if (!attr) {  
  21.                         return;  
  22.                     }  
  23.                     var absURL = /^(https?|data):/i.test(attr);  
  24.                     if (absURL) {  
  25.                         return el;  
  26.                     } else {  
  27.                         return el;  
  28.                     }  
  29.                 });  
  30.                 return nodeList;  
  31.             }  
  32.   
  33.             function screenshotPage() {  
  34.                 urlsToAbsolute(document.images);  
  35.                 urlsToAbsolute(document.querySelectorAll("link[rel='stylesheet']"));  
  36.                 var screenshot = document.documentElement.cloneNode(true);  
  37.                 var b = document.createElement('base');  
  38.                 b.href = document.location.protocol + '//' + location.host;  
  39.                 var head = screenshot.querySelector('head');  
  40.                 head.insertBefore(b, head.firstChild);  
  41.                 screenshot.style.pointerEvents = 'none';  
  42.                 screenshot.style.overflow = 'hidden';  
  43.                 screenshot.style.webkitUserSelect = 'none';  
  44.                 screenshot.style.mozUserSelect = 'none';  
  45.                 screenshot.style.msUserSelect = 'none';  
  46.                 screenshot.style.oUserSelect = 'none';  
  47.                 screenshot.style.userSelect = 'none';  
  48.                 screenshot.dataset.scrollX = window.scrollX;  
  49.                 screenshot.dataset.scrollY = window.scrollY;  
  50.                 var script = document.createElement('script');  
  51.                 script.textContent = '(' + addOnPageLoad_.toString() + ')();';  
  52.                 screenshot.querySelector('body').appendChild(script);  
  53.                 var blob = new Blob([screenshot.outerHTML], {  
  54.                     type: 'text/html'  
  55.                 });  
  56.                 return blob;  
  57.             }  
  58.   
  59.             function addOnPageLoad_() {  
  60.                 window.addEventListener('DOMContentLoaded'function (e) {  
  61.                     var scrollX = document.documentElement.dataset.scrollX || 0;  
  62.                     var scrollY = document.documentElement.dataset.scrollY || 0;  
  63.                     window.scrollTo(scrollX, scrollY);  
  64.                 });  
  65.             }  
  66.   
  67.             function generate() {  
  68.                 window.URL = window.URL || window.webkitURL;  
  69.                 window.open(window.URL.createObjectURL(screenshotPage()));  
  70.             }  
  71.             exports.screenshotPage = screenshotPage;  
  72.             exports.generate = generate;  
  73.         })(window);  
  74.     </script>  
  75. </body>  
  76. </html> 

No comments :