Use socket programming to communicate with Java in Firefox plugins

Source: Internet
Author: User
Tags sin

The reason to write this blog post, because I usually use selenium this component, about the Browser tab management This problem I have a great headache, and has not been a good solution. Selenium this component is very powerful, but it's this browser window management mechanism Let it in my mind directly from 100 points to 80. Because of this problem, I learned today in the Firefox addon using the socket to listen to my Java request, and then by myself to call the Firefox Tabs API tab page Management. Interested friends can look down!

First, be familiar with several socket-related core xpcom components

    1. Nsiserversocket Opening a socket service
    2. Nsiserversocketlistener Socket request Listener, it has two interface methods onsocketaccepted and onstoplistening. Where onsocketaccepted is used to handle socket requests and responses. Onstoplistening This is supposed to execute some code when the listener is destroyed, without actually testing it.
    3. Nsitransport This component provides some APIs to access the input and output streams in a socket request. It is generally used to read the contents of the socket request in the Nsiserversocketlistener onsocketaccepted method and to write the custom socket response back to the requesting client via the output stream.

With these components, you can start writing a simple socket communication instance with Firefox add-on as the server and Java as the client, and here's a reference link to these components

    • Https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIServerSocket
    • Https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIServerSocketListener
    • Https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsITransport
    • Https://github.com/icsi-berkeley/fathom/blob/master/src/firefox/modules/ServerSocket.jsm This link is a piece of source code called Fathom Project on GitHub, at first I didn't quite understand nsiserversocketlistener this component, thought to use as declaration Nsiserversocket

      Cc["@mozilla. Org/network/server-socket;1"]
      . CreateInstance (Ci.nsiserversocketlistener) This way to declare it, the results of an error, the source code inside there is a Nsiserversocketlistener use instance. is actually a JSON object, as long as you implement the onsocketaccepted and Onstoplistening methods.

Now you can create a socket service in your Firefox add-on main.js

const {CU,CC,CI} = require ("Chrome"), var serversocket = cc["@mozilla. Org/network/server-socket;1"]                             . CreateInstance (ci.nsiserversocket);  Create a Socket service
Serversocket.init (9638,false,-1); Listening on port 9638

When you run the CFX run to open the browser, you can see that port 9638 is already occupied by Firefox.exe if it works.

Next processing the socket request

Const Nsiserversocket = ci.nsiserversocket;const Nsitransport = Ci.nsitransport;const Nsiscriptableinputstream = ci.nsiscriptableinputstream;//Create socket listener var listener = {  Onsocketaccepted:function (socket, transport) {// Process the received data  var input = Transport.openinputstream (nsitransport.open_blocking,0,0); var sin = cc["@mozilla. org/ Scriptableinputstream;1 "].createinstance (Nsiscriptableinputstream); sin.init (input); var resp =" ";d o {  resp = Sin.read (512);} while (resp = = ""), while (sin.available () > 0) Resp + = Sin.read (+); Console.log (resp);  },  onstoplistening: function (socket, status) {  }}

Start the listener.

Serversocket.asynclisten (listener);

Write another Java to test, the code is simple, create a new socket request and then send a string

    try {Socket socket = new socket ("localhost", 9638); OutputStream outputstream = Socket.getoutputstream (); O Utputstream.write ("Dfasdfad". GetBytes ()); Outputstream.close (); Socket.close ();} catch (IOException e) {e.printstacktrace ();}    

Finally, after running the Java program, you can see it in the Firefox Add-on SDK console

Here a simple FF addon socket program is written, the latter can be based on specific needs to do something else, such as sending a string "Open_new_tab" and then bring a URL address to this add-on, It opens a new tab page and loads the given URL.

Because the primary language did not learn, so the above example of the new friends may be more dizzy, so it is best to learn the most basic Firefox-addon development and then look at this article, or CFX run is not what you mean to see the words are difficult

Here is the source code address: HTTP://YUNPAN.CN/CQ6SCFDD4QSDQ (extract code: D400)

Use socket programming to communicate with Java in Firefox plugins

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.