テキストを入力するために隠しテキスト領域を使用し、それを選択してからdocument.execCommandを使用してクリップボードにコピーします。これは通常動作しますが、テキストが大きい場合は失敗します(falseを返します)。 Chrome v55では、約180K文字が失敗するようです。大文字のテキストでdocument.execCommand( 'copy')を使用してクリップボードにコピーするとエラーが発生します
この方法でコピーできるデータ量には制限がありますか?通常のCtrl + Cは同じ制限を受けていないようです。
注:誰かがこれをDoes document.execCommand('copy') have a size limitation?の可能な複製としてマークしました。似たような質問かもしれませんが、私が使用していない特定のフレームワークとしてタグ付けされており、答えられていませんでした。私は私の質問がより一般的であり、なおも適切であると信じています。
参考のためにコードを添付します。
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.execCommand( 'コピー')はサイズの制限がありますか?](https://stackoverflow.com/questions/43641182/does-document-execcommandcopy-サイズ制限あり) –
編集を参照してください。その質問は解決されなかった。 –