-->

Chrome Extension - Message Passing to batch file

2019-02-26 08:29发布

问题:

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

回答1:

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