Javascript -复制所有的html代码到剪贴板

Javascript - copy all html code into clipboard

本文关键字:html 代码 剪贴板 复制 Javascript      更新时间:2023-09-26

我正在制作一个应用程序,必须从一个特殊的网页检查信息。我需要做的是将这个页面的html内容传递给我现有的程序,然后它完成所有其余的工作。

我得到数据的网站在IE8和更新的版本中工作,所以它有点缩小了问题。我需要做一个扩展的IE,它可以复制所有的html代码从页面它已被调用(并将其保存到一个。txt,在最好的情况下),所以结果将是对例子:

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

我知道如何使这样的扩展,唯一的问题是javascript:我是一个新手。这个问题有什么简短的解决办法吗?

有很多选择

使用XMLSerializer

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

2)

   document.documentElement.outerHTML
   or
   document.documentElement.innerHTML

此处引用

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>