Take screenshot of the whole webpage

天涯浪子 提交于 2020-01-03 06:41:31

问题


Need a working code with only javascript and no extra library files (-> jquery,...) That take a screenshot from the whole webpage. (top to bottom page)

Current i have this which does not work why?

<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function () {

var window = document.getElementById('item');
var canvas = document.getElementById('my-canvas');
var ctx = canvas.getContext("2D");
ctx.drawImage(window, 0,0, 100, 200, "rgb(255,255,255)");
canvas.toDataURL("image/png");

});
</script>
</head>
<body id="item">
test website
<canvas id='my-canvas'></canvas>

回答1:


It's wasn't easy but for the other people look to the browser engine. Because thanks to the use of the blob (kindly clone the whole webpage).

urlsToAbsolute(document.images);
urlsToAbsolute(document.querySelectorAll("link[rel='stylesheet']"));
urlsToAbsolute(document.scripts);

var screenshot = document.documentElement.cloneNode(true);
var blob = new Blob([screenshot.outerHTML], {type: 'text/html'});
window.open(window.URL.createObjectURL(blob));
screenshot.dataset.scrollX = window.scrollX;
screenshot.dataset.scrollY = window.scrollY;
screenshot.style.pointerEvents = 'none';
screenshot.style.overflow = 'hidden';
screenshot.style.userSelect = 'none';

var script = document.createElement('script');
script.textContent = '(' + addOnPageLoad_.toString() + ')();';
screenshot.querySelector('body').appendChild(script);

window.addEventListener('DOMContentLoaded', function(e) {
    var scrollX = document.documentElement.dataset.scrollX || 0;
    var scrollY = document.documentElement.dataset.scrollY || 0;
    window.scrollTo(scrollX, scrollY);
});

I found you can read more in the chromium project: http://www.html5rocks.com/en/tutorials/streaming/screenshare/




回答2:


You could try something like phantom.js. It is a "headless WebKit scriptable with a JavaScript API" that allows you to do screen capture.

From my experience it works well.



来源:https://stackoverflow.com/questions/15884239/take-screenshot-of-the-whole-webpage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!