gpt4 book ai didi

javascript - 与 DOM 交互的 Firefox 扩展

转载 作者:行者123 更新时间:2023-11-29 21:47:41 25 4
gpt4 key购买 nike

我正在开发一个带有选项面板的 firefox 扩展,用于与当前选项卡 DOM 进行交互。当您选择一个选项时,事件选项卡的 DOM 中的所有元素都会在鼠标悬停时突出显示,当您单击一个元素时,我想根据面板中选择的选项触发不同的操作。我已经让它工作了,但是当您选择一个选项然后选择其他选项时,内容脚本“opt”变量具有这两个值。

主要.js

panel.port.on('select', function(sel){
attachScript(sel);
panel.hide();
});

function attachScript(selected)
{
var worker = tabs.activeTab.attach({
contentScriptFile: [self.data.url("js/jquery-1.11.3.min.js"), self.data.url("js/content.js")]
});

worker.port.emit('option', selected);
}

内容.js

var opt = '';

self.port.on('option', function(option){
$('body').off('click', '.activelem',controlClick);
if (option)
{
$('body').on('click', '.activelem', controlClick);
opt = option;
window.addEventListener('mousemove', handler);
}
});

我的问题:

  • 每次我选择面板选项并执行 attachScript 函数时,脚本都会被注入(inject),或者只是第一次注入(inject)?
  • 我是否需要设置 port.once 来注入(inject)脚本,然后使用普通的 port.on?如果是这样,我如何每次都与内容脚本通信?
  • 如何在每次按下 ActionButton 时删除内容脚本中的监听器,以便每次打开面板时都是一个全新的开始。

最佳答案

Every time than I get the panel option selected and execute the attachScript function, the scripts are injected

是的

Do I need to set a port.once to inject the scripts and then use a normal port.on? If this is the way, how do I communicate with the content script every time?

您可能应该跟踪哪些选项卡已经附加了工作人员,weakmap适合那个目的

How can I remove the listener in the content script every time than the ActionButton is pressed, so every time the panel is opened its a fresh start.

尝试 destroy正在工作

关于javascript - 与 DOM 交互的 Firefox 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30523810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com