是否可以监听 HTML 页面文档中未发生的复制事件,例如浏览器窗口的 URL 栏?
也许有一个 Chrome 扩展 API 或一个我忽略的巧妙解决方案?
最佳答案
这是可能的解决方案,不是最好的解决方案,但总比没有好。
它监视文本剪贴板中的 URL,如果粘贴的 URL 与当前选项卡中的 URL 相同 - 我们可以认为它是从多功能框复制的。
(编辑: list v2 解决方案)
background.js:
// create element for pasting
const textEl = document.createElement('textarea');
document.body.appendChild(textEl);
var prevPasted = '';
setInterval(function () {
// paste text from clipboard to focused textarea
textEl.focus();
textEl.value = '';
document.execCommand('paste');
const pastedText = textEl.value;
// simple cache check
if (pastedText !== prevPasted) {
prevPasted = pastedText;
if (pastedText.match(/https?:/)) { // you can improve you URL scheme
// get the current tab
chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
var tab = tabs[0];
// check if current tab has the same URL
if (tab.url === pastedText) {
console.log('Omnibox URL:', pastedText);
}
});
}
}
}, 500);
不要忘记将clipboardRead和选项卡权限添加到 list 中。
关于javascript - Chrome 扩展程序 : Detect copy action from Window's URL bar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45259036/