2

I try to simulate programmatically the user clicking on an html element type input:file to upload a file to a website with javascript on firefox browser. The following javascript codes in my javascript file simulates and opens the file dialog:

var target_element; 
var dispatchMouseEvent = function(target, var_args) { 
    var e = document.createEvent("MouseEvents");
    e.initEvent.apply(e, Array.prototype.slice.call(arguments, 1));
    target.dispatchEvent(e); 
};

target_element = window.content.document.getElementById("DivElement");
dispatchMouseEvent(target_element, 'mouseover', true, true);
dispatchMouseEvent(target_element, 'mousedown', true, true); 
dispatchMouseEvent(target_element, 'mouseup', true, true); 
dispatchMouseEvent(target_element, 'click', true, true);

but I can't find a way to simulate programmatically the selection of a file on the file dialog like a user selecting a file and click on the button Open of the file dialog. Is that possible to do it with javascript ?

Alex Ng
  • 21
  • 2
  • 1
    here is the javascript codes in the javascript file : – Alex Ng Mar 02 '13 at 12:38
  • If you're looking for automated testing, have a look at this question, these tools may be able to simulate this; http://stackoverflow.com/questions/4043706/automated-testing-of-css-and-html-front-end-coding – thaJeztah Mar 02 '13 at 12:43

1 Answers1

3

This is going to be impossible, and for good reason. If you could automate the selection of a file on client side, you would open the door to massive breaches of security and privacy.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • thanks for your answer. So is it possible to submit/upload the file with javascript without opening the file dialog ? I can see some applications like iMacros which can simulate the file uploaded to the website without any problems. So I guess it should be possible to do it in javascript ? – Alex Ng Mar 02 '13 at 12:51
  • 1
    just imagine some random coder could get any file from your pc and upload it without you noticing it, so for security reasons you can't upload something without the user doing it – Juan Antonio Orozco Mar 02 '13 at 15:00
  • I think there is a misunderstanding. I am talking about javascript running on the user context side aka client browser, not javascript running from server side. How do you want security issues in those case ? Mozilla firefox has tons of libraries to access file systems, I develop tons of script doing that, no security issue at all. Anything done by a user can be done programmatically, the problem in my case it is about a file dialog opened so I can't find a way to reference this file dialog with javascript. – Alex Ng Mar 02 '13 at 17:09
  • @Alex do you mean in a plugin? – Pekka Mar 02 '13 at 18:04
  • there is no need of plugin, not even need of jquery, pure javascript can probably solve the problem. I would like to test if it is possible to reference the file dialog with javascript. There is a Mozilla module interface to reference file dialog https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIFilePicker, but unfortunately I can't not reference the file dialog opened. But I guess it could be possible to send the selected file programmatically to the file dialog. – Alex Ng Mar 03 '13 at 04:37
  • @Alex I mean, do you mean in the context of a plugin? – Pekka Mar 03 '13 at 10:18
  • yes, javascript is running in the context of the plugin imacros. – Alex Ng Mar 03 '13 at 19:09
  • @Alex that's vital information that you should put in your question. The way it currently reads, it suggests you want to do this from a web site – Pekka Mar 03 '13 at 19:13