有两种解决方案有效,但都有缺陷。一种仅在您的请求花费的时间少于一秒的情况下才有效,并且一种已被弃用,因此不应在生产环境中使用它。
第一个是使用setTimeout,这是为数不多的不会失去execCommand “特权”的异步函数之一。但它不会丢失,前提是它等于或小于 1000ms。因此,如果您的要求少于此要求,那么您就可以开始了,但如果不是,那么您就会出错。如果将它与某种超时处理结合起来,它可以工作,但如果请求经常花费超过 1 秒,那么它可能还不够好。像这样的例子:
var toCopy;
const buttonClick = () => {
setTimeout(function() {
if (toCopy) {
// strangely, this execCommand will work
console.log(document.execCommand('copy'));
}
}, 1000);
var client = new XMLHttpRequest();
client.onload = function(data) {
toCopy = this.responseText;
};
// by setting your timeout on your request
// with the same duration as the setTiemout
// you make sure it either works or throws an error
client.ontimeout = function(data) {
console.log('timeout');
};
client.timeout = 1000;
client.open("GET", "https://www.random.org/integers/?num=1&min=1&max=100&col=1&base=10&format=plain");
client.send();
}
$(() => {
$("button").click(buttonClick)
})
document.addEventListener('copy', function(e) {
e.preventDefault();
e.clipboardData.setData('text/plain', toCopy);
});
https://jsfiddle.net/8md4Ltu2/4/
还有另一种让它工作的方法,但它已被弃用,所以不要使用。但是,为了彻底,我会把它放在这里。您可以将 XMLHttpRequest 的异步标志设置为 false。请求将是同步的,因此 execCommand 的处理非常简单。但是这个同步标志被弃用了,如果你尝试使用它,指导方针是抛出一个错误,所以它不被使用。见:https ://xhr.spec.whatwg.org/#synchronous-flag
var toCopy;
const buttonClick = () => {
var client = new XMLHttpRequest();
client.onload = function(data) {
toCopy = this.responseText;
console.log(document.execCommand('copy'));
};
client.open("GET", "https://www.random.org/integers/?num=1&min=1&max=100&col=1&base=10&format=plain", false);
client.send();
}
$(() => {
$("button").click(buttonClick)
})
document.addEventListener('copy', function(e) {
e.preventDefault();
e.clipboardData.setData('text/plain', toCopy);
});
https://jsfiddle.net/zezskm2x/2/