Firefox:从内容脚本返回值

Firefox: Returning a value from a content script

执行内容脚本时,对于活动选项卡,有没有办法让该内容脚本return执行脚本的弹出窗口的值?

Chromen 也有类似的问题 - 但 FF 没有。

有一种虚拟啤酒可以帮助您!

谢谢,

在 MDN 上查看 this page 关于内容脚本

如果您正在使用高级 API 选项卡(我认为您是),则需要执行以下操作:


向脚本发送数据

要向内容脚本发送消息,请在 var my_tab = tab.attach({ ... }) 下方添加以下内容:

my_tab.port.emit("my_message", "Message from the add-on")

然后在脚本中你可以使用它来监听消息:

self.port.on("my_message", function(data) {
    // Do stuff here!
    // data contains the data sent (i.e. "Message from the add-on")
})

从脚本向 index.js 发送数据

这可能是您感兴趣的部分。

要从脚本发送数据,请使用以下代码:

self.port.emit("my_response", "Response from content script")

并在 index.js 收到它:

my_tab.port.on("my_response", function(data) {
    // Do stuff here!
    // data contains the data sent (i.e. "Response from content script")
})