Batch file not prompting for user input when triggered using chrome native messaging

I am working on a Chrome extension Extension and using Native messaging for invoking Ping.exe. Since I need to choose IP's at runtime, I was thinking of having a batch file which can prompt user for entering IP's.It works fine, if i directly click on the batch file and i get proper response(also i get user prompt).But when I try to invoke it through a UI which i have created, using Native messaging it does not prompt user and directly invoke the exe with the default IP set.What's wrong i am doing here ?

Thanks in advance.

Jaideep

batch File -

@echo off
set id= 192.168.1.1
set /p id= Enter IP:
set Pathname="C:\Windows\System32"
cd /d %Pathname%
start call PING.exe %id%
PAUSE

JS -

document.getElementById('but1').addEventListener(
      'click', connect);

function connect() {
	
  var hostName = "com.google.chrome.example.echo"; 
  port = chrome.runtime.connectNative(hostName);
  port.onMessage.addListener(onNativeMessage);
  port.onDisconnect.addListener(onDisconnected);
}
}

Manifest -- 

{
  "name": "com.google.chrome.example.echo",
  "description": "My Application",
  "path": "C:\\Extension\\ping.bat",
  "type": "stdio",
  "allowed_origins": [
    "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"
  ]
}

]3

Upvotes: 1

Views: 194

Answers (2)

Jean-François Fabre
Jean-François Fabre

Reputation: 140168

That's because your UI creates the process without a valid standard input or a redirected standard input.

When interactive set tries to scan the input, it gets "end of file", and keeps the default, previously set value.

I can propose you an even better alternative, since it's graphical:

1) create a .vbs script (in the same directory as your batch file) that prompts for an address and prints it to stdout:

SET FS = CreateObject("Scripting.FileSystemObject")
SET StdOut = FS.GetStandardStream(1)
StdOut.Write(InputBox("IP Address"))

2) in your batch script do this:

cscript /nologo %~pd0\ipprompt.vbs > %TEMP%\ipaddr.txt
set /p id=<%TEMP%\ipaddr.txt

A VB window will appear prompting you for IP address, and echoes it, redirected to a file. Put that back in your id env. variable and you're done, without using standard input at all.

Upvotes: 2

Xan
Xan

Reputation: 77503

When using Native Messaging, Chrome is the input of the process (it creates an instance of the native host with STDIN/STDOUT piped to Chrome process).

 +-----------+        +-----------------+
 |           |        |                 |
 |           |        | STDIN           |
 |        +------------------->         |
 | Chrome    |        |         Native  |
 | extension |        | STDOUT   host   |
 |        <-------------------+         |
 |           |        |                 |
 +-----------+        +-----------------+

Since input of batch file is done through STDIN, you cannot get typed input in this case. If you want user input, you'll need a UI program (that also communicates via STDIO with Chrome).

Upvotes: 1

Related Questions