Quantcast
Channel: Active questions tagged powershell-3.0 - Stack Overflow
Viewing all articles
Browse latest Browse all 134

How to use powershell as native messaging host?

$
0
0

My requirement is to read the content from the file and send it to the chrome browser extension. I am using native messaging. I did all the configuration for native messaging but failed to send messages from the native messaging host to the extension.I am getting error like "Error when communicating with the native messaging host".I got to know that it was due to the wrong implementation of the native messaging protocol.

The Actual protocol from native messaging is :"The same format is used to send messages in both directions: each message is serialized using JSON, UTF-8 encoded and is preceded with 32-bit message length in native byte order. The maximum size of a single message from the native messaging host is 1 MB, mainly to protect Chrome from misbehaving native applications. The maximum size of the message sent to the native messaging host is 4 GB."

My code is as below:native-messaging-example-host.ps1:

$file = "D:\temp\test\test.txt"$data = Get-content $file -Raw$Objectdata = $data |ConvertFrom-Json$formatedData = $Objectdata |Select-Object -Property message |ConvertTo-Json -Depth 1 -Compress$encconsumerkey= [System.Text.UTF8Encoding]::new().GetBytes($formatedData)$Writer = New-Object System.IO.BinaryWriter([System.Console]::OpenStandardOutput());$Writer.Write([Int32]$encconsumerkey.length)$Writer.Write($encconsumerkey)$Writer$Writer.Flush()$Writer.Close()

My Batch Script which calls the above PowerShell script

MyExample1.bat

@echo offcolor 1Fecho.C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "D:\projects-repo\CromeNativeMessaging\NativeHost\native-messaging-example-host.ps1":EOFecho Waiting secondstimeout /t 10 /nobreak > NUL

NativeMessageHello.json

{"name": "native.messaging.trail.example","description": "My Example1","path": "D:/projects-repo/CromeNativeMessaging/NativeHost/MyExample1.bat","type": "stdio","allowed_origins": ["chrome-extension://lknkdjfamgnamaeaebinefklcobjpphm/"    ]  }

Someone please help me where I am doing wrong.


Viewing all articles
Browse latest Browse all 134

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>