2015-09-07 62 views
0

我正在制作一个应用程序,它必须检查特殊网页中的信息。我需要做的是将这个页面的html内容传递给我现有的程序,其余部分完成。Javascript - 将所有html代码复制到剪贴板

我得到的数据在IE8和更新的网站,所以它有点缩小了问题。我需要为IE做一个扩展,它可以从所调用的页面中复制所有的html代码(并保存在一个.txt文件中,最好的情况下),所以结果将如下例所示:

<html> 
<body> 
Hello world 
</body> 
</html> 

我知道如何做这样的扩展,唯一的问题是javascript:我是一个新手。有没有针对这个问题的简短解决方案?

+0

你用Google搜索? –

回答

0

有许多可供选择使用的XMLSerializer

var Source = new XMLSerializer().serializeToString(document); 

2)

document.documentElement.outerHTML 
    or 

    document.documentElement.innerHTML 
0

尝试

1)本:

我从here

了参考

function copyTextToClipboard(text) { 
 
    var textArea = document.createElement("textarea"); 
 
    textArea.style.position = 'fixed'; 
 
    textArea.style.top = 0; 
 
    textArea.style.left = 0; 
 
    textArea.style.width = '2em'; 
 
    textArea.style.height = '2em'; 
 
    textArea.style.padding = 0; 
 
    textArea.style.border = 'none'; 
 
    textArea.style.outline = 'none'; 
 
    textArea.style.boxShadow = 'none'; 
 
    textArea.style.background = 'transparent'; 
 
    textArea.value = text; 
 
    document.body.appendChild(textArea); 
 
    textArea.select(); 
 
    try { 
 
    var successful = document.execCommand('copy'); 
 
    var msg = successful ? 'successful' : 'unsuccessful'; 
 
    console.log('Copying text command was ' + msg); 
 
    } catch (err) { 
 
    console.log('Oops, unable to copy'); 
 
    } 
 
    document.body.removeChild(textArea); 
 
} 
 
$(document).ready(function() { 
 
    $("#btnDate").click(function() { 
 
    var allHTML = document.documentElement.outerHTML; 
 
    copyTextToClipboard(allHTML); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<button type="button" id="btnDate" class="btn btn-primary">Copy HTML 
 
</button>