batch y/n popup possible? - javascript

I stumbled upon a bit of code that creates a popup, and you can edit the text from it, add multiple lines...
mshta javascript:alert("Line 1\n Line 2");close();
Go ahead and fire up command prompt and enter in that line. I guess that the \n means next line, as you can add/delete those all you want.
My question is, is there a way to use that same popup method, but rather than have a single button that says "OK", use two buttons, that when you click one, you "goto a", and when click the other, you "goto b".
I also can't figure out how to modify that other popup that I posted above, to say change it from "HTML APPLICATION" to some other title, and rename the "OK" button to something else. Either way, that is beside the point, and just curiosity kicking in.
EDIT: In my last comment, about closing the script, I figured out that I can just throw
;close();
at the end of the parentheses. But how can I set the true/false as a variable in the batch script?
EDIT: I have decided that autoit is a much better language for this kinda thing. Voting to have closed.

Ok, first let me explain what is going on in your example. When you type in:
mshta javascript:alert("Line 1\n Line 2");close();
what you're actually doing is telling your computer:
Run the program called mshta.exe, and pass the following javascript to it as an argument
You can do this with any program, for example, try opening notepad with a new file called "banana.txt" by typing:
notepad banana
Note that both cases have virtually nothing to do with batch. In your example, mshta.exe just runs some javascript to open an alert window, just as in my example notepad.exe deals with the creation of the .txt file.
Ok, so now that we've got that out of the way, onto your actual question: Is there a way to write a script in batch that will open a popup confirmation window?
The simple answer to your question is no. Batch is very good[citation needed] at moving around directories, opening files/programs, and...that's about it. If you need to do anything that isn't in that list, you should really be using a different scripting language.
However, while I cannot emphasize enough how much that is the correct answer to your question, I recognize that it's not a very satisfying one, so I will say that if you absolutely have to have a solution in batch, I would do something like the following:
#echo off
echo code=Msgbox("I'm A Message Box!", vbYesNo, "I'm The Title!") > "%temp%\popupBox.vbs"
echo WScript.Quit code >> "%temp%\popupBox.vbs"
cscript /nologo "%temp%\popupBox.vbs"
if %errorlevel%==6 call :ok_tag
if %errorlevel%==7 call :cancel_tag
echo Done!
exit /b 1
:ok_tag
echo You pressed Yes!
exit /b
:cancel_tag
echo You pressed No!
exit /b
This code essentially circumvents your problem by creating and running a temporary vbscript file (another scripting language) whose job is to display a confirmation window for you, and then using the exit code from that script to determine which button was clicked.
Notice again how much longer and more complicated this is than simply writing the program in pure vbscript to begin with, but if you must have a .bat file, then a .bat file you shall have.

#echo off
for /f "usebackq" %%f in (
`mshta "javascript:new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(new ActiveXObject('WScript.Shell').PopUp('Select one',0,'Title',36));close();"`
) do (
if "%%f"=="6" (
echo YES
) else if "%%f"=="7" (
echo NO
) else (
echo ????
)
)

Related

Return the results of running a program in realtime using Node.js?

Is there a way to run a program (for example: tcpdump) and every time it shows something through the console, from nodejs capture it and return the result to print in an html? no need to save it, keep it in real time.
My idea is that a program can be executed and while it is running and showing things in the console, they can be sent to the html where you can progressively see all the results of the program's execution.
Thanks for help.
I think using socket io is a good solution,
check there get started tutorial
No need to do anything like "capturing output" in Node.js. You can use the heredoc available in almost every shell. The heredoc, in the most basic sense, redirects the output of a command to a file. In your example, if you use tcpdump, here's what you'd do:
$ tcpdump [options] >> file.html
The >> heredoc operator appends to a file(create if doesn't exist). This means if you already have some content in file.html, the content will still be there, and the output will be appended to the end of the file.
The > heredoc writes to a file(also, create if doesn't exist). So, if you have some content in file.html, that will be overwritten with the new content.

How to edit the string "Password: key symbol " printed on Sudo (anything) command?

I came here to ask for your help because I'm a little lost in this challenge I have in hands along some days.
My goal is simple (although I do not know if it's easy to implement on code) - edit/modify the string "Password: " printed on Sudo (anything) command which is usually accompanied by a key symbol on right. My goal is not only modify the sting for another one but also in case of that possibility, replace for another symbols. Looking on Mac OS or iTerm terminals we can see clearly examples of what I'm describing typing "sudo whoami".
Looking on Hyper Open Source terminal to accomplish my objective I don't understand why am I unnable to edit that string on node_modules/getpass/lib/index.js - line 46 (for who has this terminal installed).
It is the only file on this program where the word is supposed to be printed. After modifying the word/string, nothing changed. Am I looking in the wrong place? Is it something more internal/deep to which I do not have access?
I hope you can elucidate me on the way forward. Thanks for your time!
Are you trying to change it in Hyper itself?
You're referencing the module node-getpass, its getPass function looks like this:
.getPass([options, ]callback)
if you pass an object with a prompt property you can set a custom prompt, instead of the text Password.
let options = {
prompt: "Type your password:"
}
const pass = require('getpass')
pass.getPass(options, callback);

Check if html file would open JavaScript alert dialog

I'm wondering if it is possible to, in Java, detect whether or not an HTML file would open an alert dialog if opened in the browser. Preferably headlessly. For example, a file with the below contents were parsed, it would return true.
<html><script>alert("hey")</script></html>
and the below would return true also
<html><iframe src="javascript:alert(1)" onload="alert(2)"></iframe></html>
but the below would return false because it would not open an alert dialog if it were opened in the browser (because none of the code is syntactically correct, and the part that is isn't in a tag).
<html><script>alert;,(123w)</script>alert(1)</html>
I have thought of a way to approach this problem, but it is flawed. Basically, you see if the stringalert(1) is in the file, etc.
The problem with this is that it wouldn't work in cases where that code isn't inside of script tags or tags that make it execute. An example of where it wouldn't work is: The following would return true, even though it wouldn't actually open a popup <html>alert(1)</html>.
This isn't Android by the way. Appreciate your help!
You will need to not only verify if the Alert function is there but check if the JavaScript function would even run. An example of this is if there is a script with an Alert function inside a function that never runs. The Alert function would be there but it would never run. This would give a false positive. So the in the best case you should run the JavaScript in some way to validate the code and to see if the function would ever run.
As Louis pointed out in the comments Option 2 is better in this case as you will need to account for both the DOM and JavaScript's behaviour as both can change if the Alert function runs and how it runs.
Option 1 : Run the JavaScript with Script Engine
You would need some way of separating the HTML from the JavaScript but once you have that you can do this method.
You can run the JavaScript in Java using ScriptEngine. https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/prog_guide/api.html
If you read the API there is a way to create variables and communicate between your Java Program and the JavaScript you are Running.
To capture the context of the Alert you can create a custom JavaScript function that overwrites the Alert function. Inside this custom function you can send the arguments of the function back to your Java Program.
Option 2 : Headless Browser
You can also try to use a headless browser like JBrowserDriver and as you can see you have an Alert interface with getText as a function. For async issue the headless browser has a default amount of time for waiting for the script to complete. If this default amount is not enough you can use the setScriptTimeout to handle it.
http://machinepublishers.github.io/jBrowserDriver/

Selenium IDE - How to handle java-script alerts and confirmations

I am novice to automation testing and have started using Selenium IDE as the choice for automation.
I just want to know if there is any way to handle java-script alerts using IDE.
Take a scenario: I am clicking on "Delete" button and there comes a java-script alert box with OK and Cancel options, but Xpath can not identified for these elements.On the other hand, when I make a script for click event on delete button and run it using Selenium IDE, the element gets automatically deleted.
So, guys please help. Leave a reply in case my question needs to be elaborated.
Thanks in advance.
Take a look at the documentation. The main part is this:
When running under Selenium, JavaScript pop-ups will not appear. This
is because the function calls are actually being overridden at runtime
by Selenium’s own JavaScript. However, just because you cannot see the
pop-up doesn’t mean you don’t have to deal with it. To handle a
pop-up, you must call its assertFoo(pattern) function. If you fail to
assert the presence of a pop-up your next command will be blocked and
you will get an error similar to the following
[error] Error: There was an unexpected Confirmation! [Chose an option.]
A step-by-step manual is in the section about Alerts.
java-script alerts in ide can be handle using selenium commands storeAlert and
storeConfirmation what it will do is it Retrieves the message of a JavaScript confirmation or alert dialog generated during the previous action. this commands can be put at the position where the alert will occur
command:storeAlert
Target:variableName
command:storeConfirmation
Target:variableName
variable name is the variable in which the occurred alert will store
Thank you..

stop javascript execution

When I run javascript script file in windows command line environment, and there is a free text coming after my code. How can I stop javascript interpreter to run into it?
For example:
var fso = new ActiveXObject("Scripting.FileSystemObject");
delete fso;
exit(); // some kind of WORKING exit command
Hungry lazy frog ate a big brown fox.
There is nothing you can do to stop the interpreter before it the compiler sees a particular line because the whole Javascript source file is first compiled to bytecode and it is the bytecode that is interpreted not your source code.
What you could do (though it would still be messy) would be to put some free text in a comment at the end of the file. Then you could open the source file and read it from the rest of the code. It still can't be completely free text though as it would have to be a valid comment
/*
whatever text you want provided it doesn't contain * followed immediately by /
*/
Much better is simply to admit defeat and store any data you need in a separate file.
If you surround that with quotes it will be interpreted/compiled but will have no effect eg
var fso = new ActiveXObject("Scripting.FileSystemObject");
delete fso;
exit(); // some kind of WORKING exit command
"Hungry lazy frog ate a big brown fox.";
Even if you did exit, you'd still have a problem because the whole script block needs to be parsed before the first instruction is executed. With any bogus content following an exit instruction, the script would fail to parse at all, even though that code would never be reached.
If the free text was a single line, you could get away with a trailing //. Doesn't work for multi-line comments though, as you would need a closing */. Same for conditional-comments requiring /*#end #*/.

Categories