html copy text,Javascript - Copy string to clipboard as text/html

Since this answer has gotten some attention, I have completely rewritten the messy original to be easier to grasp. If you want to look at the pre-revisioned version, you can find it here.

The boiled down question:

Can I use JavaScript to copy the formatted output of some HTML code to the users clipboard?

Answer:

Yes, with some limitations, you can.

Solution:

Below is a function that will do exactly that. I tested it with your required browsers, it works in all of them. However, IE 11 will ask for confirmation on that action.

Explanation how this works can be found below, you may interactively test the function out in this jsFiddle.

// This function expects an HTML string and copies it as rich text.

function copyFormatted (html) {

// Create container for the HTML

// [1]

var container = document.createElement('div')

container.innerHTML = html

// Hide element

// [2]

container.style.position = 'fixed'

container.style.pointerEvents = 'none'

container.style.opacity = 0

// Detect all style sheets of the page

var activeSheets = Array.prototype.slice.call(document.styleSheets)

.filter(function (sheet) {

return !sheet.disabled

})

// Mount the container to the DOM to make `contentWindow` available

// [3]

document.body.appendChild(container)

// Copy to clipboard

// [4]

window.getSelection().removeAllRanges()

var range = document.createRange()

range.selectNode(container)

window.getSelection().addRange(range)

// [5.1]

document.execCommand('copy')

// [5.2]

for (var i = 0; i < activeSheets.length; i++) activeSheets[i].disabled = true

// [5.3]

document.execCommand('copy')

// [5.4]

for (var i = 0; i < activeSheets.length; i++) activeSheets[i].disabled = false

// Remove the container

// [6]

document.body.removeChild(container)

}

Explanation:

Look into the comments in the code above to see where you currently are in the following process:

We create a container to put our HTML code into.

We style the container to be hidden and detect the page's active stylesheets. The reason will be explained shortly.

We put the container into the page's DOM.

We remove possibly existing selections and select the contents of our container.

We do the copying itself. This is actually a multi-step process:

Chrome will copy text as it sees it, with applied CSS styles, while other browsers will copy it with the browser's default styles.

Therefore we will disable all user styles before copying to get the most consistent result possible.

Before we do this, we prematurely execute the copy command.

This is a hack for IE11: In this browser, the copying must be manually confirmed once. Until the user clicked the "Confirm" button, IE users would see the page without any styles. To avoid this, we copy first, wait for confirmation, then disable the styles and copy again. That time we won't get a confirmation dialog since IE remembers our last choice.

We actually disable the page's styles.

Now we execute the copy command again.

We re-enable the stylesheets.

We remove the container from the page's DOM.

And we're done.

Caveats:

The formatted content will not be perfectly consistent across browsers.

As explained above, Chrome (i.e. the Blink engine) will use a different strategy than Firefox and IE: Chrome will copy the contents with their CSS styling, but omitting any styles that are not defined.

Firefox and IE on the other hand won't apply page-specific CSS, they will apply the browser's default styles. This also means they will have some weird styles applied to them, e.g. the default font (which is usually Times New Roman).

For security reasons, browsers will only allow the function to execute as an effect of a user interaction (e.g. a click, keypress etc.)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值