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

前端 未结 3 704
别跟我提以往
别跟我提以往 2020-12-12 05:42

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

相关标签:
3条回答
  • 2020-12-12 05:57

    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).

    0 讨论(0)
  • 2020-12-12 06:05

    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.

    0 讨论(0)
  • 2020-12-12 06:14

    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
    
    0 讨论(0)
提交回复
热议问题