<!-- 改善されたコピーボタンのシステムのスクリプト(2026年2月18日_12時51分) -->
<script>
async function copyRichText(elementId, btn) {
const node = document.getElementById(elementId);
const originalText = btn.textContent; // 元のボタンの文字を保存
// 成功/失敗時にボタンの文字を変えて戻す関数
const updateButton = (msg) => {
btn.textContent = msg;
setTimeout(() => {
btn.textContent = originalText;
}, 1500);
};
try {
// 【第1段階】 最新のClipboard APIを試す
if (navigator.clipboard && navigator.clipboard.write) {
const htmlBlob = new Blob([node.innerHTML], { type: 'text/html' });
const textBlob = new Blob([node.innerText], { type: 'text/plain' });
await navigator.clipboard.write([
new ClipboardItem({
'text/plain': textBlob,
'text/html': htmlBlob
})
]);
updateButton('コピーしました');
} else {
// APIが使えない環境ならエラーを投げてcatchへ移動
throw new Error('Clipboard API not supported');
}
} catch (err) {
// 【第2段階】 エラーが出たら、昔ながらの方法(execCommand)で救済する
console.warn('最新APIでのコピーに失敗しました。予備の方法を実行します:', err);
try {
const selection = window.getSelection();
const range = document.createRange();
selection.removeAllRanges(); // 選択範囲をクリア
range.selectNodeContents(node); // 要素の中身を選択
selection.addRange(range); // 選択状態にする
const successful = document.execCommand('copy'); // コピー実行
selection.removeAllRanges(); // 選択解除
if (successful) {
updateButton('コピーしました');
} else {
updateButton('コピー失敗');
}
} catch (fallbackErr) {
console.error('予備のコピー方法も失敗しました:', fallbackErr);
updateButton('コピー失敗');
}
}
}
</script>
Twitterの台詞セット、全部アカウント【Mark4】
0投資