2

I used the sample project https://developer.chrome.com/extensions/samples I am able to run the python native app.

Is there any way to get the message within native-messaging-example-host.bat

I don't want to load python script

What I want to do here is send message from chrome {text: "xyz.bat"}

and the batch file should run START xyz.bat

Rob W
  • 341,306
  • 83
  • 791
  • 678
strike123
  • 21
  • 2
  • 2
    Windows batch is a terrible choice for this purpose. If you'd like to continue, just find out how to read the raw data from stdin (without newline mangling). See https://stackoverflow.com/questions/24764657/how-do-i-use-a-shell-script-as-chrome-native-messaging-host-application for my thoughts on using bash (also a shell scripting language) as a native messaging host, you can apply the same reasoning to constructing a native messaging host in your .bat script. – Rob W Apr 12 '15 at 08:50
  • 1
    Thanks for the suggestion, I used there sample python code and used py2exe to make it executable Now I am passing file path from extension and checking if file exists in my python code and passing it to os.system() Thanks again for guidance! – strike123 Apr 13 '15 at 08:57
  • 1
    I hope that you do proper input sanitization (i.e. check that the line-to-be-executed is within the allowed paths), because right now it seems that you have opened a massive security issue in your extension. If there is a way to invoke the native messaging host by an attacker (usually a combination of bugs in Chrome and/or your extension), then you have a full system compromise. – Rob W Apr 13 '15 at 09:16
  • Yes right. The extension is only for our Intranet websites. – strike123 Apr 15 '15 at 16:50

1 Answers1

0

You should not approach this problem from the batch file standpoint, as in lieu of my solution, it requires the program to be run upfront, which in most applications is depreciated in favor of running it in the background. However if you still want to know how you could potentially do it in batch...

If you can pass the message to a blank html page (not currently sure how you can or want to do it this way), where the only thing on that html page is your runme.bat we can run a program that would copy the page, open a text file and paste it inside, close the text file, and run the batch with input from it. So code wise,

@if (@CodeSection == @Batch) @then
@echo off
set SendKeys=CScript //nologo //E:JScript "%~F0"

rem below copys everything on the page, and closes it
%SendKeys% "{TAB}" 
%SendKeys% "^{A}"
%SendKeys% "^{C}"
%SendKeys% "^{W}"

rem open text file,wait for it load, paste clipboard, save and exit

start newreadforme.txt
timeout /nobreak /t 5 
%SendKeys% "^{V}"
%SendKeys% "^{S}"
timeout /nobreak /t 2
%SendKeys% "^{W}"
start program.bat 
goto :EOF
@end
// JScript section
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));

then in your batch file

@echo off
set /p x=<newreadforme.txt
start %x%

This code will run simulated keystrokes on the opened page to copy its contents and relay to a text file to be referenced from another batchfile. But you should only use this method as a last resort as this approach is a TERRIBLE way to solve your problem. My code requires you to keep the webpages open and upfront and make sure no one interferes with the program during its execution. So if a user is using the computer the time its running, then they may accidentally mess with the inputs.

On top of that, already you need to be modifying webpages to achieve your end result so you probably should use a language that supports html to filesystem operations. Nodejs can provide a nice interface between the file system and html pages that you may decide to pass. How you handle the webpage filled with the message i am not sure of but you should most certainly avoid using batch to do what you ask in favor of more html friendly languages

Jouster500
  • 762
  • 12
  • 25