Here I come with the one quick tip related to HTML5 and JavaScript. This is about to take webpage screenshot using HTML5 and JavaScript code only. Yes you have read it correct its using HTML5 and JavaScript only. :)
[gads]
Without wasting much time let’s have a look at code and yes working example for sure.
[cc lang=”js”]
(function (exports) {
function urlsToAbsolute(nodeList) {
if (!nodeList.length) {
return [];
}
var attrName = ‘href’;
if (nodeList[0].__proto__ === HTMLImageElement.prototype
|| nodeList[0].__proto__ === HTMLScriptElement.prototype) {
attrName = ‘src’;
}
nodeList = [].map.call(nodeList, function (el, i) {
var attr = el.getAttribute(attrName);
if (!attr) {
return;
}
var absURL = /^(https?|data):/i.test(attr);
if (absURL) {
return el;
} else {
return el;
}
});
return nodeList;
}
function screenshotPage() {
urlsToAbsolute(document.images);
urlsToAbsolute(document.querySelectorAll(“link[rel=’stylesheet’]”));
var screenshot = document.documentElement.cloneNode(true);
var b = document.createElement(‘base’);
b.href = document.location.protocol + ‘//’ + location.host;
var head = screenshot.querySelector(‘head’);
head.insertBefore(b, head.firstChild);
screenshot.style.pointerEvents = ‘none’;
screenshot.style.overflow = ‘hidden’;
screenshot.style.webkitUserSelect = ‘none’;
screenshot.style.mozUserSelect = ‘none’;
screenshot.style.msUserSelect = ‘none’;
screenshot.style.oUserSelect = ‘none’;
screenshot.style.userSelect = ‘none’;
screenshot.dataset.scrollX = window.scrollX;
screenshot.dataset.scrollY = window.scrollY;
var script = document.createElement(‘script’);
script.textContent = ‘(‘ + addOnPageLoad_.toString() + ‘)();’;
screenshot.querySelector(‘body’).appendChild(script);
var blob = new Blob([screenshot.outerHTML], {
type: ‘text/html’
});
return blob;
}
function addOnPageLoad_() {
window.addEventListener(‘DOMContentLoaded’, function (e) {
var scrollX = document.documentElement.dataset.scrollX || 0;
var scrollY = document.documentElement.dataset.scrollY || 0;
window.scrollTo(scrollX, scrollY);
});
}
function generate() {
window.URL = window.URL || window.webkitURL;
window.open(window.URL.createObjectURL(screenshotPage()));
}
exports.screenshotPage = screenshotPage;
exports.generate = generate;
})(window);
[/cc]
Demo
[gads]
I have created a demo page for above code so you can see that in action. Live Example.
I left this article open for discussion, so it would be great if you post your views, comments, suggestion via comment form below.
As usual you have posted with a few extremely intriguing elements and also
I have already added this web-site to one I’ll pay attention to ! ! !
Hi colleagues, its great article regarding cultureand completely defined, keep it up all the time.
nice one sir
and how would i save it as a real graphic?
Is it possible the result image to be saved into a file?
Avinash sir
this good article
so pls tell me how to save this snapshot in image in c#
Heyyy, I would like to use this code but the example is not working and I have some questions. Would you reupload the example? Very thank you
Nifty. But what’s the point if one can’t save the image or do anything with it except seeing it in another tab? I am curious to learn more about what you did there though.
Unfortunately the demo site is no longer there. Any mirror available?
Will this work in an iframe? I want to use this in a Google Maps iframe to automatically get a snapshot of the map. :(
if you want to take snap shot of google map..use html2canvas and while using that make “userCors: True” this will convert your google map into dataurl and you can save it as image
Avinash sir can you tell how to take snapshot remote website without opening it?
You Should Use The Phantom JS For That Which Can Easily Provide you the images without opening the url.
Its up now, thanks for pointing…
Hi avinash;
is there a way to get the blob inside an image control ?
i tried the code, but me i don’t get any img or css…. any idea why?
Do you have link available to review?
All this does in your example is open the code in a new tab. No screenshot is taken. Am I missing something?
please say the method to save the image
Have you found a solution to save the resulting image ? Thanks a lot anyway..
I am unable to use this code plaese sir let u know me how to use to save the image created by the code.
Hi Avinash, I have a question can we assign the blob to base64 and then save it or any other method is available to save the screen shot .Please let me know about it.