如何在没有任何弹出窗口的情况下将此屏幕截图保存在本地驱动器中?

时间:2018-02-28 10:29:16

标签: javascript

我想将此屏幕截图保存在本地驱动器中,而不显示要下载的弹出窗口。

    (function() {
        'use strict';
        $("body").prepend ( `
    <button id="btCapture">SCREENSHOT</button>
    <input type="submit" value="Take Screenshot Of Div" onclick="capture();" />
    <form method="POST" enctype="multipart/form-data" action="save.php" id="myForm">
    <input type="hidden" name="img_val" id="img_val" value="" />
    </form>
    <div></div>`);

    document.getElementById("btCapture").onclick = function() { generate();};
        (function (exports) {
            function urlsToAbsolute(nodeList) {
                if (!nodeList.length) {
                    return [];
                }
                var attrName = 'href';
                if (nodeList[0].prototype === HTMLImageElement.prototype || nodeList[0].protottype === 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);
    })

;

1 个答案:

答案 0 :(得分:0)

你不能。允许网页将文件写入最终用户计算机上的任意路径将是恶意软件利用的 大量 漏洞。用户必须参与其中。