What's the best way to communicate with a Firefox addon

一曲冷凌霜 提交于 2019-11-28 14:24:22

The usual way of communicating from an application to a Firefox add-on is via TCP sockets. You create an nsIServerSocket instance, call init() on it and then asyncListen(). When the application connects to your socket the method onSocketAccepted of your listener gets called and you get an nsITransport instance that you can read data from or write to (use NetUtil.jsm to read from the input stream asynchronously).

For a relatively simply example implementation see mozSocket.jsm (not using NetUtils.jsm for reading data).

I don't know if its the best way, but I think using MozRepl will help you. MozRepl will make you able to interact with firefox through telnet.

% telnet localhost 4242
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

Welcome to MozRepl.

repl> content.location.href
"http://stackoverflow.com/questions/8525428/whats-the-best-way-to-communicate-with-a-firefox-addon"
repl> 

After installing MozRepl, You can use this little ruby script to get the url of currently opend tab.

require 'net/telnet'

t = Net::Telnet.new('Port' => 4242)
t.waitfor(/repl.*>/)
puts eval(t.cmd("content.location.href").split[0])
t.close

I wonder if this has been implemented in Firefox yet or if it's still in the idea phase: Mozilla Notifications API.

Google has GCM for Chrome extensions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!