简体   繁体   中英

Programmatically open Firefox and do POST request? [idea: MozRepl, suggestions?]

In my application, I need to have a button which opens Firefox (if not already open), executes a POST request including a file upload, and displays the resulting page to the user (for further navigation).

From what I've found until now, this seems to necessitate the use of a Firefox extension like MozRepl or JSSH, so I can connect to the process via Telnet or SSH from my application. Are there other ways? I'm open to good suggestions...

I would then need to use content.XMLHttpRequest to create a POST request:

var req = new XMLHttpRequest();
req.open("POST", "http://myurl", true);
// [...]
req.send()

But what comes in [...]? There is somehow a File object that is built from local forms; but how can I instantiate and fill one myself? And how do I get the page results to show up in Firefox?

I can either write a temporary file to disk and read it from there, or write the file contents directly via Javascript/MozRepl, both are acceptable for me (but 1) probably not for Javascript).

Thanks in advance, -M.

From the command line you can open a page that will contain filled form and JavaScript that would trigger this form to post. XSS protection only applies to background XMLHttpRequest, it's ok if you are posting a form and automatically move browsing to that page.

So you should open a webpage that would upload a form.

But you aren't even forced to create this webpage separately, you may set it with JavaScript directly in place of URL, just like bookmarklets work:

firefox "javascript:'<html><body onload=\'document.forms[0].submit()\'><form action=\'http://www.example.com\' method=\'POST\'><input name=\'whatever\' value=\'whatever\' type=\'hidden\'></form></body></html>'"

Or you can create an HTML file that would redirect GET to POST, then usage would be like follows:

file:///C:/getToPost?name1=value1&name2=value2#http://url.com/service

This technique is described at this superuser answer with source code for such file.

Note that this HTML file isn't forced to be local, it may be placed on remote server. But take into account that in case of remote file browser would notify final website about your redirecting page as HTTP Referer.

Couldn't you do a GET request to another page on your server that contains AJAX code that sends to the POST request? You can always open a browser with any url that may include a querystring without any fancy API (ie system('firefox.exe http://stackoverflow.com') . That way, you wouldn't need to worry about browser interaction.

Beyond that, why not just make the POST request in your application and display the results in the app?

From the command line:

firefox --new-window www.example.com

Will open up a new window of Firefox, and navigate to the URL you give it. It sounds like you can just use your POST target as the URL, but if not you could always go to a page that makes a $.POST call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM