Jump to content

Google Chrome "Native Messaging"


Recommended Posts

Greetings,

Is possible build a script to send/receive commands in "Native Messagin" ?

If you install this extension:
https://chrome.google.com/webstore/detail/native-messaging-example/mefhabneoickhhbiglkmehhmemefkopf/related

And this complement:
https://github.com/Mandar-Shinde/Google-Chrome-Native-Messaging/releases/download/V1.0/GoogleNativeClient.exe

You have a work example to send / receive messages from Google Chrome's plugin and NativeMessagingApp.exe.

I found the source NativeMessagingApp.exe's here:
https://github.com/Mandar-Shinde/Google-Chrome-Native-Messaging

I try build this script... But not work...
 

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_Outfile=C:\Users\Public\GoogleNativeClient\NativeMessagingApp2.exe
#AutoIt3Wrapper_UseUpx=y
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#NoTrayIcon

#include-once
#include <Array.au3>
#include <JSMN.au3> ; https://github.com/chechelaky/AutoIt/blob/master/JSMN.au3
#include <Debug.au3>

Global $msg = "@empty"

Global Const $CLIENT_START = '{"text":"Client Started"}'
Global Const $CLIENT_STOP = '{"text":"Client Stopped"}'
Global Const $CLIENT_CLICK = '{"text":"Client Click"}'

_DebugSetup("NativeMessagingApp2", True, 4, @ScriptDir & "\NativeMessagingApp2.log", True)

Global $read, $json, $out = ""

If IsArray($CmdLine) Then
;~  $msg = @LF & "Google Chrome Native Messaging App V1" & @LF & @LF & _
;~          "GPLV3 software distributed under the License is distributed on an ""AS IS"" BASIS" & @LF & _
;~          "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied." & @LF & _
;~          "See the License for the specific language governing permissions and" & @LF & _
;~          "limitations under the License." & @LF & @LF

;~  Local $size = UBound($CmdLine, 1) - 1
;~  $msg &= "App running with args=" & $size & @LF
;~  For $ii = 0 To $size
;~      $msg &= $ii & " : " & $CmdLine[$ii] & @LF
;~  Next
;~  _DebugOut($msg)
EndIf

sendMessage($CLIENT_START)

While Sleep(10)
    $read = ConsoleRead()
    If @error And $out Then
        _DebugOut("$read...............: " & $out)
        $json = Jsmn_decode($out)
        If @error Then
            _DebugOut("$json...............: @Error")
        Else
            _DebugOut("$json...............: @Ok")
        EndIf

        If IsObj($json) And $json.Exists("text") Then
            sendMessage('{"text":"' & $json.Item("text") & '"}')
        Else
            sendMessage('{"text":"@Error"}')
        EndIf
        $out = ""
    Else
        $out &= $read
    EndIf
WEnd

Func sendMessage($var)
    ConsoleWrite($var & @CRLF)
    _DebugOut("sendMessage===>" & $var)
EndFunc   ;==>Say

 

Edit the file:
C:\Users\Public\GoogleNativeClient\com.google.chrome.example.echo-win.json

And replace:
"path": "NativeMessagingApp.exe",

By:
"path": "NativeMessagingApp2.exe",

Then plugin call the AutoIt script...

But is not functional...

 

About the file mainwindow.cpp, have this function:

inline QString sendMessage(QString data){
  unsigned int len = data.length();
  cout.write(reinterpret_cast<const char *>(&len), 4);
  cout << data.toUtf8().data() << flush;
  return "";
}

How re-write this to AutoIt?
How execute a flush?



Best regards.

Edited by Luigi

Visit my repository

Link to comment
Share on other sites

https://www.autoitscript.com/autoit3/docs/functions/StdoutRead.htm

https://www.autoitscript.com/autoit3/docs/functions/StdinWrite.htm

https://www.autoitscript.com/autoit3/docs/functions/StdioClose.htm

flush most likely not needed 

https://www.autoitscript.com/autoit3/docs/functions/FileFlush.htm

 

although I would expect consoleread/consolewrite should be able to do what you need.

step 1 would be that you see the plugin from chrome starts your compiled exe. you should be able to see that happening with a simple gui window or thru taskmanager

 

Edited by junkew
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...