Javascript-将所有HTML代码复制到剪贴板 [英] Javascript - copy all html code into clipboard
问题描述
我正在制作一个必须从特殊网页检查信息的应用程序.我需要做的是将该页面的html内容传递给我现有的程序,其余所有工作都将完成.
I am making an app which has to check an information from a special webpage. What I need to do is to pass the html content from this page to my existing program, and it does all the rest.
我获取数据的网站可以在IE8和更高版本中使用,因此它可以缩小问题范围.我需要对IE进行扩展,可以从调用它的页面复制所有html代码(最好将其保存到.txt中),因此结果将如示例所示:
The website where I get the data works in IE8 and newer, so it kinda narrows the problem. I need to make an extension for IE which could copy ALL the html code from the page it has been called from (and save it into a .txt, in the best case), so result would be as on the example:
<html>
<body>
Hello world
</body>
</html>
我知道如何进行此类扩展,唯一的问题是javascript:我是新手.这个问题有解决的办法吗?
I know how to make such extensions, the only problem is javascript: I am a newbie. Are there any short solutions for this problem?
推荐答案
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>
这篇关于Javascript-将所有HTML代码复制到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!