4

I'm working on a Chrome Extension where I have to click on a <input type='file' /> in javascript, via jQuery, to get the "Save As" dialog to show up. Yes, I've been told - most browsers don't allow you to do this. However, I've discovered something very odd - I can click on an file-input element (and have the Save dialog pop-up) if I do it via the address bar, like this:

javascript: $("<input type='file' />").appendTo("body").click();

So, I figured that if I could do it there, then certainly I could do it from my content script...

Obviously I was wrong. Running $("<input type='file' />").appendTo("body").click(); from my content script not only does zilch besides appending the element, but even emulating the address bar and doing window.location = "javascript: $(\"<input type='file' />\").appendTo(\"body\").focus().click();"; does zilch as well.

My initial thought was that maybe this was some kind of chrome restriction in content scripts, but I'm wrong - the extension jsShell which works via content script, is able to run the commands and get my desired results without a hitch.

So, does anyone know why jsShell & the browser can click the file-input and get a Save dialog, but my extension can't? I've tried taking jsShell apart and figuring implementing the way it works (though I don't see anything special about the way it works), but it's STILL not working. And the console isn't revealing anything - no errors, no warnings.

This is making my head overheat, so any help is very appreciated!

mattsven
  • 22,305
  • 11
  • 68
  • 104
  • 1
    Similar questions: http://stackoverflow.com/questions/1829774/jquery-simulating-a-click-on-a-input-type-file-doesnt-work-in-firefox and http://stackoverflow.com/questions/2769001/input-type-file-auto-click – Chris Shouts Mar 09 '11 at 18:27

1 Answers1

1

Not sure what is the root of the problem, but I managed to narrow it down a bit, maybe it would be helpful to you.

Seems like click event is triggered only if fired inside any other event. For example this works:

$("body").mouseover(test);

function test() {
    $("<input type='file' />").appendTo("body").click();
}

This works too:

$("body").append($("<a/>").text("test").click(test));

But this doesn't:

$("body").append($("<a/>").text("test").click(function(){
    setTimeout(test,1000);
}));

Jsshell evaluates your script on a button click so it works, if you try to evaluate this script without button click it won't work.

I am guessing it is a security feature that allows triggering events only when a user performs some action, not from some background process.

serg
  • 109,619
  • 77
  • 317
  • 330
  • Wait...crap, so this means that I still can't do what I wanted...which is click the file-input without user interaction, right? – mattsven Mar 09 '11 at 18:13
  • @motionman95 I edited my code a bit, `body.click` wasn't a good example. Well, I dont' know what you are trying to do. If a user doesn't click some button or moves a mouse in order to show this dialog then probably it wouldn't work (if it is what I think it is) – serg Mar 09 '11 at 18:23
  • I don't think he tested that, either way I believe the conclusion he came to is correct.. – mattsven Mar 09 '11 at 19:02
  • 1
    @motionman95 I tested it and it works, those slashes are just leftovers from other test code (having them doesn't affect anything). – serg Mar 09 '11 at 19:15