【发布时间】:2014-08-02 21:05:54
【问题描述】:
我在 Chrome 扩展程序的 background.js 中使用此代码将文本复制到用户的剪贴板:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.command == "copy") {
executeCopy(request.text);
sendResponse({farewell: "copy request received"});
}
}
);
function executeCopy(text){
var copyDiv = document.createElement('div');
copyDiv.contentEditable = true;
document.body.appendChild(copyDiv);
copyDiv.innerHTML = text;
copyDiv.unselectable = "off";
copyDiv.focus();
document.execCommand('SelectAll');
document.execCommand("Copy", false, null);
document.body.removeChild(copyDiv);
}
它复制带有格式的文本。如何以没有格式的纯文本复制文本?
【问题讨论】:
标签: javascript google-chrome text google-chrome-extension copy-paste