Firefox Native Messaging Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
scsnake
Posts: 23
Joined: 05 Aug 2015, 03:19

Firefox Native Messaging

17 Mar 2017, 09:17

I'm studying the native messaging used in Firefox WebExtension, but it only provides example in Python.

It says:
App side

On the application side, you use standard input to receive messages and standard output to send them.

Each message is serialized using JSON, UTF-8 encoded and is preceded with a 32-bit value containing the message length in native byte order.


Currently I know FileAppend can do the stdout and JXON library can dump object to json string, but how do I produce the "32-bit value" and how to combine this value with following string?
qwerty12
Posts: 423
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

Re: Firefox Native Messaging  Topic is solved

21 Mar 2017, 10:13

Here's a conversion of the Python sample Mozilla provide, using cocobelgica's JXON library:

Code: [Select all] [Expand] [Download] (ping_pong.ahk)GeSHi © Codebox Plus



I tested it here (AutoHotkey A32 and U64 with Firefox 54.0a2) and it worked fine. However, there's an important step you need to perform: with either the compiled script or a copy of an AutoHotkey exe, you need to edit it so that it uses the console subsystem instead of AutoHotkey's default of the GUI subsystem. I used CFF Explorer to do it (Nt Headers -> Optional Header -> Subsystem: Windows Console), but I know Lexikos wrote an AutoHotkey script to do it as well - I just can't remember where. :) After that, just edit the batch file to run your compiled exe/console-subsystem-using-AutoHotkey.exe.
scsnake
Posts: 23
Joined: 05 Aug 2015, 03:19

Re: Firefox Native Messaging

01 Oct 2017, 06:19

I can't get it work on firefox 50 or 55 (32bit, portable). I modified the code slightly as shown below. When the firefox opened, it did show "start" and the first tick count but then no further tick count showed. Until I closed the firefox, numerous tick counts showed up for a short time before it's closed.

If I tried to send message from firefox to app (ahk script) manually from debug console, it showed "Error: File closed" and a message box from autohotkey saying "Could not close the previous instance of this script. Keep wait?"

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

qwerty12
Posts: 423
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

Re: Firefox Native Messaging

01 Oct 2017, 08:45

I didn't link to the rest of the files needed: Clone or use GH's download ZIP feature to have https://github.com/mdn/webextensions-ex ... -messaging somewhere on your computer. Read what's in the README.md file.

I don't know how using a portable copy of Firefox affects this; native messaging requires modifications be made to your registry, so I'd stick with a properly-installed version of Firefox.

I won't repeat what's in the Mozilla guide already that you linked. (I take it you did install the sample extension Mozilla provide.)

I:

  • compiled your script and ran the resulting EXE file through CFF Explorer to change the subsystem to "Windows Console"
  • changed the path in ping_pong.json to "path": "C:\\native-messaging\\app\\ping_pong.exe",
  • ran REG ADD "HKEY_CURRENT_USER\SOFTWARE\Mozilla\NativeMessagingHosts\ping_pong" /ve /d "C:\native-messaging\app\ping_pong.json" /f as per this
  • ran Firefox 56.0b12 and It Just Works

Image
scsnake
Posts: 23
Joined: 05 Aug 2015, 03:19

Re: Firefox Native Messaging

01 Oct 2017, 09:31

I'm not using the ping-pong example but I will take a try. Currently I use a compiled VB program as the native application and everything's fine, except that I'm more familiar with AHK and wondering if AHK could do the same (as a message bridge between another main script and the firefox). Thanks anyway for the demo with File object and ReadUInt function !
scsnake
Posts: 23
Joined: 05 Aug 2015, 03:19

Re: Firefox Native Messaging

02 Oct 2017, 11:10

I tried to use following app as a bridge between Firefox and another AHK script (named FirefoxLogin.exe). So that if the app received message from Firefox (via stdin), it may send the string to FirefoxLogin.exe (via WM_COPYDATA); if FirefoxLogin.exe send a message to app (via WM_COPYDATA), it may send message to Firefox (via stdout). However, it only works in one direction (Firefox -> app -> FirefoxLogin) but not the opposite. Both "Windows GUI" and "Windows Console" work in the same pattern. Through some debugging, the SendMessage from FirefoxLogin to app always fails so the app is unable to receive message from FirefoxLogin. I wonder if Firefox interferes with the app's messaging loop :?:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], RozRoyal, TAC109, Xtra and 51 guests