'Export iframe content as an HTML file [duplicate]

I'm trying to achieve pretty basic function but still didn't succeed. How it should work - a user clicks a button and the iframe content is being exported as a html file. This is the code i have:

HTML:

<input type="button" value="Export" onclick="exportLog();">
<iframe id="log" src="https://colorfill.pl/email_signature.html">
</iframe>

JS:

function exportLog(){
    var elHtml = document.getElementById('log').innerText;
    var link = document.createElement('a');
    var mimeType = 'text/html';

    link.setAttribute('download', 'logFile');
    link.setAttribute('href', 'data:' + mimeType  +  ';charset=utf-8,' + encodeURIComponent(elHtml));
    link.click();
}

Fiddle: https://jsfiddle.net/hbvxkmnf/

I've also tried to change innerText into innerHTML, so far without success..

At the moment script downloads the file yet its totally empty.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source