[英]how can i remove the event listener of html in chrome extensions?
我正在为 chrome 创建一个扩展,我有一个问题。 我想知道,如何从元素中删除事件? 这是我的事件处理程序:
const handler = function (e) {
console.log("on");
};
我希望我的扩展程序将html
mousemove
。 我做到了。
document.querySelector("html").removeEventListener("mousemove", handler);
我想做一些事情,当用户单击我的扩展图标时,如果徽章文本打开,则从html
中删除事件侦听器; 但我不知道如何从html
标记中删除事件侦听器。 我试过了,但我做不到。
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
function: myFuntion,
args: ["remove"],
});
我试过removeEventListener
但它没有用。 我在全局 scope 中定义了我的 function,但它没有工作,我收到一个错误,提示您的 function 未定义。 我什至尝试将 function 存储在 chrome 存储中。 等等? 但是在为 chrome 开发扩展程序时,如何从 html 标记中删除事件侦听器。 谢谢你的帮助。
当executeScript注入的代码运行时,每次都会创建一个新的function,所以之前的function不能去掉,因为它的内部指针现在不一样了。
您可以通过 window 持久化window
:
window.handler = window.handler || function (e) {
console.log("on");
};
然后您可以稍后在另一个 executeScript 中将其删除:
document.querySelector("html").removeEventListener("mousemove", window.handler);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.