问题
I need to be able to get the URL from the active tab in Firefox. DDE doesn't work with multiple instances so I was thinking that I could build an addon that sets a global atom or something.
I also thought that maybe I could use the clipboard, but I don't want to overwrite any existing text and custom clipboard types doesn't seem to be supported.
I don't want to resort to writing a file just to do simple IPC...so before I do it...is there a better choice for something so simple.
thanks
回答1:
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).
回答2:
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
回答3:
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.
来源:https://stackoverflow.com/questions/8525428/whats-the-best-way-to-communicate-with-a-firefox-addon