自己解決しました。 Clipboard APIの失敗時、非推奨のexecCommand('copy')を実行するように変更すると、Android端末でもコピーできました。
async copyTextToClipboard(text) {
try {
await navigator.clipboard.writeText(text);
console.log('Async: Copying to clipboard was successful!');
return true;
} catch (err) {
try {
console.log('Async: Could not copy text: ', err);
var input = document.createElement('input');
document.body.appendChild(input);
input.value = text;
input.select();
const result = document.execCommand('copy');
document.body.removeChild(input);
if (!result) {
throw new Error('document.execCommand failed');
}
console.log('document.execCommand: Copying to clipboard was successful!');
return true;
} catch (err) {
alert('コピーに失敗しました。');
console.error('document.execCommand: Could not copy text: ', err);
return false;
}
}
}
非推奨の機能を使用するのは避けたいところですが、今のところ問題なさそうですのでこちらで実装いたします。
- 0