开发者

Dealing with a very weird gChrome/Javascript problem

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 anythi开发者_如何转开发ng 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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜