有人能挂起新异步剪贴板API的剪贴板事件吗?我正在尝试这样做:
navigator.clipboard.addEventListener('clipboardchange', function (e) {
console.log("navigator clipboardchange");
});但它从不开火。
发布于 2021-12-20 09:13:36
至少对于chrome还没有实现,有关更多细节,请参见这只虫子。
读了这些评论后,似乎确实存在着实施它的压力,否则你将不得不投票
发布于 2022-11-22 17:03:33
我知道它并没有真正回答这个问题,但这也是一个同样的问题:
async startClipboardPoll () {
try {
let clipboardContent;
// Comment the following line
// if you want to run <CODE> when clipboard change from other pages
// (when the user focus back on your app)
window.onfocus = async function () { clipboardContent = await navigator.clipboard.readText() }
while (true) {
await new Promise(r => setTimeout(r, 500)) // wait
const previousContent = clipboardContent
try {
clipboardContent = await navigator.clipboard.readText()
} catch (e) {
// This function may fail if you focus another webpage
// just ignoring and continuing.
continue
}
if (previousContent == undefined) {
continue
}
if (clipboardContent !== previousContent) {
/*******************
* Clipboard changed !
* <CODE> here
* `clipboardContent` is the clipboard content
******************/
}
}
} catch (e) {
console.warn('something went wrong');
// maybe warn the user here this functionality is not available
}
}然后在代码中的某个地方调用此函数。
https://stackoverflow.com/questions/67473023
复制相似问题