admin管理员组文章数量:1430706
Say, in my Chrome extension I need to copy a user formatted HTML text on to Clipboard. The text (in its HTML form) may be something like this:
This is a <b>test</b><div><br></div>
<div>And this is too</div><div><br></div><div>Thank you</div>
If I use this JavaScript example, all I get copied is HTML markup from above. But I'm wondering, can I copy it as formatted text, or this?
This is a test
And this is too
Thank you
Say, in my Chrome extension I need to copy a user formatted HTML text on to Clipboard. The text (in its HTML form) may be something like this:
This is a <b>test</b><div><br></div>
<div>And this is too</div><div><br></div><div>Thank you</div>
If I use this JavaScript example, all I get copied is HTML markup from above. But I'm wondering, can I copy it as formatted text, or this?
Share Improve this question edited May 23, 2017 at 12:09 CommunityBot 11 silver badge asked Sep 4, 2014 at 5:03 c00000fdc00000fd 22.5k37 gold badges203 silver badges442 bronze badgesThis is a test
And this is too
Thank you
1 Answer
Reset to default 3Once crbug./395376 is fixed, you can declare the clipboardWrite
permission in your manifest file and simply use the folllowing code from yuor content script:
var element = document.body; // Example, select the "whole" document
// Change selected area
var r = document.createRange();
r.selectNode(element);
var s = window.getSelection();
s.removeAllRanges();
s.addRange(r);
// Copy - requires clipboardWrite permission + crbug./395376 must be fixed
document.execCommand('copy');
Until the previous bug is fixed, you have to pass the data to the background page and copy the message from there. This solution is not optimal because you're going to insert untrusted HTML in your background page. See https://stackoverflow./a/25275151 for examples on how using innerHTML
for copying can be abused.
If you understand the risks associated with using innerHTML, and you're accepting its consequences, then you could use the following code to copy rich text:
// content script
var element = document.body; // Example
chrome.runtime.sendMessage({
html: 'copyhtml',
text: element.outerHTML
});
background page:
chrome.runtime.onMessage.addListener(function(message) {
if (message && message.type == 'copyhtml') {
var wrapper = document.createElement('div');
// WARNING: Potentially insecure!
wrapper.innerHTML = message.html;
document.body.appendChild(wrapper);
var range = document.createRange();
r.selectNode(wrapper);
var s = window.getSelection();
s.removeAllRanges();
s.addRange(r);
// Copy - requires clipboardWrite permission
document.execCommand('copy');
wrapper.remove();
}
});
(if the reader wants to copy text instead of rich text, see Clipboard Copy / Paste on Content script (Chrome Extension))
本文标签: javascriptHow to copy HTML formatted text on to Clipboard from Google Chrome extensionStack Overflow
版权声明:本文标题:javascript - How to copy HTML formatted text on to Clipboard from Google Chrome extension? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745544045a2662622.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论